<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UpdateWineRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        // TODO: Implement authorization logic (check if user has permission to update wines)
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        $currentYear = date('Y');

        return [
            'vintage' => ['required', 'integer', 'min:1900', 'max:' . ($currentYear + 1)],
            'grape_variety_id' => ['required', 'exists:grape_varieties,id'],
            'wine_name' => ['required', 'string', 'max:255'],
            'wine_type' => ['nullable', 'in:red,white,rose'],
            'sweetness' => ['nullable', 'in:dry,semi_dry,semi_sweet,sweet'],
            'description' => ['nullable', 'string'],
            'alcohol_percentage' => ['nullable', 'numeric', 'min:0', 'max:20'],
            'bottles_produced' => ['required', 'integer', 'min:0'],
            'bottles_in_stock' => ['required', 'integer', 'min:0', 'lte:bottles_produced'],
            'price_per_bottle' => ['nullable', 'numeric', 'min:0'],
            'bottle_volume' => ['nullable', 'numeric', 'min:0', 'max:10'],
            'production_date' => ['nullable', 'date', 'before_or_equal:today'],
            'bottling_date' => ['nullable', 'date', 'before_or_equal:today', 'after_or_equal:production_date'],
            'status' => ['required', 'in:in_production,aging,ready,sold_out'],
            'image_url' => ['nullable', 'url', 'max:500'],
            'image' => ['nullable', 'image', 'mimes:jpeg,jpg,png,webp', 'max:2048'],
        ];
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array<string, string>
     */
    public function messages(): array
    {
        return [
            'bottles_in_stock.lte' => 'Bottles in stock cannot exceed bottles produced.',
            'bottling_date.after_or_equal' => 'Bottling date must be after or equal to production date.',
        ];
    }

    /**
     * Get custom attribute names for validator errors.
     *
     * @return array<string, string>
     */
    public function attributes(): array
    {
        return [
            'grape_variety_id' => 'grape variety',
            'wine_name' => 'wine name',
            'wine_type' => 'wine type',
            'alcohol_percentage' => 'alcohol percentage',
            'bottles_produced' => 'bottles produced',
            'bottles_in_stock' => 'bottles in stock',
            'price_per_bottle' => 'price per bottle',
            'bottle_volume' => 'bottle volume',
            'production_date' => 'production date',
            'bottling_date' => 'bottling date',
            'image_url' => 'image URL',
        ];
    }
}
