<?php

namespace App\Modules\Inventory\Exports;

use App\Modules\Inventory\Services\InventoryReportService;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class InventoryReportExport implements FromCollection, WithHeadings, WithMapping, ShouldAutoSize, WithStyles
{
    protected $service;
    protected $filters;

    public function __construct(InventoryReportService $service, array $filters)
    {
        $this->service = $service;
        $this->filters = $filters;
    }

    public function collection()
    {
        return $this->service->getReportData($this->filters, null); // perPage = null to get all
    }

    public function headings(): array
    {
        return [
            '商品代碼',
            '商品名稱',
            '分類',
            '進貨量',
            '出貨量',
            '調撥入',
            '調撥出',
            '調整量',
            '淨變動',
        ];
    }

    public function map($row): array
    {
        return [
            $row->product_code,
            $row->product_name,
            $row->category_name ?? '-',
            $row->inbound_qty,
            $row->outbound_qty,
            $row->transfer_in_qty,
            $row->transfer_out_qty,
            $row->adjust_qty,
            $row->net_change,
        ];
    }

    public function styles(Worksheet $sheet)
    {
        return [
            1 => ['font' => ['bold' => true]],
        ];
    }
}
