-- Update project_summary so its LEFT JOIN to `projects` goes via the
-- new operator/client/project entities (the text columns on `projects`
-- were dropped in 20260524000001). The aggregation still groups by
-- the text columns ON `time_entries` (kept as a denormalized cache);
-- this keeps every existing caller working unchanged.

drop function if exists public.project_summary(
  uuid, timestamp, timestamp, text, uuid, text, text, text, text, text, text, uuid, boolean
);

create or replace function public.project_summary(
  p_org_id uuid,
  p_start timestamp default null,
  p_end timestamp default null,
  p_status text default 'all',
  p_batch uuid default null,
  p_q text default null,
  p_project text default null,
  p_user text default null,
  p_source_email text default null,
  p_client text default null,
  p_operator text default null,
  p_imported_by uuid default null,
  p_missing_info boolean default false
)
returns table (
  operator text,
  client text,
  project text,
  row_count bigint,
  billout_seconds bigint,
  cost_usd numeric,
  income_usd numeric
)
language sql
stable
security invoker
set search_path = public
as $$
  with filtered as (
    select te.*
    from public.time_entries te
    where te.org_id = p_org_id
      and (p_start is null or te.start_at >= p_start)
      and (p_end is null or te.start_at < p_end)
      and (p_batch is null or te.import_id = p_batch)
      and (
        p_status = 'all'
        or (p_status = 'pending'
            and te.transferred_at is null
            and te.team_member_id is not null
            and te.converted_duration_seconds is not null
            and te.operator is not null
            and te.client is not null
            and te.project is not null
            and te.description is not null
            and te.end_at is not null
            and te.source_user_email is not null
            and te.converted_user is not null)
        or (p_status = 'transferred' and te.transferred_at is not null)
        or (p_status = 'blocked'
            and te.transferred_at is null
            and (te.team_member_id is null
                 or te.converted_duration_seconds is null
                 or te.operator is null
                 or te.client is null
                 or te.project is null
                 or te.description is null
                 or te.end_at is null
                 or te.source_user_email is null
                 or te.converted_user is null))
      )
      and (
        p_q is null
        or te.project ilike '%' || p_q || '%'
        or te.description ilike '%' || p_q || '%'
        or te.source_user_email ilike '%' || p_q || '%'
        or te.client ilike '%' || p_q || '%'
      )
      and (p_project is null or te.project ilike '%' || p_project || '%')
      and (p_user is null or te.converted_user ilike '%' || p_user || '%')
      and (
        p_source_email is null
        or te.source_user_email ilike '%' || p_source_email || '%'
      )
      and (p_client is null or te.client ilike '%' || p_client || '%')
      and (p_operator is null or te.operator ilike '%' || p_operator || '%')
      and (
        p_imported_by is null
        or exists (
          select 1 from public.clockify_imports ci
          where ci.id = te.import_id and ci.imported_by = p_imported_by
        )
      )
      and (
        coalesce(p_missing_info, false) = false
        or (te.operator is null or te.client is null or te.project is null)
      )
  ),
  agg as (
    select
      operator,
      client,
      project,
      count(*)::bigint as row_count,
      coalesce(sum(converted_duration_seconds), 0)::bigint as billout_seconds,
      coalesce(sum(billout_cost_usd), 0)::numeric as cost_usd
    from filtered
    group by 1, 2, 3
  )
  select
    agg.operator,
    agg.client,
    agg.project,
    agg.row_count,
    agg.billout_seconds,
    agg.cost_usd,
    p.income_usd
  from agg
  left join public.operators o
    on o.org_id = p_org_id and lower(o.name) = lower(agg.operator)
  left join public.clients c
    on c.operator_id = o.id and lower(c.name) = lower(agg.client)
  left join public.projects p
    on p.client_id = c.id and lower(p.name) = lower(agg.project)
$$;
