<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('permanent_employee_advances', function (Blueprint $table) {
            $table->id();
            $table->foreignId('created_by')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();
            $table->foreignId('permanent_employee_id')
                ->constrained('permanent_employees')
                ->cascadeOnDelete();
            $table->string('type', 16);
            $table->date('advance_date');
            $table->decimal('amount', 12, 2);
            $table->foreignId('factory_product_id')
                ->nullable()
                ->constrained('factory_products')
                ->nullOnDelete();
            $table->decimal('quantity', 12, 3)->nullable();
            $table->decimal('total_weight', 12, 3)->nullable();
            $table->decimal('weight_price', 12, 2)->nullable();
            $table->text('notes')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();

            $table->index(
                ['permanent_employee_id', 'advance_date'],
                'perm_emp_advances_emp_date_idx',
            );
            $table->index(['permanent_employee_id', 'is_active'], 'perm_emp_advances_emp_active_idx');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('permanent_employee_advances');
    }
};
