<?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
    {
        // 1. Create reseller_packages table
        Schema::create('reseller_packages', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->decimal('price', 10, 2);
            $table->integer('validity_days')->default(365);
            $table->json('features')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        // 2. Modify users table
        Schema::table('users', function (Blueprint $table) {
            $table->string('reseller_status')->nullable(); // pending, active, expired, suspended
            $table->foreignId('reseller_package_id')->nullable()->constrained('reseller_packages')->nullOnDelete();
            $table->timestamp('reseller_expires_at')->nullable();
        });

        // 3. Modify orders table
        Schema::table('orders', function (Blueprint $table) {
            $table->boolean('is_reseller_order')->default(false);
            $table->foreignId('reseller_id')->nullable()->constrained('users')->nullOnDelete();
            $table->decimal('total_reseller_price', 12, 2)->default(0.00);
            $table->decimal('total_reseller_profit', 12, 2)->default(0.00);
        });

        // 4. Modify order_items table
        Schema::table('order_items', function (Blueprint $table) {
            $table->decimal('reseller_price', 12, 2)->default(0.00);
            $table->decimal('reseller_profit', 12, 2)->default(0.00);
        });

        // 5. Create reseller_wallet_transactions table
        Schema::create('reseller_wallet_transactions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->decimal('amount', 12, 2);
            $table->string('type'); // credit (profit earnings), debit (withdrawals/fees)
            $table->string('status')->default('completed'); // pending, completed, failed, reversed
            $table->string('description')->nullable();
            $table->foreignId('order_id')->nullable()->constrained('orders')->nullOnDelete();
            $table->timestamps();
        });

        // 6. Create reseller_withdraw_requests table
        Schema::create('reseller_withdraw_requests', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->decimal('amount', 12, 2);
            $table->string('payment_method'); // Bkash, Nagad, Rocket, Bank
            $table->string('account_number');
            $table->string('status')->default('pending'); // pending, approved, rejected, cancelled
            $table->text('admin_notes')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('reseller_withdraw_requests');
        Schema::dropIfExists('reseller_wallet_transactions');

        Schema::table('order_items', function (Blueprint $table) {
            $table->dropColumn(['reseller_price', 'reseller_profit']);
        });

        Schema::table('orders', function (Blueprint $table) {
            if (DB::getDriverName() !== 'sqlite') {
                $table->dropForeign(['reseller_id']);
            }
            $table->dropColumn(['is_reseller_order', 'reseller_id', 'total_reseller_price', 'total_reseller_profit']);
        });

        Schema::table('users', function (Blueprint $table) {
            if (DB::getDriverName() !== 'sqlite') {
                $table->dropForeign(['reseller_package_id']);
            }
            $table->dropColumn(['reseller_status', 'reseller_package_id', 'reseller_expires_at']);
        });

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