<?php

namespace App\Http\Requests\Syncing;

use App\Models\Batches;
use Carbon\Carbon;
use Illuminate\Foundation\Http\FormRequest;

class CullingSyncTransactionRequest 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
    {
        return [
            "cullings.*.sync_id" => ["required"],
            "cullings.*.batch_id" => ["required", "exists:batches,id"],
            "cullings.*.culling_date" => [
                "required",
                "date_format:m/d/Y h:ia",
                function ($attribute, $value, $fail) {

                    $batch = Batches::find($this->batch_id);

                    if (!$batch) {
                        return;
                    }

                    // Parse the input datetime
                    $cullingDate = Carbon::createFromFormat('m/d/Y h:ia', $value);

                    // Check if future date
                    if ($cullingDate->gt(now())) {
                        $fail("The culling date cannot be in the future. Update the date and time. Please try again.");
                    }

                    // Check if earlier than batch creation datetime
                    if ($cullingDate->lt($batch->created_at)) {
                        $fail("The culling date cannot be earlier than the batch creation date and time.");
                    }
                },
            ],
            "cullings.*.building_id" => ["required", "exists:buidling,id"],
            "cullings.*.undersized" => ["nullable", "integer", "min:0"],
            "cullings.*.weak" => ["nullable", "integer", "min:0"],
            "cullings.*.chick_infection" => ["nullable", "integer", "min:0"],
            "cullings.*.black_button" => ["nullable", "integer", "min:0"],
            "cullings.*.red_hocks" => ["nullable", "integer", "min:0"],
            "cullings.*.dehydrated" => ["nullable", "integer", "min:0"],
            "cullings.*.diarrhea" => ["nullable", "integer", "min:0"],
            "cullings.*.lameness" => ["nullable", "integer", "min:0"],
            "cullings.*.split" => ["nullable", "integer", "min:0"],
            "cullings.*.pvr" => ["nullable", "integer", "min:0"],
            "cullings.*.crd" => ["nullable", "integer", "min:0"],
            "cullings.*.rhinitis" => ["nullable", "integer", "min:0"],
            "cullings.*.tremors" => ["nullable", "integer", "min:0"],
            "cullings.*.nd" => ["nullable", "integer", "min:0"],
            "cullings.*.others" => ["nullable", "integer", "min:0"],
            "cullings.*.others_text" => ["nullable", "string", "min:0"],
            "cullings.*.culled_by_id" => ["required", "exists:users,id"],
            "cullings.*" => ["array"]
        ];
    }
}
