<?php

namespace App\Http\Requests\Loading;

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

class LoadingRequest 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, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        $batchId = $this->route("loading");

        return [
            "loading_date" => ["required", "date"],
            "farm_id" => [
                "required",
                "integer",
                "exists:farms,id",
                function (string $attribute, mixed $value, Closure $fail) {
                    $isLeadMan = \App\Models\Farm::where("id", $value)
                        ->where("lead_man_id", auth()->id())
                        ->exists();

                    if (!$isLeadMan) {
                        $fail("Invalid action, you are not the lead man of this farm. Please contact the support");
                    }
                },
            ],
            "farm_name" => ["required", "string"],

            "building_id" => [
                "required",
                "integer",
                "exists:buildings,id",

                Rule::unique("batches", "building_id")
                    ->where(
                        fn($q) => $q
                            ->where("farm_id", $this->input("farm_id"))
                            ->where("status", "active")
                    )
                    ->ignore($batchId),
            ],
            "building_name" => ["required", "string"],
            "caretaker_id" => ["required", "integer", "exists:users,id"],

            "total_net" => ["required", "integer", "min:0"],
            "lines" => ["required", "array", "min:1"],
            "lines.*.breeder_id" => [
                "required",
                "integer",
                "exists:breeders,id",
            ],
            "lines.*.class_id" => ["required", "integer", "exists:classes,id"],
            "lines.*.initial_population" => ["required", "integer", "min:0"],
            "lines.*.reject" => ["required", "integer", "min:0"],
            "lines.*.doa" => ["required", "integer", "min:0"],
            "lines.*.net_population" => ["required", "integer", "min:0"],
            "lines.*.loaded_at" => ["required", "date_format:H:i"],
        ];
    }

    public function attribute(): array
    {
        return [
            "farm_id" => "farm id",
            "building_id" => "building id",
            "lines.*.breeder_id" => "breeder id",
            "lines.*.class_id" => "class id",
            "lines.*.initial_population" => "initial population",
            "lines.*.net_population" => "net population",
            "lines.*.loaded_at" => "loaded at",
        ];
    }

    public function messages(): array
    {
        return [
            "building_id.unique" =>
            "The selected building has an active batch. Please contact support for more details.",
        ];
    }
}
