<?php

namespace App\Domain\Recruitment\Models;

use App\Domain\Identity\Models\User;
use App\Domain\Organization\Models\Branch;
use App\Domain\Organization\Models\Company;
use App\Domain\Recruitment\Enums\ComplianceStatus;
use App\Domain\Recruitment\Enums\EmployerStatus;
use App\Support\Concerns\HasUuid;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;

class Employer extends Model
{
    use HasFactory, HasUuid, SoftDeletes;

    protected $guarded = ['id', 'uuid'];

    protected function casts(): array
    {
        return [
            'status' => EmployerStatus::class,
            'compliance_status' => ComplianceStatus::class,
            'license_expiry' => 'date',
            'agreement_start_date' => 'date',
            'agreement_expiry_date' => 'date',
            'blacklisted_at' => 'date',
            'credit_terms_days' => 'integer',
            'rating' => 'integer',
        ];
    }

    public function company(): BelongsTo
    {
        return $this->belongsTo(Company::class);
    }

    public function branch(): BelongsTo
    {
        return $this->belongsTo(Branch::class);
    }

    public function accountManager(): BelongsTo
    {
        return $this->belongsTo(User::class, 'account_manager_id');
    }

    public function contacts(): HasMany
    {
        return $this->hasMany(EmployerContact::class);
    }

    public function primaryContact(): HasOne
    {
        return $this->hasOne(EmployerContact::class)->where('is_primary', true);
    }

    public function scopeAcceptingDemands(Builder $query): Builder
    {
        return $query->where('status', EmployerStatus::Active->value);
    }

    public function agreementHasExpired(): bool
    {
        return $this->agreement_expiry_date !== null && $this->agreement_expiry_date->isPast();
    }

    /**
     * Whether a new job demand may be raised: an active relationship, cleared
     * compliance, and a live agreement (§14, §15).
     */
    public function canAcceptNewDemand(): bool
    {
        return $this->status->canReceiveDemands()
            && $this->compliance_status->isCleared()
            && ! $this->agreementHasExpired();
    }

    /** @return array<int, string> Why a demand cannot be raised right now. */
    public function demandBlockers(): array
    {
        $blockers = [];

        if (! $this->status->canReceiveDemands()) {
            $blockers[] = "Employer status is {$this->status->label()}.";
        }

        if (! $this->compliance_status->isCleared()) {
            $blockers[] = "Compliance is {$this->compliance_status->label()}.";
        }

        if ($this->agreementHasExpired()) {
            $blockers[] = 'The recruitment agreement has expired.';
        }

        return $blockers;
    }
}
