<?php

namespace App\Http\Requests;

use App\Enums\Type;
use App\Models\CustomerType;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreCustomerRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Langkah 1: Normalisasi data sebelum validasi dijalankan
     */
    protected function prepareForValidation()
    {
        if ($this->phone_number) {
            $phone = preg_replace('/[^0-9]/', '', $this->phone_number);
            if (str_starts_with($phone, '0')) {
                $phone = '62' . substr($phone, 1);
            } elseif (str_starts_with($phone, '+62')) {
                $phone = '62' . substr($phone, 3);
            }
            $this->merge([
                'phone_number' => $phone,
            ]);
        }

        // Cek apakah type_id adalah Farmer
        if ($this->type) {
            $customerType = CustomerType::find($this->type);
            $this->merge([
                'is_farmer_type' => $customerType && $customerType->type === Type::Farmer,
                'is_dealer_type' => $customerType && $customerType->type === Type::Dealer,
            ]);
        }
    }

    public function rules(): array
    {
        return [
            'type' => 'required|exists:customer_types,id',
            'name' => [
                'required',
                'string',
                'max:255',
                Rule::unique('customers')->where(fn ($query) => $query->where('type_id', $this->type)),
            ],
            'address' => 'required|string|max:500',
            'owner_name' => 'required|string|max:255',
            'phone_number' => [
                'required',
                'numeric',
                'digits_between:10,15',
                Rule::unique('customers')->where(fn ($query) => $query->where('type_id', $this->type)),
            ],
            'type_crop' => $this->is_farmer_type ? 'required|string|max:100' : 'nullable|string|max:100',
            'land_area' => $this->is_farmer_type ? 'required|numeric|min:0' : 'nullable|numeric|min:0',
        ];

    }

    public function messages(): array
    {
        return [
            'type.required'          => 'Pilih tipe customer terlebih dahulu.',
            'type.exists'            => 'Tipe customer tidak ditemukan.',
            'name.required'             => 'Nama harus diisi.',
            'name.unique'               => 'Nama customer ini sudah terdaftar.',
            'address.required'          => 'Alamat lengkap wajib diisi.',
            'owner_name.required'       => 'Nama pemilik wajib diisi.',
            'phone_number.required'     => 'Nomor HP wajib diisi.',
            'phone_number.numeric'      => 'Nomor HP harus berupa angka.',
            'phone_number.unique'       => 'Nomor HP ini sudah terdaftar.',
            'type_crop.required'        => 'Tipe crop wajib diisi untuk kategori Petani.',
            'land_area.required'        => 'Luas lahan wajib diisi untuk kategori Petani.',
        ];
    }
}
