<?php

namespace App\Http\Requests;

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

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

    protected function prepareForValidation(): void
    {
        if ($this->codigo) {
            $this->merge(['codigo' => strtoupper(trim($this->codigo))]);
        }
    }

    public function rules(): array
    {
        return [
            'codigo' => ['required', 'string', 'max:50', Rule::unique('cupones', 'codigo')->ignore($this->route('cupon'))],
            'tipo' => ['required', Rule::in(['porcentaje', 'monto_fijo'])],
            'porcentaje' => ['required_if:tipo,porcentaje', 'nullable', 'numeric', 'min:0.01', 'max:100'],
            'monto' => ['required_if:tipo,monto_fijo', 'nullable', 'numeric', 'min:0.01'],
            'origen' => ['required', Rule::in(['manual', 'sistema'])],
            'fecha_inicio' => ['nullable', 'date'],
            'fecha_fin' => ['nullable', 'date', 'after_or_equal:fecha_inicio'],
            'usos_maximos' => ['nullable', 'integer', 'min:1'],
        ];
    }
}
