<?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
    {
        Schema::create('departments', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug')->unique();
            $table->text('description')->nullable();
            $table->string('cover_image')->nullable();
            $table->boolean('is_active')->default(true);
            $table->boolean('accepts_open_applications')->default(false);
            $table->integer('sort_order')->default(0);
            $table->timestamps();
        });

        // job_postings tablosuna department_id ekle
        Schema::table('job_postings', function (Blueprint $table) {
            $table->foreignId('department_id')->nullable()->after('department')->constrained('departments')->nullOnDelete();
        });

        // job_applications tablosuna department_id ekle (ilansız başvuru için)
        Schema::table('job_applications', function (Blueprint $table) {
            $table->foreignId('department_id')->nullable()->after('job_posting_id')->constrained('departments')->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('job_applications', function (Blueprint $table) {
            $table->dropForeignIdFor(\App\Models\Department::class);
        });
        Schema::table('job_postings', function (Blueprint $table) {
            $table->dropForeignIdFor(\App\Models\Department::class);
        });
        Schema::dropIfExists('departments');
    }
};