<?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::table('recruitment_vacancies', function (Blueprint $table) {
            $table->string('slug', 160)->nullable()->after('title');
            $table->boolean('show_on_portal')->default(false)->after('status');
            $table->boolean('is_featured')->default(false)->after('show_on_portal');
            $table->string('seo_title', 180)->nullable()->after('description');
            $table->string('seo_description', 320)->nullable()->after('seo_title');
            $table->string('seo_keywords', 255)->nullable()->after('seo_description');
            $table->unique(['tenant_id', 'slug'], 'rvac_tenant_slug_uq');
            $table->index(['tenant_id', 'show_on_portal', 'is_featured'], 'rvac_portal_feat_idx');
        });

        Schema::table('recruitment_applications', function (Blueprint $table) {
            $table->string('tracking_token', 64)->nullable()->after('reference_code');
            $table->unique(['tenant_id', 'tracking_token'], 'rapp_tenant_track_uq');
        });

        Schema::create('recruitment_career_portal_settings', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->unique();
            $table->boolean('is_enabled')->default(false);
            $table->string('portal_title', 180)->nullable();
            $table->string('portal_subtitle', 255)->nullable();
            $table->text('about_text')->nullable();
            $table->string('contact_email', 191)->nullable();
            $table->string('contact_phone', 60)->nullable();
            $table->string('seo_title', 180)->nullable();
            $table->string('seo_description', 320)->nullable();
            $table->string('seo_keywords', 255)->nullable();
            $table->boolean('recaptcha_enabled')->default(false);
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('recruitment_career_portal_settings');

        Schema::table('recruitment_applications', function (Blueprint $table) {
            $table->dropUnique('rapp_tenant_track_uq');
            $table->dropColumn('tracking_token');
        });

        Schema::table('recruitment_vacancies', function (Blueprint $table) {
            $table->dropUnique('rvac_tenant_slug_uq');
            $table->dropIndex('rvac_portal_feat_idx');
            $table->dropColumn([
                'slug', 'show_on_portal', 'is_featured',
                'seo_title', 'seo_description', 'seo_keywords',
            ]);
        });
    }
};
