<?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::table('users', function (Blueprint $table): void {
            if (! Schema::hasColumn('users', 'business_name')) {
                $table->string('business_name')->nullable();
            }
            if (! Schema::hasColumn('users', 'client_type')) {
                $table->string('client_type')->default('plan');
            }
            if (! Schema::hasColumn('users', 'assigned_designer_id')) {
                $table->foreignId('assigned_designer_id')->nullable()->references('id')->on('users')->nullOnDelete();
            }
            if (! Schema::hasColumn('users', 'notes')) {
                $table->text('notes')->nullable();
            }
            if (! Schema::hasColumn('users', 'profile_image')) {
                $table->string('profile_image')->nullable();
            }
            if (! Schema::hasColumn('users', 'bio')) {
                $table->text('bio')->nullable();
            }
        });

        if (! Schema::hasTable('payment_methods')) {
            Schema::create('payment_methods', function (Blueprint $table): void {
                $table->id();
                $table->string('name');
                $table->string('method_name')->nullable();
                $table->string('account_title')->nullable();
                $table->string('account_number')->nullable();
                $table->string('bank_name')->nullable();
                $table->text('instructions')->nullable();
                $table->string('qr_code')->nullable();
                $table->string('status')->default('active');
                $table->timestamps();
            });
        }

        if (! Schema::hasTable('notifications')) {
            Schema::create('notifications', function (Blueprint $table): void {
                $table->id();
                $table->string('title');
                $table->text('body');
                $table->string('audience')->default('admin');
                $table->string('status')->default('unread');
                $table->timestamps();
            });
        }

        if (! Schema::hasTable('about_sections')) {
            Schema::create('about_sections', function (Blueprint $table): void {
                $table->id();
                $table->string('section_key')->unique();
                $table->string('title');
                $table->string('subtitle')->nullable();
                $table->text('body')->nullable();
                $table->string('icon')->nullable();
                $table->unsignedInteger('sort_order')->default(0);
                $table->string('status')->default('active');
                $table->timestamps();
            });
        }
        Schema::table('about_sections', function (Blueprint $table): void {
            foreach ([
                'section_key' => fn () => $table->string('section_key')->nullable(),
                'title' => fn () => $table->string('title')->nullable(),
                'subtitle' => fn () => $table->string('subtitle')->nullable(),
                'body' => fn () => $table->text('body')->nullable(),
                'icon' => fn () => $table->string('icon')->nullable(),
                'sort_order' => fn () => $table->unsignedInteger('sort_order')->default(0),
                'status' => fn () => $table->string('status')->default('active'),
            ] as $column => $addColumn) {
                if (! Schema::hasColumn('about_sections', $column)) {
                    $addColumn();
                }
            }
        });

        if (! Schema::hasTable('team_members')) {
            Schema::create('team_members', function (Blueprint $table): void {
                $table->id();
                $table->string('name');
                $table->string('role_title')->nullable();
                $table->string('image_path')->nullable();
                $table->text('bio')->nullable();
                $table->json('skills')->nullable();
                $table->text('education')->nullable();
                $table->text('experience')->nullable();
                $table->string('portfolio_url')->nullable();
                $table->unsignedInteger('sort_order')->default(0);
                $table->string('status')->default('active');
                $table->timestamps();
            });
        }
        Schema::table('team_members', function (Blueprint $table): void {
            foreach ([
                'name' => fn () => $table->string('name')->nullable(),
                'role_title' => fn () => $table->string('role_title')->nullable(),
                'image_path' => fn () => $table->string('image_path')->nullable(),
                'bio' => fn () => $table->text('bio')->nullable(),
                'skills' => fn () => $table->json('skills')->nullable(),
                'education' => fn () => $table->text('education')->nullable(),
                'experience' => fn () => $table->text('experience')->nullable(),
                'portfolio_url' => fn () => $table->string('portfolio_url')->nullable(),
                'sort_order' => fn () => $table->unsignedInteger('sort_order')->default(0),
                'status' => fn () => $table->string('status')->default('active'),
            ] as $column => $addColumn) {
                if (! Schema::hasColumn('team_members', $column)) {
                    $addColumn();
                }
            }
        });

        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')->nullable();
                $table->decimal('balance', 12, 4)->default(0);
                $table->string('status')->default('active');
                $table->timestamp('last_synced_at')->nullable();
                $table->timestamps();
            });
        }
        Schema::table('gts_providers', function (Blueprint $table): void {
            if (! Schema::hasColumn('gts_providers', 'balance')) {
                $table->decimal('balance', 12, 4)->default(0);
            }
        });

        if (! Schema::hasTable('gts_services')) {
            Schema::create('gts_services', function (Blueprint $table): void {
                $table->id();
                $table->foreignId('gts_provider_id')->nullable()->constrained('gts_providers')->nullOnDelete();
                $table->string('provider_service_id')->nullable();
                $table->string('name');
                $table->string('category')->nullable();
                $table->integer('min_quantity')->default(1);
                $table->integer('max_quantity')->default(10000);
                $table->decimal('rate_per_1000', 12, 4)->default(0);
                $table->decimal('provider_rate_per_1000', 12, 4)->default(0);
                $table->decimal('profit_percent', 8, 2)->default(20);
                $table->boolean('auto_submit')->default(false);
                $table->boolean('refill_enabled')->default(false);
                $table->boolean('dripfeed_enabled')->default(false);
                $table->text('description')->nullable();
                $table->string('status')->default('active');
                $table->timestamps();
            });
        }
        Schema::table('gts_services', function (Blueprint $table): void {
            foreach ([
                'min_quantity' => fn () => $table->integer('min_quantity')->default(1),
                'max_quantity' => fn () => $table->integer('max_quantity')->default(10000),
                'rate_per_1000' => fn () => $table->decimal('rate_per_1000', 12, 4)->default(0),
                'provider_rate_per_1000' => fn () => $table->decimal('provider_rate_per_1000', 12, 4)->default(0),
                'profit_percent' => fn () => $table->decimal('profit_percent', 8, 2)->default(20),
                'auto_submit' => fn () => $table->boolean('auto_submit')->default(false),
                'refill_enabled' => fn () => $table->boolean('refill_enabled')->default(false),
                'dripfeed_enabled' => fn () => $table->boolean('dripfeed_enabled')->default(false),
            ] as $column => $addColumn) {
                if (! Schema::hasColumn('gts_services', $column)) {
                    $addColumn();
                }
            }
        });

        if (! Schema::hasTable('gts_wallets')) {
            Schema::create('gts_wallets', function (Blueprint $table): void {
                $table->id();
                $table->foreignId('user_id')->constrained()->cascadeOnDelete();
                $table->decimal('balance', 12, 2)->default(0);
                $table->string('currency')->default('PKR');
                $table->string('status')->default('active');
                $table->timestamps();
            });
        }
        Schema::table('gts_wallets', function (Blueprint $table): void {
            if (! Schema::hasColumn('gts_wallets', 'status')) {
                $table->string('status')->default('active');
            }
        });

        if (! Schema::hasTable('gts_wallet_transactions')) {
            Schema::create('gts_wallet_transactions', function (Blueprint $table): void {
                $table->id();
                $table->foreignId('gts_wallet_id')->constrained('gts_wallets')->cascadeOnDelete();
                $table->foreignId('payment_method_id')->nullable()->constrained()->nullOnDelete();
                $table->string('type')->default('credit');
                $table->decimal('amount', 12, 2)->default(0);
                $table->string('reference')->nullable();
                $table->string('sender_account')->nullable();
                $table->string('proof_path')->nullable();
                $table->text('note')->nullable();
                $table->string('status')->default('pending');
                $table->timestamp('applied_at')->nullable();
                $table->timestamps();
            });
        }

        if (! Schema::hasTable('gts_orders')) {
            Schema::create('gts_orders', function (Blueprint $table): void {
                $table->id();
                $table->foreignId('user_id')->constrained()->cascadeOnDelete();
                $table->foreignId('gts_service_id')->nullable()->constrained('gts_services')->nullOnDelete();
                $table->string('order_number')->nullable();
                $table->string('provider_order_id')->nullable();
                $table->string('target_link');
                $table->integer('quantity')->default(0);
                $table->decimal('charge', 12, 2)->default(0);
                $table->integer('progress')->default(0);
                $table->integer('start_count')->nullable();
                $table->integer('remains')->nullable();
                $table->text('notes')->nullable();
                $table->string('status')->default('queued');
                $table->string('provider_status')->nullable();
                $table->json('provider_response')->nullable();
                $table->timestamp('api_submitted_at')->nullable();
                $table->timestamp('api_synced_at')->nullable();
                $table->timestamps();
            });
        }
        Schema::table('gts_orders', function (Blueprint $table): void {
            foreach ([
                'order_number' => fn () => $table->string('order_number')->nullable(),
                'target_link' => fn () => $table->string('target_link')->nullable(),
                'progress' => fn () => $table->integer('progress')->default(0),
                'notes' => fn () => $table->text('notes')->nullable(),
                'provider_status' => fn () => $table->string('provider_status')->nullable(),
                'provider_response' => fn () => $table->json('provider_response')->nullable(),
                'api_submitted_at' => fn () => $table->timestamp('api_submitted_at')->nullable(),
                'api_synced_at' => fn () => $table->timestamp('api_synced_at')->nullable(),
            ] as $column => $addColumn) {
                if (! Schema::hasColumn('gts_orders', $column)) {
                    $addColumn();
                }
            }
        });
    }

    public function down(): void
    {
        //
    }
};
