<?php

namespace App\Actions\Reports;

use App\Actions\Reports\Concerns\StreamsReportExport;
use Illuminate\Support\Collection;
use Symfony\Component\HttpFoundation\StreamedResponse;

/** Stream the Sales by Cashier report as CSV / XLSX / PDF. */
class ExportSalesByCashier
{
    use StreamsReportExport;

    /** @param Collection<int, array<string, mixed>> $rows */
    public function __invoke(Collection $rows, string $from, string $to, string $format = 'csv'): StreamedResponse
    {
        $header = apply_filters('reports.sales_by_cashier.export.header', [
            'Cashier', 'Sales', 'Items sold', 'Revenue', 'Discounts', 'Avg basket',
        ]);

        $out = $rows->map(fn ($r) => apply_filters('reports.sales_by_cashier.export.row', [
            (string) $r['name'],
            (string) $r['sales_count'],
            (string) $r['items_sold'],
            (string) $r['revenue'],
            (string) $r['discounts'],
            (string) $r['avg_basket'],
        ], $r))->all();

        return $this->respondWithReportExport('sales-by-cashier', __('reports.sales_by_cashier.title'), $header, $out, $format, $from, $to);
    }
}
