<?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('global_transactions', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('hashkey', 300)->unique();
            
            $table->unsignedBigInteger('user_id')->index();
            $table->decimal('amount', 15, 2)->default(0);
            $table->integer('type')->default(0); // ProductTransactionType::UNKNOWN
            $table->string('status', 50)->default('completed');
            $table->text('description')->nullable();
            
            $table->unsignedBigInteger('product_id')->nullable()->index();
            $table->unsignedBigInteger('store_id')->nullable()->index();
            
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('product_id')->references('id')->on('prd_items')->onDelete('set null');
            $table->foreign('store_id')->references('id')->on('str')->onDelete('set null');
            $table->foreign('created_by')->references('id')->on('users')->onDelete('set null');
            $table->foreign('updated_by')->references('id')->on('users')->onDelete('set null');
            
            $table->timestamps();
        });
    }

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