-- name: CreateProfissional :one
INSERT INTO cadastro_profissionais (
    usuario_id, nome, funcao_profissional_id, endereco, cidade, uf, whatsapp,
    cpf_cnpj_titular, banco, agencia, conta_pix, carro_disponivel,
    tem_estudio, qtd_estudio, tipo_cartao, observacao, qual_tec,
    educacao_simpatia, desempenho_evento, disp_horario, media,
    tabela_free, extra_por_equipamento, equipamentos, email, avatar_url
) VALUES (
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15,
    $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26
) RETURNING *;

-- name: GetProfissionalByUsuarioID :one
SELECT p.*, 
    COALESCE(
        (SELECT json_agg(json_build_object('id', f.id, 'nome', f.nome))
         FROM profissionais_funcoes_junction pfj
         JOIN funcoes_profissionais f ON pfj.funcao_id = f.id
         WHERE pfj.profissional_id = p.id
        ), '[]'::json
    ) as functions
FROM cadastro_profissionais p
WHERE p.usuario_id = $1 LIMIT 1;

-- name: GetProfissionalByID :one
SELECT p.*, 
    COALESCE(
        (SELECT json_agg(json_build_object('id', f.id, 'nome', f.nome))
         FROM profissionais_funcoes_junction pfj
         JOIN funcoes_profissionais f ON pfj.funcao_id = f.id
         WHERE pfj.profissional_id = p.id
        ), '[]'::json
    ) as functions
FROM cadastro_profissionais p
WHERE p.id = $1 LIMIT 1;

-- name: ListProfissionais :many
SELECT p.*, u.email as usuario_email,
    COALESCE(
        (SELECT json_agg(json_build_object('id', f.id, 'nome', f.nome))
         FROM profissionais_funcoes_junction pfj
         JOIN funcoes_profissionais f ON pfj.funcao_id = f.id
         WHERE pfj.profissional_id = p.id
        ), '[]'::json
    ) as functions
FROM cadastro_profissionais p
LEFT JOIN usuarios u ON p.usuario_id = u.id
ORDER BY p.nome;

-- name: UpdateProfissional :one
UPDATE cadastro_profissionais
SET
    nome = $2,
    funcao_profissional_id = $3,
    endereco = $4,
    cidade = $5,
    uf = $6,
    whatsapp = $7,
    cpf_cnpj_titular = $8,
    banco = $9,
    agencia = $10,
    conta_pix = $11,
    carro_disponivel = $12,
    tem_estudio = $13,
    qtd_estudio = $14,
    tipo_cartao = $15,
    observacao = $16,
    qual_tec = $17,
    educacao_simpatia = $18,
    desempenho_evento = $19,
    disp_horario = $20,
    media = $21,
    tabela_free = $22,
    extra_por_equipamento = $23,
    equipamentos = $24,
    avatar_url = $25,
    email = $26,
    atualizado_em = NOW()
WHERE id = $1
RETURNING *;

-- name: DeleteProfissional :exec
DELETE FROM cadastro_profissionais
WHERE id = $1;

-- name: SearchProfissionais :many
SELECT p.*, 
    COALESCE(
        (SELECT json_agg(json_build_object('id', f.id, 'nome', f.nome))
         FROM profissionais_funcoes_junction pfj
         JOIN funcoes_profissionais f ON pfj.funcao_id = f.id
         WHERE pfj.profissional_id = p.id
        ), '[]'::json
    ) as functions
FROM cadastro_profissionais p
WHERE p.nome ILIKE '%' || $1 || '%'
ORDER BY p.nome
LIMIT 20;

-- name: SearchProfissionaisByFunction :many
SELECT p.*, 
    COALESCE(
        (SELECT json_agg(json_build_object('id', f2.id, 'nome', f2.nome))
         FROM profissionais_funcoes_junction pfj2
         JOIN funcoes_profissionais f2 ON pfj2.funcao_id = f2.id
         WHERE pfj2.profissional_id = p.id
        ), '[]'::json
    ) as functions
FROM cadastro_profissionais p
WHERE (p.nome ILIKE '%' || $1 || '%')
  AND (
      EXISTS (
        SELECT 1 
        FROM profissionais_funcoes_junction pfj
        JOIN funcoes_profissionais f ON pfj.funcao_id = f.id
        WHERE pfj.profissional_id = p.id AND f.nome = $2
      )
      OR 
      p.funcao_profissional_id = (SELECT id FROM funcoes_profissionais WHERE nome = $2 LIMIT 1)
  )
ORDER BY p.nome
LIMIT 20;

-- name: AddFunctionToProfessional :exec
INSERT INTO profissionais_funcoes_junction (profissional_id, funcao_id)
VALUES ($1, $2)
ON CONFLICT DO NOTHING;

-- name: ClearProfessionalFunctions :exec
DELETE FROM profissionais_funcoes_junction WHERE profissional_id = $1;

-- name: DeleteProfessionalFunctions :exec
DELETE FROM profissionais_funcoes_junction WHERE profissional_id = $1;

-- name: GetProfissionalByCPF :one
SELECT p.*, 
    COALESCE(
        (SELECT json_agg(json_build_object('id', f.id, 'nome', f.nome))
         FROM profissionais_funcoes_junction pfj
         JOIN funcoes_profissionais f ON pfj.funcao_id = f.id
         WHERE pfj.profissional_id = p.id
        ), '[]'::json
    ) as functions
FROM cadastro_profissionais p
WHERE p.cpf_cnpj_titular = $1 LIMIT 1;

-- name: LinkUserToProfessional :exec
UPDATE cadastro_profissionais SET usuario_id = $2 WHERE id = $1;

