<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Spatie\Permission\Models\Role;

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('users', function (Blueprint $table) {
            if (! Schema::hasColumn('users', 'address')) {
                $table->text('address')->nullable()->after('phone');
            }
            if (! Schema::hasColumn('users', 'province_id')) {
                $table->foreignId('province_id')->nullable()->after('address')->constrained('provinces')->nullOnDelete();
            }
            if (! Schema::hasColumn('users', 'county_id')) {
                $table->foreignId('county_id')->nullable()->after('province_id')->constrained('counties')->nullOnDelete();
            }
            if (! Schema::hasColumn('users', 'postal_code')) {
                $table->string('postal_code', 20)->nullable()->after('county_id');
            }
            if (! Schema::hasColumn('users', 'latitude')) {
                $table->decimal('latitude', 10, 8)->nullable()->after('postal_code');
            }
            if (! Schema::hasColumn('users', 'longitude')) {
                $table->decimal('longitude', 11, 8)->nullable()->after('latitude');
            }
            if (! Schema::hasColumn('users', 'is_blocked')) {
                $table->boolean('is_blocked')->default(false)->after('status');
            }
            if (! Schema::hasColumn('users', 'blocked_at')) {
                $table->timestamp('blocked_at')->nullable()->after('is_blocked');
            }
            if (! Schema::hasColumn('users', 'blocked_reason')) {
                $table->text('blocked_reason')->nullable()->after('blocked_at');
            }
            if (! Schema::hasColumn('users', 'account_type')) {
                $table->string('account_type', 40)->nullable()->after('login_type'); // admin, employer, producer, expert
            }
        });

        Schema::table('producers', function (Blueprint $table) {
            if (! Schema::hasColumn('producers', 'status')) {
                $table->string('status', 30)->default('pending')->after('unit_videos');
            }
            if (! Schema::hasColumn('producers', 'admin_reason')) {
                $table->text('admin_reason')->nullable()->after('status');
            }
            if (! Schema::hasColumn('producers', 'reviewed_by')) {
                $table->foreignId('reviewed_by')->nullable()->after('admin_reason')->constrained('users')->nullOnDelete();
            }
            if (! Schema::hasColumn('producers', 'reviewed_at')) {
                $table->timestamp('reviewed_at')->nullable()->after('reviewed_by');
            }
        });

        Schema::table('experts', function (Blueprint $table) {
            if (! Schema::hasColumn('experts', 'admin_reason')) {
                $table->text('admin_reason')->nullable()->after('status');
            }
            if (! Schema::hasColumn('experts', 'reviewed_by')) {
                $table->foreignId('reviewed_by')->nullable()->after('admin_reason')->constrained('users')->nullOnDelete();
            }
            if (! Schema::hasColumn('experts', 'reviewed_at')) {
                $table->timestamp('reviewed_at')->nullable()->after('reviewed_by');
            }
        });

        if (Schema::hasColumn('experts', 'status')) {
            \Illuminate\Support\Facades\DB::table('experts')
                ->where('status', 'active')
                ->update(['status' => 'approved']);
        }

        foreach (['admin', 'writer', 'user', 'employer', 'producer', 'expert'] as $role) {
            Role::findOrCreate($role, 'api');
        }
    }

    public function down(): void
    {
        Schema::table('experts', function (Blueprint $table) {
            foreach (['reviewed_at', 'reviewed_by', 'admin_reason'] as $column) {
                if (Schema::hasColumn('experts', $column)) {
                    if ($column === 'reviewed_by') {
                        $table->dropConstrainedForeignId('reviewed_by');
                    } else {
                        $table->dropColumn($column);
                    }
                }
            }
        });

        Schema::table('producers', function (Blueprint $table) {
            foreach (['reviewed_at', 'reviewed_by', 'admin_reason', 'status'] as $column) {
                if (Schema::hasColumn('producers', $column)) {
                    if ($column === 'reviewed_by') {
                        $table->dropConstrainedForeignId('reviewed_by');
                    } else {
                        $table->dropColumn($column);
                    }
                }
            }
        });

        Schema::table('users', function (Blueprint $table) {
            foreach (['account_type', 'blocked_reason', 'blocked_at', 'is_blocked', 'longitude', 'latitude', 'postal_code'] as $column) {
                if (Schema::hasColumn('users', $column)) {
                    $table->dropColumn($column);
                }
            }
            if (Schema::hasColumn('users', 'county_id')) {
                $table->dropConstrainedForeignId('county_id');
            }
            if (Schema::hasColumn('users', 'province_id')) {
                $table->dropConstrainedForeignId('province_id');
            }
            if (Schema::hasColumn('users', 'address')) {
                $table->dropColumn('address');
            }
        });
    }
};
