<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('core_sales_order_lines', function (Blueprint $table) {
            $table->id();
            $table->foreignId('sales_order_id')->constrained('core_sales_orders')->cascadeOnDelete();
            $table->foreignId('product_id')->nullable()->constrained('core_products')->nullOnDelete();
            $table->foreignId('product_variant_id')->nullable()->constrained('core_product_variants')->nullOnDelete();
            $table->unsignedBigInteger('unit_id')->nullable(); // FK added in WMSInventoryCore
            $table->string('product_name'); // Snapshot at time of order
            $table->string('product_sku')->nullable();
            $table->decimal('quantity', 10, 2)->default(1);
            $table->decimal('weight', 8, 2)->nullable();
            $table->decimal('unit_price', 15, 4)->default(0);
            $table->decimal('unit_cost', 15, 4)->nullable();
            $table->decimal('tax_rate', 8, 2)->nullable();
            $table->decimal('tax_amount', 15, 2)->default(0);
            $table->decimal('discount_rate', 8, 2)->nullable();
            $table->decimal('discount_amount', 15, 2)->default(0);
            $table->decimal('subtotal', 15, 2)->default(0);
            $table->decimal('line_total', 15, 2)->default(0);
            $table->decimal('profit', 15, 2)->nullable();
            $table->integer('fulfilled_quantity')->default(0);
            $table->boolean('is_fulfilled')->default(false);
            $table->integer('returned_quantity')->default(0);
            $table->text('return_reason')->nullable();
            $table->foreignId('created_by_id')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by_id')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->softDeletes();

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

        Schema::create('core_sales_order_attachments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('sales_order_id')->constrained('core_sales_orders')->cascadeOnDelete();
            $table->string('file_path');
            $table->string('file_name')->nullable();
            $table->string('file_type')->nullable();
            $table->foreignId('created_by_id')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by_id')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->softDeletes();
        });
    }

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