<?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('compliance_rules', function (Blueprint $table) {
            $table->id();
            $table->foreignId('company_id')->nullable()->constrained()->onDelete('cascade');
            $table->string('rule_name');
            $table->string('rule_type');
            $table->text('description');
            $table->json('rule_config');
            $table->enum('severity', ['low', 'medium', 'high', 'critical'])->default('medium');
            $table->boolean('is_active')->default(true);
            $table->boolean('is_group_level')->default(false);
            $table->timestamps();
            
            $table->index(['company_id', 'is_active']);
        });

        Schema::create('compliance_alerts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('company_id')->constrained()->onDelete('cascade');
            $table->foreignId('compliance_rule_id')->nullable()->constrained()->onDelete('set null');
            $table->string('alert_type');
            $table->enum('severity', ['low', 'medium', 'high', 'critical'])->default('medium');
            $table->string('title');
            $table->text('message');
            $table->json('metadata')->nullable();
            $table->enum('status', ['active', 'acknowledged', 'resolved', 'dismissed'])->default('active');
            $table->date('triggered_date');
            $table->foreignId('acknowledged_by')->nullable()->constrained('users');
            $table->timestamp('acknowledged_at')->nullable();
            $table->foreignId('resolved_by')->nullable()->constrained('users');
            $table->timestamp('resolved_at')->nullable();
            $table->text('resolution_notes')->nullable();
            $table->timestamps();
            
            $table->index(['company_id', 'status', 'severity']);
            $table->index('triggered_date');
        });

        Schema::create('company_health_metrics', function (Blueprint $table) {
            $table->id();
            $table->foreignId('company_id')->constrained()->onDelete('cascade');
            $table->date('metric_date');
            $table->decimal('cash_balance', 15, 2);
            $table->decimal('monthly_revenue', 15, 2)->nullable();
            $table->decimal('monthly_expenses', 15, 2)->nullable();
            $table->decimal('profit_margin', 5, 2)->nullable();
            $table->integer('runway_days')->nullable();
            $table->decimal('debt_to_equity', 8, 4)->nullable();
            $table->integer('credit_score')->nullable();
            $table->decimal('liquidity_ratio', 5, 2)->nullable();
            $table->json('additional_metrics')->nullable();
            $table->timestamps();
            
            $table->unique(['company_id', 'metric_date']);
            $table->index('metric_date');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('company_health_metrics');
        Schema::dropIfExists('compliance_alerts');
        Schema::dropIfExists('compliance_rules');
    }
};
