<?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('driver_cashback_wallets', function (Blueprint $table) {
            $table->id();
            $table->foreignId('driver_id')->constrained('users')->onDelete('cascade');
            $table->decimal('balance', 15, 2)->default(0);
            $table->timestamp('first_deposit_at')->nullable();
            $table->date('last_cashback_date')->nullable();
            $table->unsignedSmallInteger('consecutive_days')->default(0);
            $table->timestamps();
            $table->unique('driver_id');
        });

        Schema::create('driver_cashback_transactions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('cashback_wallet_id')->constrained('driver_cashback_wallets')->onDelete('cascade');
            $table->string('type', 32); // trip_cashback | continuity_bonus | rating_bonus | referral_bonus | transfer_to_wallet | early_transfer_penalty
            $table->decimal('amount', 15, 2);
            $table->decimal('balance_after', 15, 2)->nullable();
            $table->string('source', 64)->nullable(); // trip_request_id | referral_id
            $table->unsignedBigInteger('source_id')->nullable();
            $table->text('description')->nullable();
            $table->json('meta')->nullable();
            $table->timestamps();
            $table->index(['cashback_wallet_id', 'created_at']);
        });

        Schema::create('driver_referral_codes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('driver_id')->constrained('users')->onDelete('cascade');
            $table->string('code', 32)->unique();
            $table->timestamps();
            $table->unique('driver_id');
        });

        Schema::create('driver_referrals', function (Blueprint $table) {
            $table->id();
            $table->foreignId('referrer_driver_id')->constrained('users')->onDelete('cascade');
            $table->foreignId('referred_driver_id')->constrained('users')->onDelete('cascade');
            $table->string('referral_code', 32);
            $table->date('referred_at');
            $table->unsignedTinyInteger('bonus_months_remaining')->default(1);
            $table->timestamps();
            $table->unique('referred_driver_id');
        });

        Schema::table('app_settings', function (Blueprint $table) {
            if (!Schema::hasColumn('app_settings', 'cashback_enabled')) {
                $table->boolean('cashback_enabled')->default(true)->after('wallet_aman_card_instructions');
            }
            if (!Schema::hasColumn('app_settings', 'cashback_base_percent')) {
                $table->decimal('cashback_base_percent', 5, 2)->default(6)->after('cashback_enabled');
            }
            if (!Schema::hasColumn('app_settings', 'cashback_level_1_trips')) {
                $table->unsignedTinyInteger('cashback_level_1_trips')->default(5)->after('cashback_base_percent');
            }
            if (!Schema::hasColumn('app_settings', 'cashback_level_1_percent')) {
                $table->decimal('cashback_level_1_percent', 5, 2)->default(5)->after('cashback_level_1_trips');
            }
            if (!Schema::hasColumn('app_settings', 'cashback_level_2_trips')) {
                $table->unsignedTinyInteger('cashback_level_2_trips')->default(10)->after('cashback_level_1_percent');
            }
            if (!Schema::hasColumn('app_settings', 'cashback_level_2_percent')) {
                $table->decimal('cashback_level_2_percent', 5, 2)->default(6)->after('cashback_level_2_trips');
            }
            if (!Schema::hasColumn('app_settings', 'cashback_level_3_trips')) {
                $table->unsignedTinyInteger('cashback_level_3_trips')->default(15)->after('cashback_level_2_percent');
            }
            if (!Schema::hasColumn('app_settings', 'cashback_level_3_percent')) {
                $table->decimal('cashback_level_3_percent', 5, 2)->default(8)->after('cashback_level_3_trips');
            }
            if (!Schema::hasColumn('app_settings', 'cashback_rating_threshold')) {
                $table->decimal('cashback_rating_threshold', 3, 2)->default(4.80)->after('cashback_level_3_percent');
            }
            if (!Schema::hasColumn('app_settings', 'cashback_rating_bonus_percent')) {
                $table->decimal('cashback_rating_bonus_percent', 5, 2)->default(1)->after('cashback_rating_threshold');
            }
            if (!Schema::hasColumn('app_settings', 'cashback_continuity_days')) {
                $table->unsignedTinyInteger('cashback_continuity_days')->default(7)->after('cashback_rating_bonus_percent');
            }
            if (!Schema::hasColumn('app_settings', 'cashback_continuity_bonus_percent')) {
                $table->decimal('cashback_continuity_bonus_percent', 5, 2)->default(2)->after('cashback_continuity_days');
            }
            if (!Schema::hasColumn('app_settings', 'cashback_lock_days')) {
                $table->unsignedSmallInteger('cashback_lock_days')->default(30)->after('cashback_continuity_bonus_percent');
            }
            if (!Schema::hasColumn('app_settings', 'cashback_early_transfer_penalty_percent')) {
                $table->decimal('cashback_early_transfer_penalty_percent', 5, 2)->default(20)->after('cashback_lock_days');
            }
            if (!Schema::hasColumn('app_settings', 'cashback_referral_percent')) {
                $table->decimal('cashback_referral_percent', 5, 2)->default(5)->after('cashback_early_transfer_penalty_percent');
            }
            if (!Schema::hasColumn('app_settings', 'cashback_referral_months')) {
                $table->unsignedTinyInteger('cashback_referral_months')->default(1)->after('cashback_referral_percent');
            }
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('driver_cashback_transactions');
        Schema::dropIfExists('driver_referrals');
        Schema::dropIfExists('driver_referral_codes');
        Schema::dropIfExists('driver_cashback_wallets');

        Schema::table('app_settings', function (Blueprint $table) {
            $cols = [
                'cashback_enabled', 'cashback_base_percent', 'cashback_level_1_trips', 'cashback_level_1_percent',
                'cashback_level_2_trips', 'cashback_level_2_percent', 'cashback_level_3_trips', 'cashback_level_3_percent',
                'cashback_rating_threshold', 'cashback_rating_bonus_percent', 'cashback_continuity_days',
                'cashback_continuity_bonus_percent', 'cashback_lock_days', 'cashback_early_transfer_penalty_percent',
                'cashback_referral_percent', 'cashback_referral_months',
            ];
            foreach ($cols as $col) {
                if (Schema::hasColumn('app_settings', $col)) {
                    $table->dropColumn($col);
                }
            }
        });
    }
};
