<?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('str', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('hashkey', 300)->unique();
            $table->string('storecode', 50)->default('');
            $table->string('name', 150)->nullable();
            $table->text('description')->nullable();
            $table->timestamps();
            $table->json('store_type')->nullable();
            $table->string('status', 50)->nullable();
            $table->unsignedBigInteger('created_by')->nullable();
            $table->foreign('created_by')->references('id')->on('users');
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->foreign('updated_by')->references('id')->on('users');
            $table->unsignedBigInteger('created_for')->nullable();
            $table->foreign('created_for')->references('id')->on('users');
            // $table->foreignId('created_by')->constrained('users')->nullable();
            // $table->foreignId('updated_by')->constrained('users')->nullable();
            // $table->foreignId('created_for')->constrained('users')->nullable();
            $table->string('remarks', 50)->nullable();
            $table->longText('logs')->nullable();
            $table->json('specs')->nullable();
            $table->longText('additionaldata')->nullable();
            // $table->foreignId('owner_id')->constrained('users')->nullable();
            // $table->foreignId('manager_id')->constrained('users')->nullable();

            $table->unsignedBigInteger('owner_id')->nullable();
            $table->foreign('owner_id')->references('id')->on('users');

            $table->unsignedBigInteger('manager_id')->nullable();
            $table->foreign('manager_id')->references('id')->on('users');

            $table->string('category', 50)->nullable();
            $table->string('subcategory', 50)->nullable();
            $table->json('photourl')->nullable();
            $table->text('address')->nullable();
            $table->boolean('is_active')->default(true);

            // Indexes
            $table->index('storecode');
            $table->index('name');
            $table->index('status');
            $table->index('created_by');
            $table->index('created_for');
            $table->index('owner_id');
            $table->index('manager_id');
        });

        Schema::create('prd_items', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('hashkey', 300)->unique();
            $table->timestamps();
            // $table->foreignId('created_by')->constrained('users')->nullable();
            // $table->foreignId('updated_by')->constrained('users')->nullable();
            // $table->foreignId('created_for')->constrained('users')->nullable();

            $table->unsignedBigInteger('created_by')->nullable();
            $table->foreign('created_by')->references('id')->on('users');
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->foreign('updated_by')->references('id')->on('users');
            $table->unsignedBigInteger('created_for')->nullable();
            $table->foreign('created_for')->references('id')->on('users');

            $table->string('category', 50)->nullable();
            $table->string('subcategory', 50)->nullable();
            $table->longText('logs')->nullable();
            $table->json('specs')->nullable();
            $table->string('product_type')->nullable();
            $table->json('photourl')->nullable();
            $table->integer('available')->nullable();
            $table->integer('sold')->nullable();
            $table->bigInteger('price')->nullable();


            // $table->foreignId('store_id')->constrained('stores')->nullable();


            // $table->foreignId('owner_id')->constrained('users')->nullable();


            $table->unsignedBigInteger('owner_id')->nullable();
            $table->foreign('owner_id')->references('id')->on('users');


            $table->boolean('is_active')->default(true);
            $table->bigInteger('views')->nullable();
            $table->string('name', 300)->nullable();
            $table->longText('description')->nullable();
            $table->json('reviews')->nullable();
            $table->string('barcode', 50)->nullable();
            $table->string('status', 50)->nullable();
            $table->string('remarks', 150)->nullable();
            $table->string('unitname', 300)->nullable();
            $table->integer('rating')->nullable();
            $table->string('sku', 50)->nullable();
            $table->string('qrcode', 300)->nullable();
            $table->string('shortcode', 50)->nullable();
            $table->string('shortname', 50)->nullable();

            $table->index('created_by');
            $table->index('updated_by');
            $table->index('created_for');
            // $table->index('store_id');
            $table->index('owner_id');
            $table->index('category');
            $table->index('subcategory');

        });


        // Products Transactions table
        Schema::create('prd_trx', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('hashkey', 300)->unique();
            $table->timestamps();
      
            $table->text('description')->nullable();
            $table->text('notes')->nullable();
            $table->tinyInteger('transaction_type')->default(0);
            $table->unsignedBigInteger('created_by')->nullable();
            $table->foreign('created_by')->references('id')->on('users');
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->foreign('updated_by')->references('id')->on('users');
            $table->unsignedBigInteger('created_for')->nullable();
            $table->foreign('created_for')->references('id')->on('users');

            $table->bigInteger('store_id')->nullable();
            $table->string('transactiontype', 50)->nullable();
            $table->bigInteger('product_id')->nullable();
            $table->longText('transactiondata')->nullable();
            $table->string('subtype', 50)->nullable();
            $table->string('name', 300)->nullable();
            // $table->bigInteger('owner_id')->nullable();

            $table->unsignedBigInteger('owner_id')->nullable();
            $table->foreign('owner_id')->references('id')->on('users');

            $table->string('transactionsessionhash', 300)->unique()->nullable();
            $table->bigInteger('quantity')->nullable();
            $table->longText('logs')->nullable();
            $table->string('remarks', 50)->nullable();
            $table->integer('price')->nullable();
            $table->boolean('is_void')->default(false);
            $table->bigInteger('last_total_price')->nullable();
            $table->bigInteger('last_total_discount')->nullable();

            $table->index('created_by');
            $table->index('updated_by');
            $table->index('created_for');
            $table->index('name');
            $table->index('transactionsessionhash');
        });

        // Products Transactions Sessions table
        Schema::create('prd_trx_ses', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('hashkey', 300)->unique();
            $table->timestamps();
            $table->string('name', 300)->nullable();
            $table->text('description')->nullable();
            $table->longText('logs')->nullable();
            $table->string('remarks', 300)->nullable();

            $table->text('notes')->nullable();
            $table->unsignedBigInteger('created_by')->nullable();
            $table->foreign('created_by')->references('id')->on('users');
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->foreign('updated_by')->references('id')->on('users');
            $table->unsignedBigInteger('created_for')->nullable();
            $table->foreign('created_for')->references('id')->on('users');

            $table->string('subtype', 50)->nullable();
            $table->longText('additionaldata')->nullable();
            $table->string('category', 50)->nullable();
            $table->bigInteger('store_id')->nullable();
            $table->string('status', 50)->nullable();
            $table->boolean('is_void')->default(false);
            $table->bigInteger('last_total_price')->nullable();
            $table->bigInteger('last_total_discount')->nullable();


            $table->index('hashkey');
            $table->index('created_by');
            $table->index('updated_by');
            $table->index('created_for');
        });

        Schema::create('prd_trx_ses_arc', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->text('name')->nullable();
            $table->text('description')->nullable();
            $table->json('details')->nullable();
            $table->string('hashkey', 300)->unique();

            $table->unsignedBigInteger('transactions_sessions_id')->nullable();
            $table->foreign('transactions_sessions_id')->references('id')->on('prd_trx_ses');

            $table->text('notes')->nullable();
            $table->unsignedBigInteger('created_by')->nullable();
            $table->foreign('created_by')->references('id')->on('users');
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->foreign('updated_by')->references('id')->on('users');
            $table->unsignedBigInteger('created_for')->nullable();
            $table->foreign('created_for')->references('id')->on('users');
            $table->json('transactions_snapshot')->nullable();


            $table->index('created_by');
            $table->index('updated_by');
            $table->index('created_for');
            $table->index('hashkey');
        });
    }


    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('prd_trx_ses_arc');
        Schema::dropIfExists('prd_trx_ses');
        Schema::dropIfExists('prd_trx');
        Schema::dropIfExists('prd_items_arc');
        Schema::dropIfExists('prd_items');
        Schema::dropIfExists('str');

    }
};
