<?php

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

/**
 * Public website CMS and publishing layer (§150, §156, §162).
 *
 * The website is the front door of the same system, not a separate site: jobs
 * are published from approved ERP job demands, and applications land straight
 * in the applicant CRM.
 */
return new class extends Migration
{
    public function up(): void
    {
        // Editable content blocks, keyed by slug (§150).
        Schema::create('site_contents', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('company_id')->constrained()->cascadeOnUpdate()->restrictOnDelete();
            $table->string('key', 80);
            $table->string('group', 40)->default('general');
            $table->string('label');
            $table->string('type', 20)->default('text'); // text | html | image | json
            $table->text('value')->nullable();
            $table->boolean('is_published')->default(true);
            $table->unsignedSmallInteger('sort_order')->default(100);
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->unique(['company_id', 'key']);
            $table->index(['company_id', 'group', 'is_published']);
        });

        // Admin-controlled trust indicators on the homepage (§159).
        Schema::create('site_statistics', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('company_id')->constrained()->cascadeOnUpdate()->restrictOnDelete();
            $table->string('key', 60);
            $table->string('label');
            $table->string('value', 40);
            $table->string('suffix', 20)->nullable();
            $table->string('icon', 40)->nullable();
            $table->boolean('is_published')->default(true);
            $table->unsignedSmallInteger('sort_order')->default(100);
            $table->timestamps();

            $table->unique(['company_id', 'key']);
        });

        // Destination countries shown publicly (§170).
        Schema::create('destination_countries', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('company_id')->constrained()->cascadeOnUpdate()->restrictOnDelete();
            $table->string('code', 2);
            $table->string('name');
            $table->string('slug', 80);
            $table->text('summary')->nullable();
            $table->text('description')->nullable();
            $table->string('flag_emoji', 10)->nullable();
            $table->string('image_path')->nullable();
            $table->char('currency', 3)->nullable();
            $table->boolean('is_published')->default(true);
            $table->boolean('is_featured')->default(false);
            $table->unsignedSmallInteger('sort_order')->default(100);
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['company_id', 'code']);
            $table->unique(['company_id', 'slug']);
        });

        // Trades / occupations grouped for the public site (§171).
        Schema::create('job_categories', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('company_id')->constrained()->cascadeOnUpdate()->restrictOnDelete();
            $table->string('name');
            $table->string('slug', 80);
            $table->text('description')->nullable();
            $table->string('icon', 40)->nullable();
            $table->string('image_path')->nullable();
            $table->boolean('is_published')->default(true);
            $table->boolean('is_featured')->default(false);
            $table->unsignedSmallInteger('sort_order')->default(100);
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['company_id', 'slug']);
        });

        Schema::create('success_stories', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('company_id')->constrained()->cascadeOnUpdate()->restrictOnDelete();
            $table->string('title');
            $table->string('slug', 120);
            $table->string('worker_name')->nullable();
            $table->string('occupation', 120)->nullable();
            $table->string('country', 60)->nullable();
            $table->text('excerpt')->nullable();
            $table->text('body')->nullable();
            $table->string('image_path')->nullable();
            $table->boolean('is_published')->default(false);
            $table->boolean('is_featured')->default(false);
            $table->date('published_on')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['company_id', 'slug']);
            $table->index(['company_id', 'is_published']);
        });

        // Licences, memberships and accreditations (§172).
        Schema::create('company_certificates', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('company_id')->constrained()->cascadeOnUpdate()->restrictOnDelete();
            $table->string('name');
            $table->string('issuing_authority')->nullable();
            $table->string('certificate_number', 100)->nullable();
            $table->date('issue_date')->nullable();
            $table->date('expiry_date')->nullable();
            $table->text('description')->nullable();
            $table->string('image_path')->nullable();
            $table->boolean('is_published')->default(true);
            $table->unsignedSmallInteger('sort_order')->default(100);
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('site_faqs', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('company_id')->constrained()->cascadeOnUpdate()->restrictOnDelete();
            $table->string('question');
            $table->text('answer');
            $table->string('audience', 20)->default('candidate'); // candidate | employer | general
            $table->boolean('is_published')->default(true);
            $table->unsignedSmallInteger('sort_order')->default(100);
            $table->timestamps();

            $table->index(['company_id', 'audience', 'is_published']);
        });

        // Enquiries from the public contact form.
        Schema::create('contact_enquiries', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('company_id')->constrained()->cascadeOnUpdate()->restrictOnDelete();
            $table->string('name');
            $table->string('email')->nullable();
            $table->string('phone', 32)->nullable();
            $table->string('subject')->nullable();
            $table->text('message');
            $table->string('audience', 20)->default('general');
            $table->string('status', 20)->default('new');
            $table->string('ip_address', 45)->nullable();
            $table->foreignId('handled_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('handled_at')->nullable();
            $table->text('internal_notes')->nullable();
            $table->timestamps();

            $table->index(['company_id', 'status', 'created_at']);
        });

        // Publishing controls on the ERP job demand (§162).
        Schema::table('job_demands', function (Blueprint $table) {
            $table->boolean('is_public')->default(false)->after('status');
            $table->string('public_slug', 160)->nullable()->after('is_public');
            $table->timestamp('publish_at')->nullable()->after('public_slug');
            $table->timestamp('unpublish_at')->nullable()->after('publish_at');
            $table->boolean('is_featured')->default(false)->after('unpublish_at');
            $table->boolean('is_urgent')->default(false)->after('is_featured');
            // Whether the employer's name may be shown publicly.
            $table->boolean('show_employer_publicly')->default(false)->after('is_urgent');
            $table->text('public_summary')->nullable()->after('show_employer_publicly');
            $table->text('public_responsibilities')->nullable()->after('public_summary');
            $table->string('seo_title')->nullable()->after('public_responsibilities');
            $table->string('seo_description', 320)->nullable()->after('seo_title');
            $table->foreignId('job_category_id')->nullable()->after('seo_description')
                ->constrained()->nullOnDelete();

            $table->unique(['company_id', 'public_slug']);
            $table->index(['is_public', 'publish_at']);
        });
    }

    public function down(): void
    {
        Schema::table('job_demands', function (Blueprint $table) {
            $table->dropConstrainedForeignId('job_category_id');
            $table->dropColumn([
                'is_public', 'public_slug', 'publish_at', 'unpublish_at', 'is_featured',
                'is_urgent', 'show_employer_publicly', 'public_summary',
                'public_responsibilities', 'seo_title', 'seo_description',
            ]);
        });

        Schema::dropIfExists('contact_enquiries');
        Schema::dropIfExists('site_faqs');
        Schema::dropIfExists('company_certificates');
        Schema::dropIfExists('success_stories');
        Schema::dropIfExists('job_categories');
        Schema::dropIfExists('destination_countries');
        Schema::dropIfExists('site_statistics');
        Schema::dropIfExists('site_contents');
    }
};
