<?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('pw_promo', function (Blueprint $table) {
            $table->id();
            $table->string('code')->unique();
            $table->string('name');
            $table->text('description')->nullable();
            $table->enum('discount_type', ['percentage', 'fixed', 'bonus_gold'])->default('percentage');
            $table->decimal('discount_value', 10, 2);
            $table->integer('min_amount')->default(0);
            $table->integer('max_discount')->nullable();
            $table->integer('usage_limit')->nullable();
            $table->integer('usage_count')->default(0);
            $table->integer('per_user_limit')->default(1);
            $table->boolean('is_active')->default(true);
            $table->timestamp('start_date')->nullable();
            $table->timestamp('end_date')->nullable();
            $table->timestamps();
        });

        Schema::create('pw_promo_usage', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('promo_id');
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('transaction_id')->nullable();
            $table->integer('discount_amount');
            $table->timestamps();

            $table->foreign('promo_id')->references('id')->on('pw_promo')->onDelete('cascade');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('pw_promo_usage');
        Schema::dropIfExists('pw_promo');
    }
};
