<?php

namespace Modules\Lpj\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Modules\Lpj\Models\LaporanAdminKredit;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class LaporanAdminKreditExport implements WithColumnFormatting, WithHeadings, FromCollection, WithMapping
{
    public function collection()
    {
        return LaporanAdminKredit::with('debiture')->get();
    }

    public function map($row): array
    {
        return [
            $row->id,
            $row->debiture->cif,
            $row->debiture->name,
            $row->debiture->branch->name,
            $row->kode_register_t24,
            $row->jenis_agunan,
            $row->bukti_kepemilikan,
            $row->alamat_agunan,
            $row->nama_pemilik,
            $row->tanggal_kunjungan,
            $row->nilai_pasar_wajar,
            $row->nilai_likuidasi,
            $row->nama_penilai,
            $row->kolektibilitas,
            $row->keterangan,
            $row->created_at
        ];
    }

    public function headings(): array
    {
        return [
            'ID',
            'CIF',
            'Nama Debitur',
            'Cabang',
            'Kode Register T24',
            'Jenis Agunan',
            'Bukti Kepemilikan',
            'Alamat Agunan',
            'Nama Pemilik',
            'Tanggal Kunjungan',
            'Nilai Pasar Wajar',
            'Nilai Likuidasi',
            'Nama Penilai',
            'Kolektibilitas',
            'Keterangan',
            'Created At'
        ];
    }

    public function columnFormats(): array
    {
        return [
            'A' => NumberFormat::FORMAT_NUMBER,
            'B' => NumberFormat::FORMAT_NUMBER,
            'J' => NumberFormat::FORMAT_DATE_DDMMYYYY,
            'K' => NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1,
            'L' => NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1,
            'N' => NumberFormat::FORMAT_TEXT, // Kolektibilitas
            'O' => NumberFormat::FORMAT_TEXT, // Keterangan
            'P' => NumberFormat::FORMAT_DATE_DATETIME, // Created At (moved from M to P)
        ];
    }
}
