-- name: GetProfissionaisDisponiveisLogistica :many
SELECT p.*, f.nome as funcao_nome
FROM cadastro_profissionais p
LEFT JOIN funcoes_profissionais f ON p.funcao_profissional_id = f.id
WHERE p.regiao = $1
  AND ($2::text = '' OR p.nome ILIKE '%' || $2 || '%')
  AND NOT EXISTS (
      SELECT 1 FROM convites_diarios cd 
      WHERE cd.profissional_id = p.id 
        AND cd.data = CAST($3 AS DATE) 
        AND cd.status IN ('PENDENTE', 'ACEITO')
  )
ORDER BY p.nome ASC
LIMIT $4 OFFSET $5;

-- name: CountProfissionaisDisponiveisLogistica :one
SELECT COUNT(p.id)
FROM cadastro_profissionais p
WHERE p.regiao = $1
  AND ($2::text = '' OR p.nome ILIKE '%' || $2 || '%')
  AND NOT EXISTS (
      SELECT 1 FROM convites_diarios cd 
      WHERE cd.profissional_id = p.id 
        AND cd.data = CAST($3 AS DATE) 
        AND cd.status IN ('PENDENTE', 'ACEITO')
  );
