<?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
    {
        if (! Schema::hasTable('subscription_plans')) {
            Schema::create('subscription_plans', function (Blueprint $table) {
                $table->id();
                $table->string('public_id', 40)->unique();
                $table->foreignId('merchant_id')->constrained()->cascadeOnDelete();
                $table->string('environment', 16);
                $table->string('name');
                $table->text('description')->nullable();
                $table->bigInteger('price_amount_minor');
                $table->char('price_currency', 3);
                $table->string('pay_currency', 20);
                $table->string('interval', 20);
                $table->unsignedInteger('interval_count')->default(1);
                $table->string('status', 20)->default('active')->index();
                $table->json('metadata')->nullable();
                $table->timestamps();
                $table->index(['merchant_id', 'environment', 'status']);
            });
        }

        if (! Schema::hasTable('subscriptions')) {
            Schema::create('subscriptions', function (Blueprint $table) {
                $table->id();
                $table->string('public_id', 40)->unique();
                $table->foreignId('merchant_id')->constrained()->cascadeOnDelete();
                $table->foreignId('subscription_plan_id')->constrained('subscription_plans')->restrictOnDelete();
                $table->string('customer_name')->nullable();
                $table->string('customer_email');
                $table->string('status', 20)->default('active')->index();
                $table->timestamp('current_period_start')->nullable();
                $table->timestamp('current_period_end')->nullable();
                $table->timestamp('cancelled_at')->nullable();
                $table->json('metadata')->nullable();
                $table->timestamps();
                $table->index(['merchant_id', 'status']);
            });
        }

        if (! Schema::hasTable('payout_batches')) {
            Schema::create('payout_batches', function (Blueprint $table) {
                $table->id();
                $table->string('public_id', 40)->unique();
                $table->foreignId('merchant_id')->constrained()->cascadeOnDelete();
                $table->foreignId('asset_id')->constrained('assets')->restrictOnDelete();
                $table->string('environment', 16);
                $table->string('status', 30)->default('pending_approval')->index();
                $table->unsignedInteger('total_count')->default(0);
                $table->decimal('total_amount_atomic', 65, 0)->default(0);
                $table->unsignedBigInteger('approved_by')->nullable();
                $table->timestamp('approved_at')->nullable();
                $table->json('metadata')->nullable();
                $table->timestamps();
                $table->index(['merchant_id', 'environment', 'status']);
            });
        }

        if (! Schema::hasTable('payout_batch_items')) {
            Schema::create('payout_batch_items', function (Blueprint $table) {
                $table->id();
                $table->foreignId('payout_batch_id')->constrained('payout_batches')->cascadeOnDelete();
                $table->foreignId('payout_id')->nullable()->constrained('payouts')->nullOnDelete();
                $table->string('to_address');
                $table->decimal('amount_atomic', 65, 0);
                $table->string('status', 30)->default('pending')->index();
                $table->text('error')->nullable();
                $table->timestamps();
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('payout_batch_items');
        Schema::dropIfExists('payout_batches');
        Schema::dropIfExists('subscriptions');
        Schema::dropIfExists('subscription_plans');
    }
};
