<?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('stock_transfers', function (Blueprint $table) {
            $table->id();
            $table->string('transfer_number')->unique();
            $table->foreignId('from_location_id')->constrained('locations')->cascadeOnDelete();
            $table->foreignId('to_location_id')->constrained('locations')->cascadeOnDelete();
            $table->enum('status', ['draft', 'approved', 'in_transit', 'received', 'cancelled'])->default('draft');
            $table->text('notes')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('approved_at')->nullable();
            $table->timestamp('shipped_at')->nullable();
            $table->timestamp('received_at')->nullable();
            $table->timestamps();

            $table->index(['status', 'from_location_id', 'to_location_id']);
        });

        Schema::create('stock_transfer_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('stock_transfer_id')->constrained('stock_transfers')->cascadeOnDelete();
            $table->foreignId('product_id')->constrained('products')->cascadeOnDelete();
            $table->integer('quantity');
            $table->integer('received_quantity')->default(0);
            $table->timestamps();
        });

        Schema::create('suppliers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('company_id')->constrained('companies')->cascadeOnDelete();
            $table->string('name');
            $table->string('code', 50)->nullable();
            $table->string('contact_name')->nullable();
            $table->string('phone')->nullable();
            $table->string('email')->nullable();
            $table->string('gstin')->nullable();
            $table->text('address')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('purchase_orders', function (Blueprint $table) {
            $table->id();
            $table->string('po_number')->unique();
            $table->foreignId('company_id')->constrained('companies')->cascadeOnDelete();
            $table->foreignId('supplier_id')->constrained('suppliers')->cascadeOnDelete();
            $table->foreignId('location_id')->constrained('locations')->cascadeOnDelete();
            $table->enum('status', ['draft', 'ordered', 'partial', 'received', 'cancelled'])->default('draft');
            $table->date('expected_date')->nullable();
            $table->decimal('total_amount', 12, 2)->default(0);
            $table->text('notes')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->index(['status', 'location_id']);
        });

        Schema::create('purchase_order_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('purchase_order_id')->constrained('purchase_orders')->cascadeOnDelete();
            $table->foreignId('product_id')->constrained('products')->cascadeOnDelete();
            $table->integer('quantity');
            $table->integer('received_quantity')->default(0);
            $table->decimal('unit_cost', 12, 2)->default(0);
            $table->timestamps();
        });

        Schema::create('goods_receipts', function (Blueprint $table) {
            $table->id();
            $table->string('grn_number')->unique();
            $table->foreignId('location_id')->constrained('locations')->cascadeOnDelete();
            $table->foreignId('purchase_order_id')->nullable()->constrained('purchase_orders')->nullOnDelete();
            $table->foreignId('stock_transfer_id')->nullable()->constrained('stock_transfers')->nullOnDelete();
            $table->enum('status', ['draft', 'posted', 'cancelled'])->default('draft');
            $table->text('notes')->nullable();
            $table->foreignId('received_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('received_at')->nullable();
            $table->timestamps();
        });

        Schema::create('goods_receipt_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('goods_receipt_id')->constrained('goods_receipts')->cascadeOnDelete();
            $table->foreignId('product_id')->constrained('products')->cascadeOnDelete();
            $table->integer('quantity');
            $table->decimal('unit_cost', 12, 2)->nullable();
            $table->timestamps();
        });

        Schema::create('replenishment_rules', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_location_id')->constrained('locations')->cascadeOnDelete();
            $table->foreignId('warehouse_location_id')->constrained('locations')->cascadeOnDelete();
            $table->foreignId('product_id')->nullable()->constrained('products')->nullOnDelete();
            $table->integer('min_qty')->default(0);
            $table->integer('max_qty')->default(0);
            $table->integer('reorder_qty')->default(0);
            $table->boolean('is_active')->default(true);
            $table->timestamps();

            $table->index(['store_location_id', 'is_active']);
        });

        Schema::create('cash_registers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('location_id')->constrained('locations')->cascadeOnDelete();
            $table->string('name');
            $table->string('code', 50)->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();

            $table->unique(['location_id', 'name']);
        });

        if (Schema::hasTable('day_ledgers') && !Schema::hasColumn('day_ledgers', 'location_id')) {
            Schema::table('day_ledgers', function (Blueprint $table) {
                $table->foreignId('location_id')->nullable()->after('id')
                    ->constrained('locations')->nullOnDelete();
                $table->foreignId('cash_register_id')->nullable()->after('location_id')
                    ->constrained('cash_registers')->nullOnDelete();
            });
        }

        if (Schema::hasTable('stock_audits') && !Schema::hasColumn('stock_audits', 'location_id')) {
            Schema::table('stock_audits', function (Blueprint $table) {
                $table->foreignId('location_id')->nullable()->after('id')
                    ->constrained('locations')->nullOnDelete();
            });
        }

        if (Schema::hasTable('stock_adjustments') && !Schema::hasColumn('stock_adjustments', 'location_id')) {
            Schema::table('stock_adjustments', function (Blueprint $table) {
                $table->foreignId('location_id')->nullable()->after('id')
                    ->constrained('locations')->nullOnDelete();
            });
        }

        if (Schema::hasTable('shipments') && !Schema::hasColumn('shipments', 'ship_from_location_id')) {
            Schema::table('shipments', function (Blueprint $table) {
                $table->foreignId('ship_from_location_id')->nullable()->after('carrier_id')
                    ->constrained('locations')->nullOnDelete();
            });
        }
    }

    public function down(): void
    {
        if (Schema::hasColumn('shipments', 'ship_from_location_id')) {
            Schema::table('shipments', function (Blueprint $table) {
                $table->dropConstrainedForeignId('ship_from_location_id');
            });
        }
        if (Schema::hasColumn('stock_adjustments', 'location_id')) {
            Schema::table('stock_adjustments', function (Blueprint $table) {
                $table->dropConstrainedForeignId('location_id');
            });
        }
        if (Schema::hasColumn('stock_audits', 'location_id')) {
            Schema::table('stock_audits', function (Blueprint $table) {
                $table->dropConstrainedForeignId('location_id');
            });
        }
        if (Schema::hasColumn('day_ledgers', 'location_id')) {
            Schema::table('day_ledgers', function (Blueprint $table) {
                $table->dropConstrainedForeignId('cash_register_id');
                $table->dropConstrainedForeignId('location_id');
            });
        }

        Schema::dropIfExists('cash_registers');
        Schema::dropIfExists('replenishment_rules');
        Schema::dropIfExists('goods_receipt_items');
        Schema::dropIfExists('goods_receipts');
        Schema::dropIfExists('purchase_order_items');
        Schema::dropIfExists('purchase_orders');
        Schema::dropIfExists('suppliers');
        Schema::dropIfExists('stock_transfer_items');
        Schema::dropIfExists('stock_transfers');
    }
};
