<?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('channel_accounts', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->uuid('tenant_id');
            $table->string('type');
            $table->string('external_account_id')->nullable();
            $table->string('external_business_id')->nullable();
            $table->string('display_name');
            $table->string('status')->default('disconnected');
            $table->text('access_token_encrypted')->nullable();
            $table->text('refresh_token_encrypted')->nullable();
            $table->timestamp('token_expires_at')->nullable();
            $table->json('metadata_json')->nullable();
            $table->timestamp('last_sync_at')->nullable();
            $table->timestamps();

            $table->foreign('tenant_id')->references('id')->on('tenants')->cascadeOnDelete();
            $table->index(['tenant_id', 'status']);
            $table->index(['tenant_id', 'type']);
        });

        Schema::create('webhook_events', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->uuid('tenant_id')->nullable();
            $table->uuid('channel_account_id')->nullable();
            $table->string('source');
            $table->string('event_type');
            $table->string('external_event_id')->nullable();
            $table->json('payload_json');
            $table->string('processing_status')->default('pending');
            $table->text('error_message')->nullable();
            $table->unsignedInteger('retries_count')->default(0);
            $table->timestamp('processed_at')->nullable();
            $table->timestamp('created_at')->useCurrent();

            $table->foreign('tenant_id')->references('id')->on('tenants')->nullOnDelete();
            $table->foreign('channel_account_id')->references('id')->on('channel_accounts')->nullOnDelete();
            $table->index(['processing_status', 'created_at']);
            $table->index(['tenant_id', 'created_at']);
        });

        Schema::create('payment_providers', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->uuid('tenant_id');
            $table->string('provider_key');
            $table->string('display_name');
            $table->boolean('is_enabled')->default(false);
            $table->boolean('is_live_mode')->default(false);
            $table->json('credentials_json')->nullable();
            $table->string('webhook_secret')->nullable();
            $table->string('checkout_base_url')->nullable();
            $table->timestamps();

            $table->foreign('tenant_id')->references('id')->on('tenants')->cascadeOnDelete();
            $table->unique(['tenant_id', 'provider_key']);
        });

        Schema::create('payment_requests', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->uuid('tenant_id');
            $table->uuid('order_id');
            $table->uuid('payment_provider_id');
            $table->string('status')->default('draft');
            $table->decimal('amount', 10, 2);
            $table->string('currency_code', 3)->default('USD');
            $table->string('external_reference')->nullable();
            $table->string('checkout_url')->nullable();
            $table->timestamp('expires_at')->nullable();
            $table->unsignedBigInteger('created_by');
            $table->timestamps();

            $table->foreign('tenant_id')->references('id')->on('tenants')->cascadeOnDelete();
            $table->foreign('order_id')->references('id')->on('orders')->cascadeOnDelete();
            $table->foreign('payment_provider_id')->references('id')->on('payment_providers')->cascadeOnDelete();
            $table->foreign('created_by')->references('id')->on('users')->cascadeOnDelete();
            $table->index(['tenant_id', 'status']);
            $table->index(['tenant_id', 'order_id']);
        });

        Schema::create('payment_transactions', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->uuid('tenant_id');
            $table->uuid('payment_request_id');
            $table->string('status')->default('pending');
            $table->string('provider_reference')->nullable();
            $table->decimal('amount', 10, 2);
            $table->string('currency_code', 3)->default('USD');
            $table->json('payload_json')->nullable();
            $table->timestamp('processed_at')->nullable();
            $table->timestamps();

            $table->foreign('tenant_id')->references('id')->on('tenants')->cascadeOnDelete();
            $table->foreign('payment_request_id')->references('id')->on('payment_requests')->cascadeOnDelete();
            $table->index(['tenant_id', 'status']);
        });

        Schema::create('payment_webhook_events', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->uuid('tenant_id')->nullable();
            $table->uuid('payment_provider_id')->nullable();
            $table->string('provider_key');
            $table->string('external_event_id')->nullable();
            $table->json('payload_json');
            $table->string('processing_status')->default('pending');
            $table->text('error_message')->nullable();
            $table->timestamp('processed_at')->nullable();
            $table->timestamp('created_at')->useCurrent();

            $table->foreign('tenant_id')->references('id')->on('tenants')->nullOnDelete();
            $table->foreign('payment_provider_id')->references('id')->on('payment_providers')->nullOnDelete();
            $table->index(['provider_key', 'created_at']);
            $table->index(['processing_status', 'created_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('payment_webhook_events');
        Schema::dropIfExists('payment_transactions');
        Schema::dropIfExists('payment_requests');
        Schema::dropIfExists('payment_providers');
        Schema::dropIfExists('webhook_events');
        Schema::dropIfExists('channel_accounts');
    }
};
