<?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('payment_methods', function (Blueprint $table) {
            $table->ulid('id')->primary();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('store_id')->nullable();
            $table->string('local_id')->nullable();
            $table->string('name');
            $table->string('type'); // cash, card, bank_transfer, mobile_money, custom
            $table->string('icon')->nullable();
            $table->boolean('is_active')->default(true);
            $table->integer('sort_order')->default(0);
            // Sync
            $table->string('device_id')->nullable();
            $table->bigInteger('sync_version')->default(0);
            $table->boolean('is_deleted')->default(false);
            $table->softDeletes();
            $table->timestamps();

            $table->index('user_id');
        });

        Schema::create('vouchers', function (Blueprint $table) {
            $table->ulid('id')->primary();
            $table->string('code')->unique();
            $table->string('plan'); // free, starter, growth, pro
            $table->string('period'); // monthly, yearly, lifetime
            $table->integer('days')->default(30);
            $table->integer('max_uses')->default(1);
            $table->integer('used_count')->default(0);
            $table->timestamp('expires_at')->nullable();
            $table->boolean('is_active')->default(true);
            $table->text('notes')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->index('code');
            $table->index('is_active');
        });

        Schema::create('voucher_redemptions', function (Blueprint $table) {
            $table->ulid('id')->primary();
            $table->string('voucher_id');
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->timestamp('redeemed_at')->nullable();
            $table->timestamps();

            $table->index('voucher_id');
            $table->index('user_id');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('voucher_redemptions');
        Schema::dropIfExists('vouchers');
        Schema::dropIfExists('payment_methods');
    }
};
