<?php

namespace App\Filament\Exports;

use App\Enums\Channel;
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 MediaMonitoringExcelExport extends ExcelExport implements WithEvents, WithStyles
{
    public function setUp()
    {
        $this->withFilename(fn ($resource) => $resource::getModelLabel().'-'.date('Y-m-d'));

        $this->withColumns([
            Column::make('code')
                ->heading('Kode'),

            Column::make('media_name')
                ->heading('Nama Media'),

            Column::make('title')
                ->heading('Judul'),

            Column::make('channel')
                ->heading('Kanal')
                ->formatStateUsing(fn ($state) => Channel::tryFrom($state)?->getLabel() ?? $state),

            Column::make('writter')
                ->heading('Penulis'),

            Column::make('link')
                ->heading('Tautan'),

            Column::make('news_page')
                ->heading('Halaman Berita'),

            Column::make('influencer')
                ->heading('Influencer'),

            Column::make('keyword')
                ->heading('Kata Kunci'),

            Column::make('themes')
                ->heading('Tema')
                ->getStateUsing(fn ($record) => $record->themes->pluck('name')->join(', ')),

            Column::make('release_date')
                ->heading('Tgl Rilis'),
        ]);
    }

    public function headings(): array
    {
        return [
            ['Data Monitoring Media'],
            $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 = 'K'; // Based on column count (A-K)

                $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);
                }
            },
        ];
    }
}
