<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;

class TemplateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, ValidationRule|array|string>
     */
    public function rules(): array
    {
        return [
            'title' => 'required|string|max:255',
            'description' => 'nullable|string',
            'rows' => 'required|integer|min:1|max:3',
            'columns' => 'required|integer|min:1|max:3',
            'charts' => 'required|array|min:1',
            'charts.*.device' => 'required|uuid|exists:devices,uuid',
            'charts.*.registers' => 'required|array|min:1',
            'charts.*.registers.*' => 'required|uuid|exists:registers,uuid',
            'charts.*.count' => 'required|integer|min:1',
        ];
    }

    /**
     * Get custom attributes for validator errors.
     */
    public function attributes(): array
    {
        return [
            'charts.*.count' => 'count',
            'charts.*.device' => 'device',
            'charts.*.registers' => 'registers',
        ];
    }

    /**
     * Get the error messages for the defined validation rules.
     */
    public function messages(): array
    {
        return [
            'charts.*.count.required' => 'The count field is required for each chart.',
            'charts.*.count.integer' => 'The count must be an integer.',
            'charts.*.count.min' => 'The count must be at least 1.',
            'charts.*.device.required' => 'The device field is required for each chart.',
            'charts.*.registers.required' => 'The registers field is required for each chart.',
            'charts.*.registers.min' => 'At least one register is required for each chart.',
        ];
    }
}
