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

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

            Column::make('partnerMedia.name')
                ->heading('Media')
                ->getStateUsing(fn ($record) => $record->partnerMedia->pluck('name')->join(', ')),

            Column::make('initial_submission_date')
                ->heading('Tgl Pengajuan Awal')
                ->formatStateUsing(fn ($state) => $state?->translatedFormat('l, d F Y')),

            Column::make('final_submission_date')
                ->heading('Tgl Pengajuan Akhir')
                ->formatStateUsing(fn ($state) => $state?->translatedFormat('l, d F Y')),

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

            Column::make('payment_amount')
                ->heading('Jumlah Pembayaran')
                ->formatStateUsing(fn ($state) => 'Rp '.number_format($state, 0, ',', '.')),

            Column::make('payment_date')
                ->heading('Tanggal Pembayaran')
                ->formatStateUsing(fn ($state) => $state?->translatedFormat('l, d F Y')),

            Column::make('description')
                ->heading('Deskripsi'),
        ]);
    }

    public function headings(): array
    {
        return [
            ['Data Kerja Sama'],
            $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 = 'H'; // A-H

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