<?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
    {
        if (!Schema::hasTable('payment_methods')) {
            Schema::create('payment_methods', function (Blueprint $table) {
                $table->id();
                $table->foreignId('user_id')->constrained('users')->onDelete('cascade');
                $table->enum('type', ['card', 'mobile_money', 'bank_transfer', 'paypal'])->default('card');
                $table->string('brand')->nullable(); // Visa, Mastercard, MTN, etc.
                $table->string('last4')->nullable(); // Last 4 digits of card/phone
                $table->unsignedInteger('expiry_month')->nullable();
                $table->unsignedInteger('expiry_year')->nullable();
                $table->string('provider')->nullable(); // stripe, paypal, mpesa, etc.
                $table->string('phone')->nullable(); // For mobile money
                $table->string('provider_token')->nullable(); // Tokenized payment method ID from gateway
                $table->boolean('is_default')->default(false);
                $table->string('nickname')->nullable();
                $table->boolean('is_active')->default(true);
                $table->timestamps();
                
                $table->index(['user_id', 'is_default']);
                $table->index(['user_id', 'is_active']);
            });
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('payment_methods');
    }
};
