<?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('mexco_event_licenses', function (Blueprint $table) {
            $table->id();
            $table->string('event_id');
            $table->foreignId('plan_id')->constrained('mexco_plans');
            $table->string('status', 32)->default('active');
            $table->unsignedInteger('price_cents')->default(0);
            $table->unsignedInteger('discount_cents')->default(0);
            $table->char('currency', 3)->default('MYR');
            $table->timestamp('starts_at')->nullable();
            $table->timestamp('ends_at')->nullable();
            $table->timestamp('activated_at')->nullable();
            $table->timestamp('suspended_at')->nullable();
            $table->boolean('is_campus_sponsored')->default(false);
            $table->unsignedInteger('sponsorship_value_cents')->nullable();
            $table->timestamp('retention_expires_at')->nullable();
            $table->json('retention_warned_at')->nullable();
            $table->json('metadata')->nullable();
            $table->timestamps();

            $table->foreign('event_id')->references('id')->on('meetup_events')->cascadeOnDelete();
            $table->unique('event_id');
            $table->index(['plan_id', 'status']);
            $table->index('ends_at');
            $table->index('retention_expires_at');
        });

        Schema::create('mexco_event_addons', function (Blueprint $table) {
            $table->id();
            $table->foreignId('event_license_id')->constrained('mexco_event_licenses')->cascadeOnDelete();
            $table->foreignId('addon_id')->constrained('mexco_addons');
            $table->unsignedInteger('price_cents')->default(0);
            $table->string('status', 32)->default('active');
            $table->timestamp('starts_at')->nullable();
            $table->timestamp('ends_at')->nullable();
            $table->timestamps();

            $table->unique(['event_license_id', 'addon_id']);
        });

        Schema::create('mexco_event_entitlements', function (Blueprint $table) {
            $table->id();
            $table->string('event_id');
            $table->string('feature_code', 64);
            $table->boolean('enabled')->default(false);
            $table->unsignedInteger('limit_value')->nullable();
            $table->string('source', 32)->default('plan');
            $table->unsignedBigInteger('source_id')->nullable();
            $table->timestamp('resolved_at')->nullable();
            $table->timestamps();

            $table->foreign('event_id')->references('id')->on('meetup_events')->cascadeOnDelete();
            $table->unique(['event_id', 'feature_code']);
            $table->index(['event_id', 'enabled']);
        });

        Schema::create('mexco_event_usage', function (Blueprint $table) {
            $table->id();
            $table->string('event_id');
            $table->unsignedInteger('participants_count')->default(0);
            $table->unsignedInteger('sessions_count')->default(0);
            $table->unsignedInteger('booths_count')->default(0);
            $table->unsignedInteger('certificates_issued_count')->default(0);
            $table->unsignedInteger('storage_used_mb')->default(0);
            $table->unsignedInteger('qr_scans_count')->default(0);
            $table->unsignedInteger('stamps_count')->default(0);
            $table->unsignedInteger('api_calls_count')->default(0);
            $table->timestamp('last_calculated_at')->nullable();
            $table->timestamps();

            $table->foreign('event_id')->references('id')->on('meetup_events')->cascadeOnDelete();
            $table->unique('event_id');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('mexco_event_usage');
        Schema::dropIfExists('mexco_event_entitlements');
        Schema::dropIfExists('mexco_event_addons');
        Schema::dropIfExists('mexco_event_licenses');
    }
};
