<?php

namespace App\Entities;

use CodeIgniter\Entity\Entity;

class CandidateProfile extends Entity
{
    protected $attributes = [
        'id'                     => null,
        'user_id'                => null,
        'profile_picture_path'   => null,
        'cover_letter'           => null,
        'headline'               => null,
        'about'                  => null,
        'video_intro_url'        => null,
        'salary_expectation_min' => null,
        'salary_expectation_max' => null,
        'current_salary'         => null,
        'salary_currency'        => 'PKR',
        'preferred_locations'    => null,
        'work_preference'        => 'any',
        'preferred_employment_type' => 'any',
        'career_level'           => null,
        'industry_preferences'   => null,
        'availability_status'    => 'not_looking',
        'notice_period_end'      => null,
        'is_public'              => 1,
        'hide_email'             => 0,
        'hide_mobile'            => 0,
        'hide_employer'          => 0,
        'is_verified'            => 0,
        'profile_completion_pct' => 0,
        'last_active_at'         => null,
    ];

    protected $casts = [
        'id'                  => 'integer',
        'user_id'             => 'integer',
        'preferred_locations' => 'json-array',
        'industry_preferences'=> 'json-array',
        'is_public'           => 'boolean',
        'hide_email'          => 'boolean',
        'hide_mobile'         => 'boolean',
        'hide_employer'       => 'boolean',
        'is_verified'         => 'boolean',
    ];

    protected $dates = ['notice_period_end', 'last_active_at', 'created_at', 'updated_at'];

    /**
     * Applies privacy controls to a serialized profile for a viewer who has
     * NOT unlocked this candidate. Called by the API resource layer — never
     * trust the frontend to hide fields, per 09-security-plan.md §5.
     */
    public function redactedFor(bool $viewerHasUnlocked): array
    {
        $array = $this->toArray();

        if (! $viewerHasUnlocked) {
            if ($this->hide_email) {
                unset($array['email']);
            }
            if ($this->hide_mobile) {
                unset($array['phone']);
            }
            if ($this->hide_employer) {
                unset($array['current_employer']);
            }
            // Contact fields are always stripped for non-unlocked viewers,
            // regardless of hide_* flags — those flags control visibility
            // even to unlocked viewers; unlocked-or-not is the outer gate.
            unset($array['email'], $array['phone']);
        }

        return $array;
    }
}
