<?php

namespace App\Filament\Exports;

use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use pxlrbt\FilamentExcel\Columns\Column;
use pxlrbt\FilamentExcel\Exports\ExcelExport;

class IssueManagementExcelExport extends ExcelExport implements WithEvents, WithStyles
{
    public function setUp()
    {
        $this->withFilename(fn ($resource) => $resource::getModelLabel().'-'.date('Y-m-d'));

        $this->withColumns([
            Column::make('mediaMonitoring.title')
                ->heading('Judul Berita'),

            Column::make('issue')
                ->heading('Isu')
                ->formatStateUsing(fn ($state) => $state?->getLabel()),

            Column::make('response')
                ->heading('Respon')
                ->formatStateUsing(fn ($state) => $state?->getLabel()),

            Column::make('location.name')
                ->heading('Lokus'),

            Column::make('subLocation.name')
                ->heading('Sub Lokus'),

            Column::make('classification.name')
                ->heading('Klasifikasi'),

            Column::make('subClassification.name')
                ->heading('Sub Klasifikasi'),

            Column::make('description')
                ->heading('Keterangan'),

            Column::make('mediaMonitoring.release_date')
                ->heading('Tgl Rilis')
                ->formatStateUsing(fn ($state) => $state?->translatedFormat('l, d F Y')),
        ]);
    }

    public function headings(): array
    {
        return [
            ['Data Manajemen Isu'],
            $this->getHeadings(),
        ];
    }

    public function styles(Worksheet $sheet)
    {
        return [
            1 => [
                'font' => ['bold' => true, 'size' => 14],
                'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER],
            ],
            2 => [
                'font' => ['bold' => true],
                'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER],
            ],
        ];
    }

    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function (AfterSheet $event) {
                $sheet = $event->sheet->getDelegate();
                $lastColumn = 'I'; // A-I

                $sheet->mergeCells("A1:{$lastColumn}1");

                $sheet->getStyle("A2:{$lastColumn}".$sheet->getHighestRow())
                    ->getAlignment()
                    ->setVertical(Alignment::VERTICAL_TOP)
                    ->setWrapText(true);

                for ($i = 3; $i <= $sheet->getHighestRow(); $i++) {
                    $sheet->getRowDimension($i)->setRowHeight(-1);
                }

                foreach (range('A', $lastColumn) as $columnID) {
                    $sheet->getColumnDimension($columnID)->setAutoSize(true);
                }
            },
        ];
    }
}
