<?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
    {
        Schema::create('ledger_entries', function (Blueprint $table) {
            $table->ulid('id')->primary();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('store_id')->nullable();
            $table->string('local_id')->nullable();
            $table->string('type'); // income, expense
            $table->string('category')->nullable();
            $table->string('sub_category')->nullable();
            $table->decimal('amount', 14, 4)->default(0);
            $table->string('payment_method')->nullable();
            $table->string('reference_type')->nullable(); // order, purchase, expense, manual
            $table->string('reference_id')->nullable();
            $table->text('description')->nullable();
            $table->timestamp('entry_date')->nullable();
            // Sync
            $table->string('device_id')->nullable();
            $table->bigInteger('sync_version')->default(0);
            $table->boolean('is_deleted')->default(false);
            $table->softDeletes();
            $table->timestamps();

            $table->index('user_id');
            $table->index(['user_id', 'store_id']);
            $table->index(['user_id', 'entry_date']);
            $table->index(['user_id', 'updated_at']);
        });

        Schema::create('cash_registers', function (Blueprint $table) {
            $table->ulid('id')->primary();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('store_id')->nullable();
            $table->string('local_id')->nullable();
            $table->decimal('opening_balance', 14, 4)->default(0);
            $table->decimal('closing_balance', 14, 4)->nullable();
            $table->decimal('expected_balance', 14, 4)->nullable();
            $table->decimal('difference', 14, 4)->nullable();
            $table->decimal('total_sales', 14, 4)->default(0);
            $table->decimal('total_returns', 14, 4)->default(0);
            $table->decimal('total_expenses', 14, 4)->default(0);
            $table->string('status')->default('open'); // open, closed
            $table->text('notes')->nullable();
            $table->timestamp('opened_at')->nullable();
            $table->timestamp('closed_at')->nullable();
            // Sync
            $table->string('device_id')->nullable();
            $table->bigInteger('sync_version')->default(0);
            $table->boolean('is_deleted')->default(false);
            $table->softDeletes();
            $table->timestamps();

            $table->index('user_id');
            $table->index(['user_id', 'store_id']);
            $table->index(['user_id', 'updated_at']);
        });

        Schema::create('stock_adjustments', function (Blueprint $table) {
            $table->ulid('id')->primary();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('store_id')->nullable();
            $table->string('local_id')->nullable();
            $table->string('product_id')->nullable();
            $table->string('local_product_id')->nullable();
            $table->string('product_name')->nullable();
            $table->string('type'); // add, subtract, set
            $table->decimal('quantity_before', 14, 4)->default(0);
            $table->decimal('quantity_change', 14, 4)->default(0);
            $table->decimal('quantity_after', 14, 4)->default(0);
            $table->string('reason')->nullable();
            $table->text('notes')->nullable();
            $table->timestamp('adjustment_date')->nullable();
            // Sync
            $table->string('device_id')->nullable();
            $table->bigInteger('sync_version')->default(0);
            $table->boolean('is_deleted')->default(false);
            $table->softDeletes();
            $table->timestamps();

            $table->index('user_id');
            $table->index(['user_id', 'store_id']);
            $table->index(['user_id', 'updated_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('stock_adjustments');
        Schema::dropIfExists('cash_registers');
        Schema::dropIfExists('ledger_entries');
    }
};
