<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('pos_transactions', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('academy_id')->constrained('academies');
            $table->foreignId('branch_id')->constrained('branches');
            $table->foreignId('cash_session_id')->constrained('cash_sessions');
            $table->foreignId('participant_id')->nullable()->constrained('participants');
            $table->string('receipt_number', 50)->unique();
            $table->bigInteger('subtotal');
            $table->bigInteger('discount_amount')->default(0);
            $table->bigInteger('tax_amount')->default(0);
            $table->bigInteger('total_amount');
            $table->string('payment_method', 20);
            $table->string('payment_status', 20)->default('completed');
            $table->foreignId('invoice_id')->nullable()->constrained('invoices');
            $table->foreignId('promotion_id')->nullable()->constrained('promotions');
            $table->string('coupon_code', 50)->nullable();
            $table->text('notes')->nullable();
            $table->foreignId('processed_by')->constrained('users');
            $table->timestamp('processed_at');
            $table->timestamp('refunded_at')->nullable();
            $table->jsonb('metadata')->default('{}');
            $table->timestamps();

            $table->index(['academy_id', 'branch_id', 'processed_at']);
            $table->index('cash_session_id');
            $table->index('participant_id');
            $table->index('processed_at');
        });

        DB::statement("ALTER TABLE pos_transactions ADD CONSTRAINT pos_transactions_payment_method_check CHECK (payment_method IN ('cash', 'card', 'wallet', 'split'))");
        DB::statement("ALTER TABLE pos_transactions ADD CONSTRAINT pos_transactions_payment_status_check CHECK (payment_status IN ('completed', 'refunded', 'partially_refunded'))");

        Schema::create('pos_transaction_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('pos_transaction_id')->constrained('pos_transactions')->cascadeOnDelete();
            $table->string('item_type', 30);
            $table->integer('item_id')->nullable();
            $table->string('item_name_ar');
            $table->integer('quantity')->default(1);
            $table->bigInteger('unit_price');
            $table->bigInteger('discount_amount')->default(0);
            $table->bigInteger('line_total');
            $table->jsonb('pricing_snapshot')->default('{}');
            $table->jsonb('metadata')->default('{}');
            $table->timestamps();

            $table->index('pos_transaction_id');
            $table->index(['item_type', 'item_id']);
        });

        DB::statement("ALTER TABLE pos_transaction_items ADD CONSTRAINT pos_transaction_items_item_type_check CHECK (item_type IN ('product', 'program', 'kit', 'service', 'other'))");

        Schema::create('pos_split_payments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('pos_transaction_id')->constrained('pos_transactions')->cascadeOnDelete();
            $table->string('payment_method', 20);
            $table->bigInteger('amount');
            $table->string('reference')->nullable();
            $table->timestamps();
        });

        DB::statement("ALTER TABLE pos_split_payments ADD CONSTRAINT pos_split_payments_payment_method_check CHECK (payment_method IN ('cash', 'card', 'wallet', 'split'))");
    }

    public function down(): void
    {
        Schema::dropIfExists('pos_split_payments');
        Schema::dropIfExists('pos_transaction_items');
        Schema::dropIfExists('pos_transactions');
    }
};
