<?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
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        $supportsFullTextIndexes = DB::connection()->getDriverName() !== 'sqlite';

        Schema::create('imported_jobs', function (Blueprint $table) use ($supportsFullTextIndexes) {
            $table->id();
            $table->foreignId('job_source_id')->constrained()->cascadeOnDelete();
            $table->string('external_id')->nullable();
            $table->string('title');
            $table->string('company');
            $table->string('category', 80)->index();
            $table->string('employment_type', 80)->nullable()->index();
            $table->string('experience_level', 80)->nullable()->index();
            $table->string('location_type', 80)->default('remote')->index();
            $table->string('country_code', 2)->nullable()->index();
            $table->string('timezone')->nullable()->index();
            $table->string('salary_currency', 3)->nullable()->index();
            $table->unsignedInteger('salary_min')->nullable()->index();
            $table->unsignedInteger('salary_max')->nullable()->index();
            $table->text('summary')->nullable();
            $table->longText('description')->nullable();
            $table->json('skills')->nullable();
            $table->json('tags')->nullable();
            $table->string('source_url', 2048);
            $table->string('apply_url', 2048);
            $table->text('source_attribution')->nullable();
            $table->string('status', 50)->default('active')->index();
            $table->boolean('is_featured')->default(false)->index();
            $table->timestamp('posted_at')->nullable()->index();
            $table->timestamp('expires_at')->nullable()->index();
            $table->timestamp('last_seen_at')->nullable()->index();
            $table->json('raw_payload')->nullable();
            $table->timestamps();

            $table->unique(['job_source_id', 'external_id']);
            if ($supportsFullTextIndexes) {
                $table->fullText(['title', 'company', 'summary', 'description']);
            }
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('imported_jobs');
    }
};
