<?php

namespace App\Exports;

use App\Models\Perfume;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class PerfumeExport implements FromCollection, ShouldAutoSize, WithColumnWidths, WithEvents, WithHeadings, WithMapping, WithStyles, WithTitle
{
    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        return Perfume::with('brand')->get();
    }

    public function title(): string
    {
        return 'Data Parfum';
    }

    public function headings(): array
    {
        return [
            'Aroma',
            'SKU',
            'Merek',
            'Konsentrasi',
            'Harga Jual',
            'Base Notes',
            'Middle Notes',
            'Top Notes',
            'Deskripsi',
            'Tanggal Dibuat',
            'Tanggal Diperbarui',
        ];
    }

    /**
     * @param  Perfume  $perfume
     */
    public function map($perfume): array
    {
        return [
            $perfume->name,
            $perfume->sku,
            $perfume->brand?->name,
            $perfume->concentration->label(),
            $perfume->sale_price,
            $perfume->base_notes,
            $perfume->middle_notes,
            $perfume->top_notes,
            strip_tags($perfume->description),
            formatDateLocalized($perfume->created_at, 'd/m/Y H:i'),
            formatDateLocalized($perfume->updated_at, 'd/m/Y H:i'),
        ];
    }

    public function columnWidths(): array
    {
        return [
            'A' => 28,  // Perfume Name
            'B' => 18,  // SKU
            'C' => 23,  // Brand
            'D' => 23,  // Concentration
            'E' => 18,  // Sale Price
            'F' => 23,  // Base Notes
            'G' => 23,  // Middle Notes
            'H' => 23,  // Top Notes
            'I' => 35,  // Description
            'J' => 21,  // Created Date
            'K' => 21,  // Updated Date
        ];
    }

    /**
     * @return array
     */
    public function styles(Worksheet $sheet)
    {
        $lastRow = $sheet->getHighestRow();
        $lastColumn = $sheet->getHighestColumn();

        // Header style
        $headerStyle = [
            'font' => [
                'bold' => true,
                'size' => 12,
                'color' => ['rgb' => 'FFFFFF'],
            ],
            'fill' => [
                'fillType' => Fill::FILL_SOLID,
                'startColor' => ['rgb' => '1E40AF'], // Navy Blue - elegant
            ],
            'alignment' => [
                'horizontal' => Alignment::HORIZONTAL_CENTER,
                'vertical' => Alignment::VERTICAL_CENTER,
            ],
            'borders' => [
                'allBorders' => [
                    'borderStyle' => Border::BORDER_THIN,
                    'color' => ['rgb' => '000000'],
                ],
            ],
        ];

        // Data rows style
        $dataStyle = [
            'borders' => [
                'allBorders' => [
                    'borderStyle' => Border::BORDER_THIN,
                    'color' => ['rgb' => '000000'],
                ],
            ],
            'alignment' => [
                'vertical' => Alignment::VERTICAL_TOP,
                'horizontal' => Alignment::HORIZONTAL_LEFT,
                'indent' => 1, // Add padding
            ],
        ];

        // Currency columns style (format currency)
        $currencyStyle = [
            'numberFormat' => [
                'formatCode' => '"Rp" #,##0',
            ],
        ];

        return [
            // Header styling
            1 => $headerStyle,

            // Data rows styling
            'A2:'.$lastColumn.$lastRow => $dataStyle,

            // Currency columns styling
            'E2:E'.$lastRow => $currencyStyle,
        ];
    }

    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function (AfterSheet $event) {
                // Auto-size columns with max width
                foreach (range('A', 'K') as $column) {
                    $event->sheet->getColumnDimension($column)->setAutoSize(true);
                }

                // Set row height for header
                $event->sheet->getRowDimension(1)->setRowHeight(25);
            },
        ];
    }
}
