<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('suppliers', function (Blueprint $table) {
            $table->boolean('is_company_warehouse')->default(false)->after('is_active');
        });

        $exists = DB::table('suppliers')->where('name', 'مخزن الشركة')->exists();
        if (! $exists) {
            DB::table('suppliers')->insert([
                'name' => 'مخزن الشركة',
                'phone' => null,
                'address' => null,
                'current_balance' => 0,
                'is_active' => true,
                'is_company_warehouse' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ]);
        } else {
            DB::table('suppliers')->where('name', 'مخزن الشركة')->update(['is_company_warehouse' => true]);
        }
    }

    public function down(): void
    {
        Schema::table('suppliers', function (Blueprint $table) {
            $table->dropColumn('is_company_warehouse');
        });
    }
};
