<?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;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;

class InventoryExport implements FromCollection, WithHeadings, WithMapping, WithStyles, ShouldAutoSize
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return Inventory::with(['category', 'supplier'])->get();
    }
    
    /**
     * @return array
     */
    public function headings(): array
    {
        return [
            'ID',
            'Nombre',
            'Descripción',
            'Categoría',
            'Cantidad',
            'Stock Mínimo',
            'Precio Unitario',
            'Estado',
            'Ubicación',
            'Serie',
            'Marca',
            'Modelo',
            'Proveedor',
            'Fecha Vencimiento',
            'Última Actualización'
        ];
    }
    
    /**
     * @param mixed $row
     *
     * @return array
     */
    public function map($item): array
    {
        return [
            $item->id,
            $item->name,
            $item->description,
            $item->category ? $item->category->name : 'N/A',
            $item->quantity,
            $item->min_stock,
            $item->unit_price,
            $item->status,
            $item->location,
            $item->serial_number,
            $item->brand,
            $item->model,
            $item->supplier ? $item->supplier->name : 'N/A',
            $item->expiration_date,
            $item->updated_at->format('Y-m-d H:i:s')
        ];
    }
    
    /**
     * @param Worksheet $sheet
     *
     * @return void
     */
    public function styles(Worksheet $sheet)
    {
        return [
            // Estilo para la primera fila (encabezados)
            1 => [
                'font' => ['bold' => true],
                'fill' => [
                    'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
                    'startColor' => ['rgb' => '4A90E2']
                ],
                'font' => [
                    'color' => ['rgb' => 'FFFFFF']
                ]
            ]
        ];
    }
}
