<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use App\Rules\LocationCoordinates;
use Illuminate\Validation\Rule;

class UpdateVineyardRowRequest 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 vineyard rows)
        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 [
            'variety_variation_id' => ['nullable', 'integer', 'exists:variety_variations,id'],
            'vine_count' => ['required', 'integer', 'min:1', 'max:10000'],
            'planting_year' => ['required', 'integer', 'min:1900', 'max:' . $currentYear],
            'area' => ['nullable', 'numeric', 'min:0', 'max:99999.99'],
            'location' => ['nullable', 'string', 'max:255', new LocationCoordinates()],
            'status' => ['required', 'in:active,inactive,replanting,discarded'],
            'notes' => ['nullable', 'string', 'max:1000'],
        ];
    }

    /**
     * Get custom attribute names for validator errors.
     *
     * @return array<string, string>
     */
    public function attributes(): array
    {
        return [
            'variety_variation_id' => 'variety variation',
            'vine_count' => 'vine count',
            'planting_year' => 'planting year',
            'area' => 'area (m²)',
        ];
    }
}
