<?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('orders', 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('invoice_number')->nullable();
            $table->string('customer_id')->nullable(); // ULID FK (nullable)
            $table->string('customer_name')->nullable();
            $table->string('customer_phone')->nullable();
            $table->string('payment_method')->nullable();
            $table->decimal('subtotal', 14, 4)->default(0);
            $table->decimal('discount_amount', 14, 4)->default(0);
            $table->decimal('tax_amount', 14, 4)->default(0);
            $table->decimal('service_fee', 14, 4)->default(0);
            $table->decimal('total_amount', 14, 4)->default(0);
            $table->decimal('amount_paid', 14, 4)->default(0);
            $table->decimal('change_amount', 14, 4)->default(0);
            $table->string('status')->default('completed'); // completed, refunded, void
            $table->string('type')->default('sale'); // sale, return
            $table->string('reference_order_id')->nullable(); // for returns
            $table->text('notes')->nullable();
            $table->timestamp('order_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', 'order_date']);
            $table->index(['user_id', 'updated_at']);
            $table->index('customer_id');
        });

        Schema::create('order_items', function (Blueprint $table) {
            $table->ulid('id')->primary();
            $table->string('order_id');
            $table->string('product_id')->nullable();
            $table->string('local_product_id')->nullable();
            $table->string('product_name');
            $table->string('product_barcode')->nullable();
            $table->decimal('unit_price', 14, 4)->default(0);
            $table->decimal('cost_price', 14, 4)->default(0);
            $table->decimal('quantity', 14, 4)->default(1);
            $table->decimal('discount', 14, 4)->default(0);
            $table->decimal('tax_percent', 8, 4)->default(0);
            $table->decimal('subtotal', 14, 4)->default(0);
            $table->json('options')->nullable();
            $table->timestamps();

            $table->index('order_id');
            $table->index('product_id');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('order_items');
        Schema::dropIfExists('orders');
    }
};
