<?php

namespace App\Services\Communications;

use App\Enum\Communication\AgentAvailabilityStatus;
use App\Models\Dialler\AgentStatus;

class AgentPresenceService {
    public function isAvailable(int $solicitorId, int $agentId): bool {
        return AgentStatus::query()
            ->where('solicitor_id', $solicitorId)
            ->where('agent_id', $agentId)
            ->where('status', AgentAvailabilityStatus::Available->value)
            // The agent must still belong to this solicitor's workspace, so that
            // assigned-agent routing and call transfers can never connect to an
            // agent in another firm through a stale presence row.
            ->whereHas('agent', fn ($q) => $q->where('solicitor_id', $solicitorId))
            ->exists();
    }
}