<?php

use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;

return new class extends Migration {
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('pos_sessions', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('hashkey', 300)->unique();
            $table->string('access_key', 300)->unique(); // Guest access key
            $table->unsignedBigInteger('store_id')->index();
            $table->unsignedBigInteger('created_by')->nullable()->index();
            $table->unsignedBigInteger('updated_by')->nullable()->index();
            $table->string('customer_name', 300)->nullable();
            $table->bigInteger('total_amount')->default(0);
            $table->bigInteger('received_amount')->default(0);
            $table->bigInteger('change_amount')->default(0);
            $table->string('payment_method', 50)->nullable();
            $table->json('payment_details')->nullable();
            $table->string('status', 50)->default('active'); // active, completed, voided, pending
            $table->boolean('is_void')->default(false);
            $table->text('notes')->nullable();
            $table->longText('additionaldata')->nullable();
            $table->timestamps();

            $table->foreign('created_by')->references('id')->on('users');
            // Assuming a stores table exists, but following legacy pattern where it's bigint
        });

        Schema::create('pos_transactions', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('pos_session_id')->index();
            $table->unsignedBigInteger('product_id')->index();
            $table->integer('quantity')->default(1);
            $table->bigInteger('price_at_sale')->default(0);
            $table->bigInteger('discount')->default(0);
            $table->bigInteger('total_price')->default(0);
            $table->boolean('is_void')->default(false);
            $table->string('remarks', 300)->nullable();
            $table->timestamps();

            $table->foreign('pos_session_id')->references('id')->on('pos_sessions')->onDelete('cascade');
            // product_id references prd_items.id
        });

        Schema::create('pos_sessions_archive', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('pos_session_id')->index();
            $table->string('hashkey', 300)->index();
            $table->json('session_snapshot')->nullable();
            $table->json('transactions_snapshot')->nullable();
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->string('remarks', 300)->nullable();
            $table->timestamps();

            $table->foreign('pos_session_id')->references('id')->on('pos_sessions')->onDelete('cascade');
        });
    }

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