<?php

namespace App\Http\Requests\Auth;

use Illuminate\Foundation\Http\FormRequest;

class UpdateLeasingRecordRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'tgl_data' => 'required|date',
            'nopol' => 'required|string|max:20',
            'rangka' => 'required|string|max:50',
            'mesin' => 'required|string|max:50',
            'kendaraan' => 'required|string|max:100',
            'model' => 'nullable|string|max:100',
            'customer_name' => 'nullable|string|max:255',
            'customer_phone' => 'nullable|string|max:20',
            'customer_address' => 'nullable|string',
            'branch_id' => [
                'required', 
                'exists:branches,id',
                function ($attribute, $value, $fail) {
                    $profile = auth()->user()->leasingProfile;
                    if (!$profile || !$profile->branches()->where('id', $value)->exists()) {
                        $fail('Cabang yang dipilih tidak valid atau bukan milik perusahaan Anda.');
                    }
                },
            ],
            'overdue_days' => 'nullable|integer|min:0',
            'sisa_saldo' => 'nullable|numeric|min:0',
            'legal_flag' => 'boolean',
            'member_warning' => 'nullable|string',
            'member_instruction' => 'nullable|string',
            'internal_note' => 'nullable|string',
            'record_status' => 'required|in:active,closed',
        ];
    }

    public function messages(): array
    {
        return [
            'tgl_data.required' => 'Tanggal data wajib diisi.',
            'tgl_data.date' => 'Format tanggal data tidak valid.',
            'nopol.required' => 'Nomor Polisi wajib diisi.',
            'nopol.max' => 'Nomor Polisi maksimal 20 karakter.',
            'rangka.required' => 'Nomor Rangka wajib diisi.',
            'mesin.required' => 'Nomor Mesin wajib diisi.',
            'kendaraan.required' => 'Jenis kendaraan wajib diisi.',
            'branch_id.required' => 'Cabang wajib dipilih.',
            'branch_id.exists' => 'Cabang yang dipilih tidak valid.',
            'record_status.required' => 'Status record wajib diisi.',
            'record_status.in' => 'Status record harus berupa active atau closed.',
            'overdue_days.integer' => 'Overdue harus berupa angka.',
            'sisa_saldo.numeric' => 'Sisa saldo harus berupa angka.',
        ];
    }
}
