<?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('providers', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('code')->unique();
            $table->string('type')->default('generic');
            $table->string('mode')->default('mock')->index();
            $table->string('base_url')->nullable();
            $table->string('auth_header')->default('X-Api-Key');
            $table->string('api_key_env')->nullable();
            $table->text('encrypted_api_key')->nullable();
            $table->boolean('is_active')->default(true)->index();
            $table->unsignedInteger('timeout_seconds')->default(20);
            $table->unsignedInteger('retry_attempts')->default(1);
            $table->string('health_status')->default('unknown')->index();
            $table->timestamp('last_health_at')->nullable();
            $table->json('settings')->nullable();
            $table->timestamps();
        });

        Schema::create('provider_product_mappings', function (Blueprint $table) {
            $table->id();
            $table->foreignId('provider_id')->constrained()->cascadeOnDelete();
            $table->foreignId('product_variant_id')->constrained()->cascadeOnDelete();
            $table->string('provider_sku')->nullable();
            $table->string('fulfillment_type')->default('pubg_uc_code')->index();
            $table->string('mode')->default('single');
            $table->unsignedInteger('codes_per_order')->default(1);
            $table->json('required_fields')->nullable();
            $table->json('settings')->nullable();
            $table->boolean('is_active')->default(true)->index();
            $table->timestamps();
            $table->unique(['provider_id', 'product_variant_id']);
        });

        Schema::create('provider_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('provider_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('order_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('order_item_id')->nullable()->constrained()->nullOnDelete();
            $table->string('endpoint');
            $table->string('method')->default('POST');
            $table->json('request_payload')->nullable();
            $table->json('response_payload')->nullable();
            $table->unsignedInteger('status_code')->nullable();
            $table->string('status')->default('pending')->index();
            $table->unsignedInteger('latency_ms')->nullable();
            $table->text('error_message')->nullable();
            $table->timestamps();
        });

        Schema::table('stock_codes', function (Blueprint $table) {
            $table->foreignId('provider_id')->nullable()->after('product_variant_id')->constrained()->nullOnDelete();
            $table->json('metadata')->nullable()->after('currency_code');
            $table->timestamp('checked_at')->nullable()->after('metadata');
            $table->string('check_status')->nullable()->after('checked_at')->index();
        });
    }

    public function down(): void
    {
        Schema::table('stock_codes', function (Blueprint $table) {
            $table->dropConstrainedForeignId('provider_id');
            $table->dropColumn(['metadata', 'checked_at', 'check_status']);
        });

        Schema::dropIfExists('provider_logs');
        Schema::dropIfExists('provider_product_mappings');
        Schema::dropIfExists('providers');
    }
};
