<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('promotions', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('academy_id')->constrained('academies');
            $table->string('name_ar');
            $table->string('name')->nullable();
            $table->text('description_ar')->nullable();
            $table->string('code', 50)->nullable();
            $table->string('type', 20);
            $table->string('adjustment_type', 30);
            $table->bigInteger('adjustment_value');
            $table->jsonb('conditions')->default('{}');
            $table->string('target_type')->nullable();
            $table->integer('target_id')->nullable();
            $table->foreignId('branch_id')->nullable()->constrained('branches');
            $table->bigInteger('min_purchase_amount')->nullable();
            $table->bigInteger('max_discount_amount')->nullable();
            $table->date('start_date');
            $table->date('end_date')->nullable();
            $table->boolean('is_active')->default(true);
            $table->integer('usage_limit')->nullable();
            $table->integer('usage_count')->default(0);
            $table->integer('per_participant_limit')->nullable();
            $table->integer('priority')->default(0);
            $table->foreignId('created_by')->constrained('users');
            $table->jsonb('metadata')->default('{}');
            $table->timestamps();
            $table->softDeletes();

            $table->index(['academy_id', 'type', 'is_active']);
            $table->index('code');
            $table->index(['start_date', 'end_date']);
        });

        // Partial unique index: code must be unique per academy where code is not null
        DB::statement('CREATE UNIQUE INDEX promotions_academy_id_code_unique ON promotions (academy_id, code) WHERE code IS NOT NULL');

        // CHECK constraints
        DB::statement("ALTER TABLE promotions ADD CONSTRAINT promotions_type_check CHECK (type IN ('automatic', 'coupon', 'campaign'))");
        DB::statement("ALTER TABLE promotions ADD CONSTRAINT promotions_adjustment_type_check CHECK (adjustment_type IN ('percentage_discount', 'fixed_discount', 'fixed_price', 'percentage_increase'))");

        Schema::create('promotion_usages', function (Blueprint $table) {
            $table->id();
            $table->foreignId('academy_id')->constrained('academies');
            $table->foreignId('promotion_id')->constrained('promotions');
            $table->foreignId('participant_id')->nullable()->constrained('participants');
            $table->foreignId('invoice_id')->nullable()->constrained('invoices');
            $table->bigInteger('discount_amount');
            $table->timestamp('used_at');
            $table->jsonb('metadata')->default('{}');
            $table->timestamps();

            $table->index(['promotion_id', 'participant_id']);
            $table->index('invoice_id');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('promotion_usages');
        Schema::dropIfExists('promotions');
    }
};
