-- name: CreateTransaction :one
INSERT INTO financial_transactions (
  fot_id, data_cobranca, tipo_evento, tipo_servico, professional_name, 
  whatsapp, cpf, tabela_free, valor_free, valor_extra, descricao_extra, 
  total_pagar, data_pagamento, pgto_ok, profissional_id, regiao
) VALUES (
  $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, @regiao
) RETURNING *;

-- name: ListTransactionsByFot :many
SELECT * FROM financial_transactions 
WHERE fot_id = $1 AND regiao = @regiao
ORDER BY data_cobranca DESC;

-- name: ListTransactions :many
SELECT t.*, f.fot as fot_numero
FROM financial_transactions t
LEFT JOIN cadastro_fot f ON t.fot_id = f.id
WHERE t.regiao = @regiao
ORDER BY t.data_cobranca DESC NULLS LAST;

-- name: SumTotalByFot :one
SELECT COALESCE(SUM(total_pagar), 0)::NUMERIC 
FROM financial_transactions 
WHERE fot_id = $1 AND regiao = @regiao;

-- name: UpdateTransaction :one
UPDATE financial_transactions SET 
  fot_id = $2, data_cobranca = $3, tipo_evento = $4, tipo_servico = $5, 
  professional_name = $6, whatsapp = $7, cpf = $8, tabela_free = $9, 
  valor_free = $10, valor_extra = $11, descricao_extra = $12, 
  total_pagar = $13, data_pagamento = $14, pgto_ok = $15,
  atualizado_em = NOW()
WHERE id = $1 AND regiao = @regiao
RETURNING *;

-- name: DeleteTransaction :exec
DELETE FROM financial_transactions WHERE id = $1 AND regiao = @regiao;

-- name: GetTransaction :one
SELECT * FROM financial_transactions WHERE id = $1 AND regiao = @regiao;


-- name: ListTransactionsByProfessional :many
SELECT t.*, f.fot as fot_numero, e.nome as empresa_nome, c.nome as curso_nome
FROM financial_transactions t
LEFT JOIN cadastro_fot f ON t.fot_id = f.id
LEFT JOIN empresas e ON f.empresa_id = e.id
LEFT JOIN cursos c ON f.curso_id = c.id
WHERE 
  t.profissional_id = $1 
  OR (
     $2::text <> '' AND 
     REGEXP_REPLACE(t.cpf, '\D', '', 'g') = REGEXP_REPLACE($2, '\D', '', 'g')
  ) AND t.regiao = @regiao
ORDER BY t.data_cobranca DESC;

-- name: ListTransactionsPaginated :many
SELECT t.*, f.fot as fot_numero,
       e.nome as empresa_nome,
       c.nome as curso_nome,
       a.ano_semestre as ano_formatura,
       f.instituicao as instituicao_nome
FROM financial_transactions t
LEFT JOIN cadastro_fot f ON t.fot_id = f.id
LEFT JOIN empresas e ON f.empresa_id = e.id
LEFT JOIN cursos c ON f.curso_id = c.id
LEFT JOIN anos_formaturas a ON f.ano_formatura_id = a.id
WHERE t.regiao = @regiao
ORDER BY t.data_cobranca DESC NULLS LAST
LIMIT $1 OFFSET $2;

-- name: ListTransactionsPaginatedFiltered :many
SELECT t.*, f.fot as fot_numero,
       e.nome as empresa_nome,
       c.nome as curso_nome,
       a.ano_semestre as ano_formatura,
       f.instituicao as instituicao_nome
FROM financial_transactions t
LEFT JOIN cadastro_fot f ON t.fot_id = f.id
LEFT JOIN empresas e ON f.empresa_id = e.id
LEFT JOIN cursos c ON f.curso_id = c.id
LEFT JOIN anos_formaturas a ON f.ano_formatura_id = a.id
WHERE 
  t.regiao = @regiao AND
  (@fot::text = '' OR CAST(f.fot AS TEXT) ILIKE '%' || @fot || '%') AND
  (@data::text = '' OR CAST(t.data_cobranca AS TEXT) ILIKE '%' || @data || '%') AND
  (@evento::text = '' OR t.tipo_evento ILIKE '%' || @evento || '%') AND
  (@servico::text = '' OR t.tipo_servico ILIKE '%' || @servico || '%') AND
  (@nome::text = '' OR t.professional_name ILIKE '%' || @nome || '%') AND
  (@curso::text = '' OR c.nome ILIKE '%' || @curso || '%') AND
  (@instituicao::text = '' OR f.instituicao ILIKE '%' || @instituicao || '%') AND
  (@ano::text = '' OR CAST(a.ano_semestre AS TEXT) ILIKE '%' || @ano || '%') AND
  (@empresa::text = '' OR e.nome ILIKE '%' || @empresa || '%') AND
  -- Date Range Logic
  (@start_date::text = '' OR t.data_cobranca >= CAST(@start_date AS DATE)) AND
  (@end_date::text = '' OR t.data_cobranca <= CAST(@end_date AS DATE)) AND
  -- Weekend Logic (0=Sunday, 6=Saturday)
  (@include_weekends::boolean = true OR EXTRACT(DOW FROM t.data_cobranca) NOT IN (0, 6))
ORDER BY t.data_cobranca DESC NULLS LAST
LIMIT $1 OFFSET $2;

-- name: CountTransactions :one
SELECT COUNT(*) FROM financial_transactions WHERE regiao = @regiao;

-- name: CountTransactionsFiltered :one
SELECT COUNT(*)
FROM financial_transactions t
LEFT JOIN cadastro_fot f ON t.fot_id = f.id
LEFT JOIN empresas e ON f.empresa_id = e.id
LEFT JOIN cursos c ON f.curso_id = c.id
LEFT JOIN anos_formaturas a ON f.ano_formatura_id = a.id
WHERE 
  t.regiao = @regiao AND
  (@fot::text = '' OR CAST(f.fot AS TEXT) ILIKE '%' || @fot || '%') AND
  (@data::text = '' OR CAST(t.data_cobranca AS TEXT) ILIKE '%' || @data || '%') AND
  (@evento::text = '' OR t.tipo_evento ILIKE '%' || @evento || '%') AND
  (@servico::text = '' OR t.tipo_servico ILIKE '%' || @servico || '%') AND
  (@nome::text = '' OR t.professional_name ILIKE '%' || @nome || '%') AND
  (@curso::text = '' OR c.nome ILIKE '%' || @curso || '%') AND
  (@instituicao::text = '' OR f.instituicao ILIKE '%' || @instituicao || '%') AND
  (@ano::text = '' OR CAST(a.ano_semestre AS TEXT) ILIKE '%' || @ano || '%') AND
  (@empresa::text = '' OR e.nome ILIKE '%' || @empresa || '%') AND
  -- Date Range Logic
  (@start_date::text = '' OR t.data_cobranca >= CAST(@start_date AS DATE)) AND
  (@end_date::text = '' OR t.data_cobranca <= CAST(@end_date AS DATE)) AND
  -- Weekend Logic
  (@include_weekends::boolean = true OR EXTRACT(DOW FROM t.data_cobranca) NOT IN (0, 6));
