<?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('vouchers', function (Blueprint $table) {
            $table->id();
            $table->ulid('uuid')->unique();
            $table->string('code', 50)->unique();
            $table->string('name', 150);
            $table->foreignId('voucher_type_id')->constrained('voucher_types')->restrictOnDelete();
            $table->foreignId('discount_type_id')->nullable()->constrained('discount_types')->nullOnDelete();
            $table->decimal('discount_value', 12, 2)->default(0);
            $table->decimal('initial_balance', 12, 2)->nullable();
            $table->decimal('remaining_balance', 12, 2)->nullable();
            $table->decimal('minimum_purchase', 12, 2)->default(0);
            $table->decimal('maximum_discount', 12, 2)->nullable();
            $table->unsignedInteger('usage_limit')->nullable();
            $table->unsignedInteger('used_count')->default(0);
            $table->boolean('is_partial_redemption')->default(false);
            $table->boolean('is_stackable')->default(false);
            $table->foreignId('customer_id')->nullable()->constrained('customers')->nullOnDelete();
            // NULL = any branch. No BranchScoped trait — same reasoning as
            // product_bundles: that scope treats NULL as "no rows", not "all".
            $table->foreignId('branch_id')->nullable()->constrained('warehouses')->nullOnDelete();
            $table->timestamp('valid_from')->nullable();
            $table->timestamp('valid_until')->nullable();
            $table->foreignId('status_id')->constrained('statuses')->restrictOnDelete();
            $table->foreignId('issued_by_invoice_id')->nullable()->constrained('invoices')->nullOnDelete();
            $table->text('notes')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('deleted_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('voucher_redemptions', function (Blueprint $table) {
            $table->id();
            $table->ulid('uuid')->unique();
            $table->foreignId('voucher_id')->constrained('vouchers')->restrictOnDelete();
            $table->foreignId('invoice_id')->constrained('invoices')->cascadeOnDelete();
            $table->foreignId('customer_id')->nullable()->constrained('customers')->nullOnDelete();
            $table->decimal('redeemed_amount', 12, 2);
            $table->decimal('balance_before', 12, 2)->nullable();
            $table->decimal('balance_after', 12, 2)->nullable();
            $table->foreignId('redeemed_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('redeemed_at');
            $table->boolean('is_reversed')->default(false);
            $table->timestamp('reversed_at')->nullable();
            $table->foreignId('reversed_by')->nullable()->constrained('users')->nullOnDelete();
            $table->text('reverse_reason')->nullable();
            $table->timestamps();
        });
    }

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