<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateCandidateProfiles extends Migration
{
    public function up(): void
    {
        $this->forge->addField([
            'id'                      => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'user_id'                 => ['type' => 'BIGINT', 'unsigned' => true],
            'profile_picture_path'    => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
            'cover_letter'            => ['type' => 'TEXT', 'null' => true],
            'headline'                => ['type' => 'VARCHAR', 'constraint' => 150, 'null' => true],
            'salary_expectation_min'  => ['type' => 'DECIMAL', 'constraint' => '12,2', 'null' => true],
            'salary_expectation_max'  => ['type' => 'DECIMAL', 'constraint' => '12,2', 'null' => true],
            'salary_currency'         => ['type' => 'VARCHAR', 'constraint' => 10, 'default' => 'PKR'],
            'preferred_locations'     => ['type' => 'JSON', 'null' => true],
            'availability_status'     => ['type' => 'ENUM', 'constraint' => ['open_to_work', 'actively_looking', 'not_looking', 'available_immediately'], 'default' => 'not_looking'],
            'notice_period_end'       => ['type' => 'DATE', 'null' => true],
            'is_public'               => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 1],
            'hide_email'              => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 0],
            'hide_mobile'             => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 0],
            'hide_employer'           => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 0],
            'profile_completion_pct'  => ['type' => 'TINYINT', 'unsigned' => true, 'default' => 0],
            'last_active_at'          => ['type' => 'TIMESTAMP', 'null' => true],
            'created_at'              => ['type' => 'TIMESTAMP', 'null' => true],
            'updated_at'              => ['type' => 'TIMESTAMP', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey('user_id');
        $this->forge->addKey('availability_status');
        $this->forge->addForeignKey('user_id', 'users', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('candidate_profiles', true, ['ENGINE' => 'InnoDB']);

        // FULLTEXT must be added via raw SQL — CI4 Forge doesn't expose it directly
        $this->db->query('ALTER TABLE candidate_profiles ADD FULLTEXT INDEX ft_candidate_headline (headline)');
    }

    public function down(): void
    {
        $this->forge->dropTable('candidate_profiles', true);
    }
}
