<?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('gts_providers')) {
            Schema::create('gts_providers', function (Blueprint $table): void {
                $table->id();
                $table->string('name');
                $table->string('api_url');
                $table->text('api_key');
                $table->string('status')->default('active');
                $table->decimal('balance', 12, 2)->default(0);
                $table->timestamp('last_synced_at')->nullable();
                $table->timestamps();
            });
        }

        if (Schema::hasTable('gts_services')) {
            Schema::table('gts_services', function (Blueprint $table): void {
                if (! Schema::hasColumn('gts_services', 'gts_provider_id')) {
                    $table->foreignId('gts_provider_id')->nullable()->after('id')->constrained('gts_providers')->nullOnDelete();
                }
                if (! Schema::hasColumn('gts_services', 'provider_service_id')) {
                    $table->string('provider_service_id')->nullable()->after('gts_provider_id');
                }
                if (! Schema::hasColumn('gts_services', 'provider_rate_per_1000')) {
                    $table->decimal('provider_rate_per_1000', 10, 4)->default(0)->after('rate_per_1000');
                }
                if (! Schema::hasColumn('gts_services', 'profit_percent')) {
                    $table->decimal('profit_percent', 6, 2)->default(0)->after('provider_rate_per_1000');
                }
                if (! Schema::hasColumn('gts_services', 'auto_submit')) {
                    $table->boolean('auto_submit')->default(false)->after('profit_percent');
                }
                if (! Schema::hasColumn('gts_services', 'refill_enabled')) {
                    $table->boolean('refill_enabled')->default(false)->after('auto_submit');
                }
                if (! Schema::hasColumn('gts_services', 'dripfeed_enabled')) {
                    $table->boolean('dripfeed_enabled')->default(false)->after('refill_enabled');
                }
            });
        }

        if (Schema::hasTable('gts_orders')) {
            Schema::table('gts_orders', function (Blueprint $table): void {
                if (! Schema::hasColumn('gts_orders', 'provider_order_id')) {
                    $table->string('provider_order_id')->nullable()->after('order_number');
                }
                if (! Schema::hasColumn('gts_orders', 'provider_status')) {
                    $table->string('provider_status')->nullable()->after('status');
                }
                if (! Schema::hasColumn('gts_orders', 'start_count')) {
                    $table->unsignedInteger('start_count')->nullable()->after('provider_status');
                }
                if (! Schema::hasColumn('gts_orders', 'remains')) {
                    $table->unsignedInteger('remains')->nullable()->after('start_count');
                }
                if (! Schema::hasColumn('gts_orders', 'provider_response')) {
                    $table->json('provider_response')->nullable()->after('remains');
                }
                if (! Schema::hasColumn('gts_orders', 'api_submitted_at')) {
                    $table->timestamp('api_submitted_at')->nullable()->after('provider_response');
                }
                if (! Schema::hasColumn('gts_orders', 'api_synced_at')) {
                    $table->timestamp('api_synced_at')->nullable()->after('api_submitted_at');
                }
            });
        }
    }

    public function down(): void
    {
        Schema::table('gts_orders', function (Blueprint $table): void {
            foreach (['provider_order_id', 'provider_status', 'start_count', 'remains', 'provider_response', 'api_submitted_at', 'api_synced_at'] as $column) {
                if (Schema::hasColumn('gts_orders', $column)) {
                    $table->dropColumn($column);
                }
            }
        });

        Schema::table('gts_services', function (Blueprint $table): void {
            if (Schema::hasColumn('gts_services', 'gts_provider_id')) {
                $table->dropConstrainedForeignId('gts_provider_id');
            }
            foreach (['provider_service_id', 'provider_rate_per_1000', 'profit_percent', 'auto_submit', 'refill_enabled', 'dripfeed_enabled'] as $column) {
                if (Schema::hasColumn('gts_services', $column)) {
                    $table->dropColumn($column);
                }
            }
        });

        Schema::dropIfExists('gts_providers');
    }
};
