<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('payments', function (Blueprint $table) {
            // إضافة الحقول الجديدة
            if (!Schema::hasColumn('payments', 'user_id')) {
                $table->foreignId('user_id')->nullable()->constrained()->onDelete('cascade')->after('id');
            }

            if (!Schema::hasColumn('payments', 'paymentable_type')) {
                $table->string('paymentable_type')->nullable()->after('user_id');
            }

            if (!Schema::hasColumn('payments', 'paymentable_id')) {
                $table->unsignedBigInteger('paymentable_id')->nullable()->after('paymentable_type');
            }

            if (!Schema::hasColumn('payments', 'subscription_fee')) {
                $table->decimal('subscription_fee', 8, 2)->default(0.00)->after('amount');
            }

            if (!Schema::hasColumn('payments', 'total_amount')) {
                $table->decimal('total_amount', 8, 2)->default(0.00)->after('subscription_fee');
            }

            if (!Schema::hasColumn('payments', 'completed_at')) {
                $table->timestamp('completed_at')->nullable()->after('notes');
            }

            if (!Schema::hasColumn('payments', 'cancelled_at')) {
                $table->timestamp('cancelled_at')->nullable()->after('completed_at');
            }

            if (!Schema::hasColumn('payments', 'refund_amount')) {
                $table->decimal('refund_amount', 8, 2)->nullable()->after('cancelled_at');
            }

            if (!Schema::hasColumn('payments', 'refund_reason')) {
                $table->text('refund_reason')->nullable()->after('refund_amount');
            }

            if (!Schema::hasColumn('payments', 'refund_requested_at')) {
                $table->timestamp('refund_requested_at')->nullable()->after('refund_reason');
            }

            // تحديث الحقول الموجودة
            if (Schema::hasColumn('payments', 'payment_method')) {
                $table->enum('payment_method', ['cash', 'card', 'online', 'wallet'])->change();
            }

            if (Schema::hasColumn('payments', 'status')) {
                $table->enum('status', ['pending', 'processing', 'completed', 'failed', 'cancelled', 'refund_requested', 'refunded'])->default('pending')->change();
            }
        });

        // إضافة indexes للتحسين
        Schema::table('payments', function (Blueprint $table) {
            $table->index(['paymentable_type', 'paymentable_id']);
            $table->index(['user_id', 'status']);
            $table->index(['status', 'created_at']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('payments', function (Blueprint $table) {
            // إزالة الحقول المضافة
            $table->dropForeign(['user_id']);
            $table->dropColumn([
                'user_id',
                'paymentable_type',
                'paymentable_id',
                'subscription_fee',
                'total_amount',
                'completed_at',
                'cancelled_at',
                'refund_amount',
                'refund_reason',
                'refund_requested_at'
            ]);

            // إزالة indexes
            $table->dropIndex(['paymentable_type', 'paymentable_id']);
            $table->dropIndex(['user_id', 'status']);
            $table->dropIndex(['status', 'created_at']);
        });
    }
};
