<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        if (in_array(DB::getDriverName(), ['mysql', 'mariadb'], true)) {
            DB::statement("ALTER TABLE users MODIFY role ENUM('super_admin', 'admin', 'client', 'designer', 'gts_client') NOT NULL DEFAULT 'client'");
        }

        Schema::create('gts_services', function (Blueprint $table): void {
            $table->id();
            $table->string('category');
            $table->string('name');
            $table->text('description')->nullable();
            $table->unsignedInteger('min_quantity')->default(100);
            $table->unsignedInteger('max_quantity')->default(10000);
            $table->decimal('rate_per_1000', 10, 2)->default(0);
            $table->string('status')->default('active');
            $table->timestamps();
        });

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

        Schema::create('gts_wallet_transactions', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('gts_wallet_id')->constrained()->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();
        });

        Schema::create('gts_orders', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('gts_service_id')->constrained()->cascadeOnDelete();
            $table->string('order_number')->unique();
            $table->string('target_link');
            $table->unsignedInteger('quantity');
            $table->decimal('charge', 12, 2)->default(0);
            $table->unsignedTinyInteger('progress')->default(0);
            $table->text('notes')->nullable();
            $table->string('status')->default('queued');
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('gts_orders');
        Schema::dropIfExists('gts_wallet_transactions');
        Schema::dropIfExists('gts_wallets');
        Schema::dropIfExists('gts_services');

        if (in_array(DB::getDriverName(), ['mysql', 'mariadb'], true)) {
            DB::statement("UPDATE users SET role = 'client' WHERE role = 'gts_client'");
            DB::statement("ALTER TABLE users MODIFY role ENUM('super_admin', 'admin', 'client', 'designer') NOT NULL DEFAULT 'client'");
        }
    }
};
