<?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('company_policy_acknowledgments', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('company_id')->nullable()->index();
            $table->unsignedBigInteger('company_policy_id')->index();
            $table->unsignedBigInteger('user_id')->index();
            $table->timestamp('acknowledged_at');
            $table->string('ip_address', 45)->nullable();
            $table->timestamps();
            $table->unique(['company_policy_id', 'user_id']);
        });

        Schema::create('saved_reports', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('company_id')->nullable()->index();
            $table->unsignedBigInteger('user_id')->index();
            $table->string('name', 191);
            $table->string('report_key', 80);
            $table->json('filters')->nullable();
            $table->string('schedule_frequency', 20)->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamp('last_run_at')->nullable();
            $table->timestamps();
        });

        Schema::create('user_sessions', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id')->index();
            $table->string('session_id', 128)->nullable()->index();
            $table->string('ip_address', 45)->nullable();
            $table->string('user_agent', 500)->nullable();
            $table->timestamp('last_activity_at')->nullable();
            $table->boolean('is_current')->default(false);
            $table->timestamps();
        });

        Schema::table('form_submissions', function (Blueprint $table) {
            if (! Schema::hasColumn('form_submissions', 'review_notes')) {
                $table->text('review_notes')->nullable()->after('status');
            }
            if (! Schema::hasColumn('form_submissions', 'reviewed_by')) {
                $table->unsignedBigInteger('reviewed_by')->nullable()->after('review_notes');
            }
            if (! Schema::hasColumn('form_submissions', 'reviewed_at')) {
                $table->timestamp('reviewed_at')->nullable()->after('reviewed_by');
            }
        });

        Schema::table('users', function (Blueprint $table) {
            if (! Schema::hasColumn('users', 'two_factor_method')) {
                $table->string('two_factor_method', 20)->default('email')->after('two_factor_enabled');
            }
        });
    }

    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            if (Schema::hasColumn('users', 'two_factor_method')) {
                $table->dropColumn('two_factor_method');
            }
        });

        Schema::table('form_submissions', function (Blueprint $table) {
            foreach (['review_notes', 'reviewed_by', 'reviewed_at'] as $column) {
                if (Schema::hasColumn('form_submissions', $column)) {
                    $table->dropColumn($column);
                }
            }
        });

        Schema::dropIfExists('user_sessions');
        Schema::dropIfExists('saved_reports');
        Schema::dropIfExists('company_policy_acknowledgments');
    }
};
