<?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
    {
        // Sale sources (dynamic)
        Schema::create('sale_sources', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        // Expense categories (dynamic)
        Schema::create('expense_categories', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        // Employee salary history
        Schema::create('employee_salary_histories', function (Blueprint $table) {
            $table->id();
            $table->foreignId('employee_id')->constrained('employees')->cascadeOnDelete();
            $table->decimal('old_salary', 15, 2);
            $table->decimal('new_salary', 15, 2);
            $table->string('salary_type')->nullable();
            $table->date('effective_date');
            $table->string('notes')->nullable();
            $table->timestamps();
        });

        // Roster: add duty_start_date and duty_end_date
        Schema::table('rosters', function (Blueprint $table) {
            $table->date('duty_start_date')->nullable()->after('duty_date');
            $table->date('duty_end_date')->nullable()->after('duty_start_date');
        });

        // Employee: add joining_date, resign_date
        Schema::table('employees', function (Blueprint $table) {
            $table->date('joining_date')->nullable()->after('hired_at');
            $table->date('resign_date')->nullable()->after('joining_date');
        });

        // Expense: add expense_category_id, drop currency_id column if it exists
        Schema::table('expenses', function (Blueprint $table) {
            $table->foreignId('expense_category_id')->nullable()->constrained('expense_categories')->nullOnDelete()->after('expense_date');
        });

        // Sale: add sale_source_id
        Schema::table('sales', function (Blueprint $table) {
            $table->foreignId('sale_source_id')->nullable()->constrained('sale_sources')->nullOnDelete()->after('customer_id');
        });
    }

    public function down(): void
    {
        Schema::table('sales', function (Blueprint $table) {
            $table->dropForeignIdFor(\App\Models\SaleSource::class);
            $table->dropColumn('sale_source_id');
        });
        Schema::table('expenses', function (Blueprint $table) {
            $table->dropForeignIdFor(\App\Models\ExpenseCategory::class);
            $table->dropColumn('expense_category_id');
        });
        Schema::table('employees', function (Blueprint $table) {
            $table->dropColumn(['joining_date', 'resign_date']);
        });
        Schema::table('rosters', function (Blueprint $table) {
            $table->dropColumn(['duty_start_date', 'duty_end_date']);
        });
        Schema::dropIfExists('employee_salary_histories');
        Schema::dropIfExists('expense_categories');
        Schema::dropIfExists('sale_sources');
    }
};
