<?php

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

return new class extends Migration {
    public function up(): void
    {
        if (Schema::hasTable('admins')) {
            Schema::table('admins', function (Blueprint $table) {
                if (!Schema::hasColumn('admins', 'role')) $table->string('role', 50)->default('Super Admin')->after('email');
                if (!Schema::hasColumn('admins', 'permissions')) $table->json('permissions')->nullable()->after('role');
                if (!Schema::hasColumn('admins', 'status')) $table->string('status', 20)->default('Active')->after('permissions');
            });

            if (!DB::table('admins')->where('email', 'admin@sef.com')->exists()) {
                DB::table('admins')->insert([
                    'name' => 'Super Admin',
                    'email' => 'admin@sef.com',
                    'password' => Hash::make('12345678'),
                    'role' => 'Super Admin',
                    'permissions' => json_encode(['dashboard','products','purchases','sales','returns','expenses','customers','accounts','reports','inventory','users','settings']),
                    'status' => 'Active',
                ]);
            }
        }

        if (Schema::hasTable('customers')) {
            Schema::table('customers', function (Blueprint $table) {
                if (!Schema::hasColumn('customers', 'email')) $table->string('email', 150)->nullable()->after('phone_number');
                if (!Schema::hasColumn('customers', 'customer_group')) $table->string('customer_group', 50)->default('Retail')->after('address');
                if (!Schema::hasColumn('customers', 'opening_balance')) $table->decimal('opening_balance', 12, 2)->default(0)->after('customer_group');
                if (!Schema::hasColumn('customers', 'points')) $table->integer('points')->default(0)->after('opening_balance');
                if (!Schema::hasColumn('customers', 'status')) $table->string('status', 20)->default('Active')->after('points');
            });
        }

        if (!Schema::hasTable('account_transactions')) {
            Schema::create('account_transactions', function (Blueprint $table) {
                $table->id();
                $table->date('txn_date')->nullable();
                $table->string('type', 50)->index();
                $table->string('category', 100)->nullable();
                $table->string('reference_no', 80)->nullable()->index();
                $table->string('party_type', 50)->nullable();
                $table->unsignedBigInteger('party_id')->nullable();
                $table->string('party_name', 150)->nullable();
                $table->decimal('amount', 12, 2)->default(0);
                $table->string('payment_method', 50)->nullable();
                $table->text('notes')->nullable();
                $table->unsignedBigInteger('created_by')->nullable();
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('inventory_locations')) {
            Schema::create('inventory_locations', function (Blueprint $table) {
                $table->id();
                $table->string('name', 100);
                $table->string('code', 30)->nullable();
                $table->string('status', 20)->default('Active');
                $table->timestamps();
            });
            DB::table('inventory_locations')->insert([
                ['name' => 'Main Store', 'code' => 'MAIN', 'status' => 'Active', 'created_at' => now(), 'updated_at' => now()],
                ['name' => 'Warehouse', 'code' => 'WH', 'status' => 'Active', 'created_at' => now(), 'updated_at' => now()],
            ]);
        }

        if (!Schema::hasTable('stock_adjustments')) {
            Schema::create('stock_adjustments', function (Blueprint $table) {
                $table->id();
                $table->string('adjustment_no', 80)->index();
                $table->date('adjustment_date')->nullable();
                $table->unsignedBigInteger('product_id')->nullable();
                $table->string('product_code', 100)->nullable();
                $table->string('product_name', 150)->nullable();
                $table->integer('current_qty')->default(0);
                $table->integer('adjust_qty')->default(0);
                $table->integer('new_qty')->default(0);
                $table->string('reason', 120)->nullable();
                $table->text('notes')->nullable();
                $table->unsignedBigInteger('created_by')->nullable();
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('stock_transfers')) {
            Schema::create('stock_transfers', function (Blueprint $table) {
                $table->id();
                $table->string('transfer_no', 80)->index();
                $table->date('transfer_date')->nullable();
                $table->unsignedBigInteger('product_id')->nullable();
                $table->string('product_code', 100)->nullable();
                $table->string('product_name', 150)->nullable();
                $table->string('from_location', 100)->nullable();
                $table->string('to_location', 100)->nullable();
                $table->integer('qty')->default(0);
                $table->text('notes')->nullable();
                $table->unsignedBigInteger('created_by')->nullable();
                $table->timestamps();
            });
        }
    }

    public function down(): void
    {
        if (Schema::hasTable('stock_transfers')) Schema::dropIfExists('stock_transfers');
        if (Schema::hasTable('stock_adjustments')) Schema::dropIfExists('stock_adjustments');
        if (Schema::hasTable('inventory_locations')) Schema::dropIfExists('inventory_locations');
        if (Schema::hasTable('account_transactions')) Schema::dropIfExists('account_transactions');
    }
};
