<?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
    {
        // Add revenue to companies
        if (!Schema::hasColumn('companies', 'revenue')) {
            Schema::table('companies', function (Blueprint $table) {
                $table->decimal('revenue', 20, 2)->default(0)->after('total_liabilities');
                $table->decimal('expenses_manual', 20, 2)->default(0)->after('revenue');
            });
        }

        // Employee leave records
        if (!Schema::hasTable('employee_leaves')) {
            Schema::create('employee_leaves', function (Blueprint $table) {
                $table->id();
                $table->foreignId('employee_id')->constrained()->cascadeOnDelete();
                $table->string('type', 50)->default('annual'); // annual, sick, maternity, paternity, unpaid, other
                $table->date('start_date');
                $table->date('end_date');
                $table->integer('days')->default(1);
                $table->text('reason')->nullable();
                $table->string('status', 30)->default('pending'); // pending, approved, rejected
                $table->unsignedBigInteger('approved_by')->nullable();
                $table->text('notes')->nullable();
                $table->timestamps();
            });
        }

        // Employee salary history
        if (!Schema::hasTable('employee_salary_history')) {
            Schema::create('employee_salary_history', function (Blueprint $table) {
                $table->id();
                $table->foreignId('employee_id')->constrained()->cascadeOnDelete();
                $table->string('type', 30)->default('raise'); // raise, reduction, fine, bonus, adjustment
                $table->decimal('old_salary', 15, 2)->default(0);
                $table->decimal('new_salary', 15, 2)->default(0);
                $table->decimal('amount_change', 15, 2)->default(0);
                $table->date('effective_date');
                $table->text('reason')->nullable();
                $table->unsignedBigInteger('recorded_by')->nullable();
                $table->timestamps();
            });
        }

        // Employee events: awards, reviews, fines, dept changes, status changes
        if (!Schema::hasTable('employee_events')) {
            Schema::create('employee_events', function (Blueprint $table) {
                $table->id();
                $table->foreignId('employee_id')->constrained()->cascadeOnDelete();
                $table->string('type', 50); // award, review, fine, department_change, status_change, note
                $table->string('title', 255);
                $table->text('description')->nullable();
                $table->decimal('amount', 15, 2)->nullable(); // for fines/bonuses
                $table->date('event_date');
                $table->string('old_value', 255)->nullable(); // for changes
                $table->string('new_value', 255)->nullable();
                $table->unsignedBigInteger('recorded_by')->nullable();
                $table->timestamps();
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('employee_events');
        Schema::dropIfExists('employee_salary_history');
        Schema::dropIfExists('employee_leaves');

        if (Schema::hasColumn('companies', 'revenue')) {
            Schema::table('companies', function (Blueprint $table) {
                $table->dropColumn(['revenue', 'expenses_manual']);
            });
        }
    }
};
