<?php

namespace App\Exports;

use App\Models\Inventory;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class InventoryExport implements FromCollection, WithHeadings, WithMapping, WithStyles
{
    protected $inventories;

    public function __construct($inventories)
    {
        $this->inventories = $inventories;
    }

    public function collection()
    {
        return $this->inventories;
    }

    public function headings(): array
    {
        return [
            'Item Name',
            'Category',
            'Unit',
            'Unit Price',
            'Quantity',
            'Total Price',
            'Created At',
            'Updated At'
        ];
    }

    public function map($inventory): array
    {
        return [
            $inventory->item_name,
            $inventory->category ?? '',
            $inventory->item_unit,
            $inventory->unit_price,
            $inventory->quantity,
            $inventory->total_price,
            $inventory->created_at->format('Y-m-d H:i:s'),
            $inventory->updated_at->format('Y-m-d H:i:s'),
        ];
    }

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

