<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('jobs', function (Blueprint $table) {
            // Check if columns exist before adding
            if (!Schema::hasColumn('jobs', 'employer_id')) {
                $table->foreignId('employer_id')->after('id')->constrained('users')->onDelete('cascade');
            }
            if (!Schema::hasColumn('jobs', 'job_title')) {
                $table->string('job_title')->after('employer_id');
            }
            if (!Schema::hasColumn('jobs', 'company_name')) {
                $table->string('company_name')->after('job_title');
            }
            if (!Schema::hasColumn('jobs', 'job_description')) {
                $table->text('job_description')->after('company_name');
            }
            if (!Schema::hasColumn('jobs', 'requirements')) {
                $table->text('requirements')->nullable()->after('job_description');
            }
            if (!Schema::hasColumn('jobs', 'location')) {
                $table->string('location')->nullable()->after('requirements');
            }
            if (!Schema::hasColumn('jobs', 'salary_range')) {
                $table->string('salary_range')->nullable()->after('location');
            }
            if (!Schema::hasColumn('jobs', 'job_type')) {
                $table->string('job_type')->default('full-time')->after('salary_range');
            }
            if (!Schema::hasColumn('jobs', 'experience_level')) {
                $table->string('experience_level')->default('entry')->after('job_type');
            }
            if (!Schema::hasColumn('jobs', 'application_deadline')) {
                $table->date('application_deadline')->nullable()->after('experience_level');
            }
            if (!Schema::hasColumn('jobs', 'status')) {
                $table->string('status')->default('pending')->after('application_deadline');
            }
            if (!Schema::hasColumn('jobs', 'admin_notes')) {
                $table->text('admin_notes')->nullable()->after('status');
            }
            if (!Schema::hasColumn('jobs', 'processed_at')) {
                $table->timestamp('processed_at')->nullable()->after('admin_notes');
            }
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('jobs', function (Blueprint $table) {
            if (Schema::hasColumn('jobs', 'processed_at')) {
                $table->dropColumn('processed_at');
            }
            if (Schema::hasColumn('jobs', 'admin_notes')) {
                $table->dropColumn('admin_notes');
            }
            if (Schema::hasColumn('jobs', 'status')) {
                $table->dropColumn('status');
            }
            if (Schema::hasColumn('jobs', 'application_deadline')) {
                $table->dropColumn('application_deadline');
            }
            if (Schema::hasColumn('jobs', 'experience_level')) {
                $table->dropColumn('experience_level');
            }
            if (Schema::hasColumn('jobs', 'job_type')) {
                $table->dropColumn('job_type');
            }
            if (Schema::hasColumn('jobs', 'salary_range')) {
                $table->dropColumn('salary_range');
            }
            if (Schema::hasColumn('jobs', 'location')) {
                $table->dropColumn('location');
            }
            if (Schema::hasColumn('jobs', 'requirements')) {
                $table->dropColumn('requirements');
            }
            if (Schema::hasColumn('jobs', 'job_description')) {
                $table->dropColumn('job_description');
            }
            if (Schema::hasColumn('jobs', 'company_name')) {
                $table->dropColumn('company_name');
            }
            if (Schema::hasColumn('jobs', 'job_title')) {
                $table->dropColumn('job_title');
            }
            if (Schema::hasColumn('jobs', 'employer_id')) {
                $table->dropForeign(['employer_id']);
                $table->dropColumn('employer_id');
            }
        });
    }
};
