<?php

namespace App\Exports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithCustomStartCell;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Fill;

class IssuedVoidChequesExport implements FromCollection, WithHeadings, WithMapping, ShouldAutoSize, WithCustomStartCell, WithEvents, WithTitle
{
    public function __construct(
        private readonly Collection $rows,
        private readonly array $visibleColumns,
        private readonly array $columnLabels,
        private readonly string $companyName,
        private readonly string $filterSummary,
    ) {
    }

    public function collection(): Collection
    {
        return $this->rows;
    }

    public function startCell(): string
    {
        return 'A5';
    }

    public function headings(): array
    {
        return array_merge(
            ['SL'],
            array_map(
                fn (string $key) => $this->columnLabels[$key] ?? ucfirst(str_replace('_', ' ', $key)),
                $this->visibleColumns,
            ),
        );
    }

    public function map($row): array
    {
        return array_merge(
            [$row['serial']],
            array_map(
                fn (string $key) => $row['cells'][$key] ?? '',
                $this->visibleColumns,
            ),
        );
    }

    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function (AfterSheet $event): void {
                $sheet = $event->sheet->getDelegate();
                $columnCount = max(count($this->visibleColumns) + 1, 1);
                $lastColumn = Coordinate::stringFromColumnIndex($columnCount);
                $headerRow = 5;

                $sheet->setCellValue('A1', $this->companyName);
                $sheet->setCellValue('A2', 'Issued & Void Cheques');
                $sheet->setCellValue('A3', $this->filterSummary);

                foreach ([1, 2, 3] as $row) {
                    $sheet->mergeCells("A{$row}:{$lastColumn}{$row}");
                    $sheet->getStyle("A{$row}")
                        ->getAlignment()
                        ->setHorizontal(Alignment::HORIZONTAL_CENTER);
                }

                $sheet->getStyle('A1')->getFont()->setBold(true)->setSize(14);
                $sheet->getStyle('A2')->getFont()->setBold(true)->setSize(12);
                $sheet->getStyle("A{$headerRow}:{$lastColumn}{$headerRow}")
                    ->getFont()
                    ->setBold(true);
                $sheet->getStyle("A{$headerRow}:{$lastColumn}{$headerRow}")
                    ->getFill()
                    ->setFillType(Fill::FILL_SOLID)
                    ->getStartColor()
                    ->setARGB('FFE5E7EB');
            },
        ];
    }

    public function title(): string
    {
        return 'Issued & Void Cheques';
    }
}
