<?php

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

// ══════════════════════════════════════════════════════════════════
//  InPractice — CV Pool & Job Matching Tables
//
//  Privacy-first hiring feature.
//
//  How it works:
//    1. Professional fills in CV form → stored in cv_profiles
//    2. CV is visible in pool with NO personal info (anonymous)
//    3. Employer finds a match → creates a job_notifications record
//    4. Professional receives notification and decides to respond
//    5. If professional accepts → BOTH get consent alert
//    6. After mutual consent → contact details shared
//
//  Tables:
//    cv_profiles       → the anonymous CV data
//    job_listings      → employer job posts
//    job_notifications → employer sends interest to a specific CV
//    job_consents      → tracks the consent/acceptance flow
// ══════════════════════════════════════════════════════════════════

return new class extends Migration
{
    public function up(): void
    {
        // ── CV Profiles ────────────────────────────────────────
        Schema::create('cv_profiles', function (Blueprint $table) {
            $table->id();

            $table->foreignId('user_id')
                  ->constrained('users')
                  ->cascadeOnDelete();

            // ── Professional Info (shown anonymously) ──────────
            $table->string('job_title');                     // e.g. "Financial Analyst"
            $table->string('current_industry');              // their current sector
            $table->unsignedTinyInteger('years_experience'); // how many years
            $table->enum('experience_level', ['student', 'fresh', 'junior', 'mid', 'senior']);
            $table->json('skills');                          // array of skills
            $table->json('hub_ids');                         // which hubs they belong to
            $table->string('education_level');               // bachelor | master | phd | diploma
            $table->string('education_field')->nullable();   // e.g. "Finance & Accounting"
            $table->string('location_country')->default('Egypt');
            $table->string('location_city')->nullable();     // Cairo | Alexandria | Riyadh...
            $table->text('summary')->nullable();             // anonymous professional summary
            $table->json('preferred_sectors')->nullable();   // industries they want to work in
            $table->string('job_type_preference')->nullable(); // full_time | part_time | both
            $table->boolean('open_to_relocation')->default(false);

            // ── Visibility ────────────────────────────────────
            $table->boolean('is_visible')->default(true);    // in the pool or hidden

            // ── Personal Info (NEVER shown without consent) ───
            // These are already stored in users table
            // We only share users.name, users.email, users.phone after consent

            $table->timestamps();
        });

        // ── Job Listings ───────────────────────────────────────
        // Employers post available positions here
        Schema::create('job_listings', function (Blueprint $table) {
            $table->id();

            $table->foreignId('posted_by')
                  ->constrained('users')
                  ->cascadeOnDelete();

            $table->foreignId('hub_id')
                  ->constrained('hubs')
                  ->cascadeOnDelete();

            // ── Job Details ───────────────────────────────────
            $table->string('job_title');
            $table->string('company_name');                  // employer's company name
            $table->string('industry');
            $table->string('location_city')->nullable();
            $table->enum('job_type', ['full_time', 'part_time', 'contract', 'internship']);
            $table->text('description');
            $table->json('required_skills')->nullable();
            $table->unsignedTinyInteger('min_experience_years')->default(0);
            $table->string('salary_range')->nullable();      // optional, e.g. "15,000-20,000 EGP"

            // ── Status ────────────────────────────────────────
            $table->enum('status', ['active', 'filled', 'closed'])->default('active');

            $table->timestamps();
        });

        // ── Job Notifications ──────────────────────────────────
        // When employer sends interest to a specific CV profile
        Schema::create('job_notifications', function (Blueprint $table) {
            $table->id();

            $table->foreignId('job_listing_id')
                  ->constrained('job_listings')
                  ->cascadeOnDelete();

            $table->foreignId('cv_profile_id')
                  ->constrained('cv_profiles')
                  ->cascadeOnDelete();

            // The professional who owns the CV
            $table->foreignId('professional_user_id')
                  ->constrained('users')
                  ->cascadeOnDelete();

            // The employer who sent the notification
            $table->foreignId('employer_user_id')
                  ->constrained('users')
                  ->cascadeOnDelete();

            // ── Message ───────────────────────────────────────
            $table->text('message')->nullable();             // optional note from employer

            // ── Status Flow ───────────────────────────────────
            // pending   → professional has not responded yet
            // accepted  → professional wants to hear more (consent flow starts)
            // declined  → professional not interested
            // connected → both consented, contact details shared
            $table->enum('status', [
                'pending',
                'accepted',
                'declined',
                'connected',
            ])->default('pending');

            // ── Consent Tracking ──────────────────────────────
            $table->boolean('professional_consented')->default(false);
            $table->boolean('employer_consented')->default(false);
            $table->timestamp('connected_at')->nullable();   // when contact was shared

            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('job_notifications');
        Schema::dropIfExists('job_listings');
        Schema::dropIfExists('cv_profiles');
    }
};
