<?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('audit_logs', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('company_id')->nullable()->index();
            $table->unsignedBigInteger('actor_user_id')->nullable()->index();
            $table->string('action', 100)->index();
            $table->string('subject_type', 191)->nullable();
            $table->unsignedBigInteger('subject_id')->nullable();
            $table->json('properties')->nullable();
            $table->string('ip_address', 45)->nullable();
            $table->text('user_agent')->nullable();
            $table->timestamp('created_at')->useCurrent();

            $table->index(['subject_type', 'subject_id']);
        });

        Schema::create('performance_cycles', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('company_id')->index();
            $table->string('name');
            $table->date('start_date');
            $table->date('end_date');
            $table->string('status', 30)->default('draft')->index();
            $table->text('description')->nullable();
            $table->unsignedBigInteger('created_by')->nullable();
            $table->timestamp('launched_at')->nullable();
            $table->timestamp('closed_at')->nullable();
            $table->timestamps();
        });

        if (Schema::hasTable('performance_reviews') && ! Schema::hasColumn('performance_reviews', 'performance_cycle_id')) {
            Schema::table('performance_reviews', function (Blueprint $table) {
                $table->unsignedBigInteger('performance_cycle_id')->nullable()->after('company_id')->index();
            });
        }

        if (Schema::hasTable('goals') && ! Schema::hasColumn('goals', 'performance_cycle_id')) {
            Schema::table('goals', function (Blueprint $table) {
                $table->unsignedBigInteger('performance_cycle_id')->nullable()->after('company_id')->index();
            });
        }

        Schema::table('users', function (Blueprint $table) {
            if (! Schema::hasColumn('users', 'two_factor_enabled')) {
                $table->boolean('two_factor_enabled')->default(false)->after('remember_token');
            }
            if (! Schema::hasColumn('users', 'two_factor_secret')) {
                $table->text('two_factor_secret')->nullable()->after('two_factor_enabled');
            }
        });
    }

    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            foreach (['two_factor_enabled', 'two_factor_secret'] as $column) {
                if (Schema::hasColumn('users', $column)) {
                    $table->dropColumn($column);
                }
            }
        });

        if (Schema::hasTable('performance_reviews') && Schema::hasColumn('performance_reviews', 'performance_cycle_id')) {
            Schema::table('performance_reviews', function (Blueprint $table) {
                $table->dropColumn('performance_cycle_id');
            });
        }

        if (Schema::hasTable('goals') && Schema::hasColumn('goals', 'performance_cycle_id')) {
            Schema::table('goals', function (Blueprint $table) {
                $table->dropColumn('performance_cycle_id');
            });
        }

        Schema::dropIfExists('performance_cycles');
        Schema::dropIfExists('audit_logs');
    }
};
