<?php

namespace App\Services;

use App\Models\LeasingRecord;
use Carbon\Carbon;

class MemberRecordFormatterService
{
    public function format(LeasingRecord $record): array
    {
        return [
            'TGL' => $this->formatDate($record->tgl_data),
            'NOPOL' => strtoupper($record->nopol),
            'RANGKA' => strtoupper($record->rangka),
            'MESIN' => strtoupper($record->mesin),
            'MODEL' => strtoupper($record->model ?: '-'),
            'TAHUN' => $record->tahun ?? '-',
            'KENDARAAN' => strtoupper($record->kendaraan ?: '-'),
            'LEASING' => strtoupper($record->leasingProfile->company_name ?? 'N/A'),
            'CABANG' => strtoupper($record->branch?->branch_name ?? 'PUSAT'),
            'OVERDUE' => number_format($record->overdue_days),
            'SISA_SALDO' => number_format($record->sisa_saldo, 0, ',', '.'),
            'WARNING' => strip_tags($record->member_warning ?: 'DATA_VERIFIED_SECURE'),
            'INSTRUKSI' => strip_tags($record->member_instruction ?: 'NO_IMMEDIATE_ACTION_REQUIRED'),
            'LEGAL_FLAG' => $record->legal_flag,
        ];
    }

    private function formatDate($date): string
    {
        if (!$date) return '-';
        
        $carbon = Carbon::parse($date);
        $months = [
            1 => 'JAN', 2 => 'FEB', 3 => 'MAR', 4 => 'APR', 
            5 => 'MEI', 6 => 'JUN', 7 => 'JUL', 8 => 'AGU', 
            9 => 'SEP', 10 => 'OKT', 11 => 'NOV', 12 => 'DES'
        ];

        return $carbon->format('d/') . $months[$carbon->month] . $carbon->format('/Y');
    }
}
