<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create("culling", function (Blueprint $table) {
            $table->increments("id");
            $table->string("form_no")->index();
            $table->integer("batch_id");

            $table->date("culling_date")->index();
            $table->integer("age_in_days");
            $table->integer("population_before_cull");

            $table->string("classification")->nullable();
            $table->integer("undersized")->default(0);
            $table->integer("weak")->default(0);
            $table->integer("chick_infection")->default(0);
            $table->integer("black_button")->default(0);
            $table->integer("red_hocks")->default(0);
            $table->integer("dehydrated")->default(0);
            $table->integer("diarrhea")->default(0);
            $table->integer("lameness")->default(0);
            $table->integer("split")->default(0);
            $table->integer("pvr")->default(0);
            $table->integer("crd")->default(0);
            $table->integer("rhinitis")->default(0);
            $table->integer("tremors")->default(0);
            $table->integer("nd")->default(0);
            $table->string("others_text")->nullable();
            $table->integer("others")->default(0);

            $table
                ->string("synced_status")
                ->default("unsynced")
                ->index();
            $table->timestamp("synced_at")->nullable();
            $table->integer("synced_id")->nullable();

            $table->integer("culled_by_id")->index();
            $table->string("culled_by_name");
            $table->integer("caretaker_id")->index();
            $table->string("caretaker_name");

            $table
                ->integer("total_culled")
                ->storedAs(
                    "undersized + weak + chick_infection + black_button + red_hocks + dehydrated + diarrhea + lameness + split + pvr + crd + rhinitis + tremors + nd + others"
                );
            $table->text("remarks")->nullable();
            $table->longText("attachment_filename")->nullable();
            $table->longText("attachment_path")->nullable();
            $table->longText("signature_name")->nullable();
            $table->longText("signature_path")->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->index(["batch_id", "synced_status"]);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists("culling");
    }
};
