<?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
    {
        if (Schema::hasTable('refunds')) {
            $this->addRefundColumn('idempotency_key', fn (Blueprint $table) => $table->string('idempotency_key', 120)->nullable()->unique()->after('id'));
            $this->addRefundColumn('voucher_id', fn (Blueprint $table) => $table->unsignedBigInteger('voucher_id')->nullable()->index()->after('refund_method'));
            $this->addRefundColumn('created_by_admin_id', fn (Blueprint $table) => $table->foreignId('created_by_admin_id')->nullable()->after('voucher_id')->constrained('admins')->nullOnDelete());
            $this->addRefundColumn('refunded_at', fn (Blueprint $table) => $table->timestamp('refunded_at')->nullable()->after('created_by_admin_id'));
        }

        if (! Schema::hasTable('showroom_return_items')) {
            Schema::create('showroom_return_items', function (Blueprint $table) {
                $table->id();
                $table->foreignId('refund_id')->nullable()->constrained('refunds')->nullOnDelete();
                $table->foreignId('order_id')->constrained('orders')->cascadeOnDelete();
                $table->foreignId('order_detail_id')->nullable()->constrained('order_details')->nullOnDelete();
                $table->foreignId('item_id')->nullable()->constrained('items')->nullOnDelete();
                $table->string('item_name')->nullable();
                $table->string('variation_type')->nullable()->index();
                $table->unsignedInteger('quantity');
                $table->decimal('unit_price', 24, 8)->default(0);
                $table->decimal('discount_amount', 24, 8)->default(0);
                $table->decimal('tax_amount', 24, 8)->default(0);
                $table->decimal('line_refund_amount', 24, 8)->default(0);
                $table->decimal('cogs_amount', 24, 8)->default(0);
                $table->unsignedBigInteger('stock_movement_id')->nullable()->index();
                $table->string('source_reference', 120)->nullable()->index();
                $table->foreignId('created_by_admin_id')->nullable()->constrained('admins')->nullOnDelete();
                $table->timestamps();

                $table->index(['order_id', 'order_detail_id']);
                $table->index(['refund_id', 'item_id']);
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('showroom_return_items');

        if (! Schema::hasTable('refunds')) {
            return;
        }

        Schema::table('refunds', function (Blueprint $table) {
            if (Schema::hasColumn('refunds', 'created_by_admin_id')) {
                $table->dropConstrainedForeignId('created_by_admin_id');
            }
            foreach (['idempotency_key', 'voucher_id', 'refunded_at'] as $column) {
                if (Schema::hasColumn('refunds', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }

    private function addRefundColumn(string $column, callable $definition): void
    {
        if (Schema::hasColumn('refunds', $column)) {
            return;
        }

        Schema::table('refunds', function (Blueprint $table) use ($definition) {
            $definition($table);
        });
    }
};
