<?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("chick_loading_lines", function (Blueprint $table) {
            $table->id();

            $table->integer("synced_id")->nullable();
            $table->unsignedBigInteger("header_id")->index();
            $table
                ->foreign("header_id")
                ->references("id")
                ->on("chick_loading_headers");

            $table->integer("breeder_id")->index();
            $table->string("breeder_name");
            $table->integer("class_id")->index();
            $table->string("class_name");

            $table->integer("initial_population");
            $table->integer("reject")->default(0);
            $table->integer("doa")->default(0);
            $table
                ->integer("net_population")
                ->storedAs("initial_population - reject - doa");
            $table->time("loaded_at")->nullable();
            $table->text("notes")->nullable();

            $table->timestamps();
            $table->softDeletes();

            // Allow same breeder/class multiple times per day (different batches)
            // If you want to prevent duplicates, uncomment this:
            // $table->unique(["header_id", "breeder_id", "class_id"], "unique_line");
        });
    }

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