<?php

namespace App\Http\Requests\Admin;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

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

    public function rules(): array
    {
        $id = $this->route('voucher') ?? $this->input('id');

        return [
            'code'         => ['required', 'string', 'max:50', 'alpha_num',
                               Rule::unique('vouchers', 'code')->ignore($id)->whereNull('deleted_at')],
            'type'         => ['required', Rule::in(['percent', 'fixed'])],
            'value'        => ['required', 'numeric', 'min:0'],
            'min_order'    => ['nullable', 'numeric', 'min:0'],
            'max_discount' => ['nullable', 'numeric', 'min:0'],
            'quota'        => ['nullable', 'integer', 'min:1'],
            'valid_from'   => ['nullable', 'date'],
            'valid_until'  => ['nullable', 'date', 'after_or_equal:valid_from'],
            'is_active'    => ['boolean'],
        ];
    }

    public function messages(): array
    {
        return [
            'code.required'       => 'Kode voucher wajib diisi.',
            'code.alpha_num'      => 'Kode hanya boleh huruf dan angka.',
            'code.unique'         => 'Kode voucher sudah digunakan.',
            'type.required'       => 'Jenis diskon wajib dipilih.',
            'value.required'      => 'Nilai diskon wajib diisi.',
            'valid_until.after_or_equal' => 'Tanggal akhir harus sama atau setelah tanggal mulai.',
        ];
    }
}
