<?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::table('offboardings', function (Blueprint $table) {
            $table->string('status', 30)->default('in_progress')->after('type');
            $table->date('last_working_date')->nullable()->after('offboarding_date');
            $table->unsignedSmallInteger('notice_period_days')->default(30)->after('last_working_date');
        });

        Schema::create('fnf_settlements', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('company_id')->index();
            $table->unsignedBigInteger('user_id')->index();
            $table->unsignedBigInteger('offboarding_id')->nullable()->index();
            $table->date('last_working_date')->nullable();
            $table->unsignedSmallInteger('notice_period_days')->default(30);
            $table->unsignedSmallInteger('notice_served_days')->default(0);
            $table->decimal('daily_rate', 14, 2)->default(0);
            $table->decimal('notice_pay_amount', 14, 2)->default(0);
            $table->decimal('notice_recovery_amount', 14, 2)->default(0);
            $table->decimal('leave_encashment_days', 10, 2)->default(0);
            $table->decimal('leave_encashment_amount', 14, 2)->default(0);
            $table->decimal('salary_dues_amount', 14, 2)->default(0);
            $table->decimal('bonus_gratuity_amount', 14, 2)->default(0);
            $table->decimal('other_earnings', 14, 2)->default(0);
            $table->decimal('loan_recovery', 14, 2)->default(0);
            $table->decimal('asset_recovery', 14, 2)->default(0);
            $table->decimal('other_recovery', 14, 2)->default(0);
            $table->decimal('total_earnings', 14, 2)->default(0);
            $table->decimal('total_recovery', 14, 2)->default(0);
            $table->decimal('net_payable', 14, 2)->default(0);
            $table->json('breakdown_json')->nullable();
            $table->string('status', 30)->default('draft');
            $table->timestamp('approved_at')->nullable();
            $table->unsignedBigInteger('approved_by')->nullable();
            $table->timestamp('paid_at')->nullable();
            $table->unsignedBigInteger('paid_by')->nullable();
            $table->text('notes')->nullable();
            $table->timestamps();

            $table->unique(['company_id', 'user_id', 'offboarding_id'], 'fnf_settlement_unique');
        });

        Schema::create('offboarding_checklist_items', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('company_id')->index();
            $table->unsignedBigInteger('offboarding_id')->index();
            $table->string('category', 30);
            $table->string('task_name', 191);
            $table->text('description')->nullable();
            $table->string('status', 30)->default('pending');
            $table->unsignedBigInteger('assigned_to')->nullable();
            $table->timestamp('completed_at')->nullable();
            $table->unsignedBigInteger('completed_by')->nullable();
            $table->text('notes')->nullable();
            $table->unsignedSmallInteger('display_order')->default(0);
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('offboarding_checklist_items');
        Schema::dropIfExists('fnf_settlements');
        Schema::table('offboardings', function (Blueprint $table) {
            $table->dropColumn(['status', 'last_working_date', 'notice_period_days']);
        });
    }
};
