<?php

namespace App\Entities;

use CodeIgniter\Entity\Entity;

class Job extends Entity
{
    protected $attributes = [
        'id'                    => null,
        'agency_id'             => null,
        'posted_by_user_id'     => null,
        'title'                 => null,
        'description'           => null,
        'department'            => null,
        'category_id'           => null,
        'experience_level'      => 'mid',
        'salary_min'            => null,
        'salary_max'            => null,
        'salary_currency'       => 'PKR',
        'is_salary_visible'     => 1,
        'location'              => null,
        'is_remote'             => 0,
        'is_hybrid'             => 0,
        'required_skills'       => null,
        'employment_type'       => 'full_time',
        'education_requirement' => null,
        'benefits'              => null,
        'status'                => 'draft',
        'is_featured'           => 0,
        'is_urgent'             => 0,
        'views_count'           => 0,
        'expires_at'            => null,
        'seo_slug'              => null,
        'meta_title'            => null,
        'meta_description'      => null,
    ];

    protected $casts = [
        'id'                => 'integer',
        'agency_id'         => 'integer',
        'posted_by_user_id' => 'integer',
        'required_skills'   => 'json-array',
        'is_remote'         => 'boolean',
        'is_hybrid'         => 'boolean',
        'is_featured'       => 'boolean',
        'is_urgent'         => 'boolean',
        'is_salary_visible' => 'boolean',
    ];

    protected $dates = ['expires_at', 'created_at', 'updated_at', 'deleted_at'];

    public function isPublished(): bool
    {
        return $this->status === 'published';
    }

    public function isExpired(): bool
    {
        return $this->expires_at !== null && strtotime($this->expires_at) < time();
    }

    public function salaryDisplay(): ?string
    {
        if (! $this->is_salary_visible || (! $this->salary_min && ! $this->salary_max)) {
            return null;
        }
        if ($this->salary_min && $this->salary_max) {
            return "{$this->salary_currency} {$this->salary_min} - {$this->salary_max}";
        }
        return "{$this->salary_currency} " . ($this->salary_min ?: $this->salary_max) . '+';
    }
}
