<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Color;

class QuestionBankFormatExport implements FromArray, WithHeadings, ShouldAutoSize, WithStyles, WithTitle
{
    public function title(): string
    {
        return 'Format Import Soal';
    }

    public function headings(): array
    {
        return [
            'lesson_id',
            'type',
            'difficulty',
            'kompetensi',
            'question',
            'option_1',
            'option_2',
            'option_3',
            'option_4',
            'option_5',
            'answer',
        ];
    }

    public function array(): array
    {
        return [
            // Row keterangan kolom
            [
                'ID Mata Pelajaran (angka)',
                'pg / pg_kompleks / menjodohkan / isian / uraian',
                'mudah / sedang / sulit',
                'Kompetensi Dasar (opsional)',
                'Teks soal lengkap',
                'Teks pilihan A',
                'Teks pilihan B',
                'Teks pilihan C',
                'Teks pilihan D',
                'Teks pilihan E (opsional)',
                'Jawaban benar (1/2/3/4/5 atau teks essay)',
            ],
            // Baris pemisah kosong
            ['', '', '', '', '', '', '', '', '', '', ''],
            // Contoh data 1
            [
                '1',
                'pg',
                'mudah',
                'KD 3.1',
                'Siapakah Presiden pertama Indonesia?',
                'Soekarno',
                'Soeharto',
                'Habibie',
                'Megawati',
                '',
                '1',
            ],
            // Contoh data 2
            [
                '2',
                'pg_kompleks',
                'sedang',
                'KD 3.2',
                'Manakah yang termasuk bilangan prima? (bisa lebih dari satu jawaban)',
                '2',
                '3',
                '4',
                '5',
                '7',
                '1,2,4',
            ],
            // Contoh data 3
            [
                '3',
                'uraian',
                'sulit',
                'KD 4.1',
                'Jelaskan proses terjadinya fotosintesis!',
                '',
                '',
                '',
                '',
                '',
                'Fotosintesis adalah...',
            ],
        ];
    }

    public function styles(Worksheet $sheet)
    {
        // Style baris header (baris 1 = heading, baris 2 = keterangan)
        $lastCol = 'K';

        // Header row styling
        $sheet->getStyle("A1:{$lastCol}1")->applyFromArray([
            'font' => [
                'bold'  => true,
                'color' => ['argb' => 'FFFFFFFF'],
                'size'  => 11,
            ],
            'fill' => [
                'fillType'   => Fill::FILL_SOLID,
                'startColor' => ['argb' => 'FF1F4E79'],
            ],
            'alignment' => [
                'horizontal' => Alignment::HORIZONTAL_CENTER,
                'vertical'   => Alignment::VERTICAL_CENTER,
                'wrapText'   => true,
            ],
            'borders' => [
                'allBorders' => [
                    'borderStyle' => Border::BORDER_THIN,
                    'color'       => ['argb' => 'FFBFBFBF'],
                ],
            ],
        ]);

        // Keterangan row styling (row 2)
        $sheet->getStyle("A2:{$lastCol}2")->applyFromArray([
            'font' => [
                'italic' => true,
                'color'  => ['argb' => 'FF595959'],
                'size'   => 9,
            ],
            'fill' => [
                'fillType'   => Fill::FILL_SOLID,
                'startColor' => ['argb' => 'FFDAE3F3'],
            ],
            'alignment' => [
                'horizontal' => Alignment::HORIZONTAL_CENTER,
                'vertical'   => Alignment::VERTICAL_CENTER,
                'wrapText'   => true,
            ],
            'borders' => [
                'allBorders' => [
                    'borderStyle' => Border::BORDER_THIN,
                    'color'       => ['argb' => 'FFBFBFBF'],
                ],
            ],
        ]);

        // Baris pemisah (row 3) - warna abu muda
        $sheet->getStyle("A3:{$lastCol}3")->applyFromArray([
            'fill' => [
                'fillType'   => Fill::FILL_SOLID,
                'startColor' => ['argb' => 'FFF2F2F2'],
            ],
        ]);

        // Baris contoh data styling (row 4 & 5)
        foreach ([4, 5] as $rowNum) {
            $bgColor = ($rowNum % 2 === 0) ? 'FFFFFFFF' : 'FFEAF4FB';
            $sheet->getStyle("A{$rowNum}:{$lastCol}{$rowNum}")->applyFromArray([
                'fill' => [
                    'fillType'   => Fill::FILL_SOLID,
                    'startColor' => ['argb' => $bgColor],
                ],
                'borders' => [
                    'allBorders' => [
                        'borderStyle' => Border::BORDER_THIN,
                        'color'       => ['argb' => 'FFBFBFBF'],
                    ],
                ],
                'alignment' => [
                    'vertical' => Alignment::VERTICAL_CENTER,
                    'wrapText' => true,
                ],
            ]);
        }

        // Set row heights
        $sheet->getRowDimension(1)->setRowHeight(30);
        $sheet->getRowDimension(2)->setRowHeight(40);

        // Freeze header row
        $sheet->freezePane('A2');

        return [];
    }
}
