<?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
    {
        if (Schema::hasTable('suppliers')) {
            Schema::table('suppliers', function (Blueprint $table) {
                if (!Schema::hasColumn('suppliers', 'phone')) $table->string('phone', 30)->nullable()->after('status');
                if (!Schema::hasColumn('suppliers', 'email')) $table->string('email', 150)->nullable()->after('phone');
                if (!Schema::hasColumn('suppliers', 'contact_person')) $table->string('contact_person', 120)->nullable()->after('email');
                if (!Schema::hasColumn('suppliers', 'address')) $table->string('address', 255)->nullable()->after('contact_person');
            });
        }

        if (Schema::hasTable('purchases')) {
            Schema::table('purchases', function (Blueprint $table) {
                if (!Schema::hasColumn('purchases', 'supplier_id')) $table->unsignedBigInteger('supplier_id')->nullable()->after('invoice_no');
                if (!Schema::hasColumn('purchases', 'subtotal')) $table->decimal('subtotal', 12, 2)->default(0)->after('notes');
                if (!Schema::hasColumn('purchases', 'discount_total')) $table->decimal('discount_total', 12, 2)->default(0)->after('subtotal');
                if (!Schema::hasColumn('purchases', 'transport_cost')) $table->decimal('transport_cost', 12, 2)->default(0)->after('discount_total');
                if (!Schema::hasColumn('purchases', 'paid_amount')) $table->decimal('paid_amount', 12, 2)->default(0)->after('grand_total');
                if (!Schema::hasColumn('purchases', 'due_amount')) $table->decimal('due_amount', 12, 2)->default(0)->after('paid_amount');
                if (!Schema::hasColumn('purchases', 'payment_method')) $table->string('payment_method', 50)->nullable()->after('due_amount');
            });
        }

        if (Schema::hasTable('sales')) {
            Schema::table('sales', function (Blueprint $table) {
                if (!Schema::hasColumn('sales', 'customer_id')) $table->unsignedBigInteger('customer_id')->nullable()->after('invoice_no');
                if (!Schema::hasColumn('sales', 'invoice_subtotal')) $table->decimal('invoice_subtotal', 12, 2)->default(0)->after('due_amount');
                if (!Schema::hasColumn('sales', 'invoice_discount')) $table->decimal('invoice_discount', 12, 2)->default(0)->after('invoice_subtotal');
                if (!Schema::hasColumn('sales', 'invoice_grand_total')) $table->decimal('invoice_grand_total', 12, 2)->default(0)->after('invoice_discount');
                if (!Schema::hasColumn('sales', 'paid_amount')) $table->decimal('paid_amount', 12, 2)->default(0)->after('invoice_grand_total');
                if (!Schema::hasColumn('sales', 'payment_method')) $table->string('payment_method', 50)->nullable()->after('paid_amount');
            });
        }

        if (Schema::hasTable('sale_returns')) {
            Schema::table('sale_returns', function (Blueprint $table) {
                if (!Schema::hasColumn('sale_returns', 'sale_id')) $table->unsignedBigInteger('sale_id')->nullable()->after('return_no');
                if (!Schema::hasColumn('sale_returns', 'invoice_no')) $table->string('invoice_no', 50)->nullable()->after('sale_id');
                if (!Schema::hasColumn('sale_returns', 'refund_method')) $table->string('refund_method', 50)->nullable()->after('refund_total');
            });
        }

        if (Schema::hasTable('supplier_returns')) {
            Schema::table('supplier_returns', function (Blueprint $table) {
                if (!Schema::hasColumn('supplier_returns', 'purchase_id')) $table->unsignedBigInteger('purchase_id')->nullable()->after('return_no');
                if (!Schema::hasColumn('supplier_returns', 'refund_method')) $table->string('refund_method', 50)->nullable()->after('total');
            });
        }

        if (!Schema::hasTable('stock_movements')) {
            Schema::create('stock_movements', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('product_id')->nullable()->index();
                $table->string('product_code', 100)->nullable()->index();
                $table->string('product_name', 150)->nullable();
                $table->string('movement_type', 50)->index();
                $table->string('reference_no', 80)->nullable()->index();
                $table->integer('qty')->default(0);
                $table->decimal('unit_cost', 12, 2)->default(0);
                $table->decimal('unit_price', 12, 2)->default(0);
                $table->text('notes')->nullable();
                $table->timestamps();
            });
        }
    }

    public function down(): void
    {
        if (Schema::hasTable('stock_movements')) Schema::dropIfExists('stock_movements');
    }
};
