-- Performance: index foreign-key / filter columns.
-- Postgres does not auto-index FK columns. These back the RLS policy subqueries
-- (task_id IN (… join projects …)) and the app's eq/in filters on these columns.
-- All additive and safe; no behavior change.

create index if not exists tasks_project_id_idx        on public.tasks (project_id);
create index if not exists tasks_assigned_to_idx        on public.tasks (assigned_to);
create index if not exists tasks_status_idx             on public.tasks (status);
create index if not exists tasks_submitted_at_idx       on public.tasks (submitted_at desc);

create index if not exists submissions_task_id_idx      on public.submissions (task_id);
create index if not exists submissions_submitted_by_idx on public.submissions (submitted_by);
create index if not exists submissions_submitted_at_idx on public.submissions (submitted_at desc);

create index if not exists projects_company_id_idx      on public.projects (company_id);

-- deliveries.submission_id already covered by a UNIQUE index.
create index if not exists delivery_files_delivery_id_idx     on public.delivery_files (delivery_id);
create index if not exists submission_files_submission_id_idx on public.submission_files (submission_id);

create index if not exists project_members_project_id_idx on public.project_members (project_id);
create index if not exists project_members_profile_id_idx on public.project_members (profile_id);

create index if not exists company_members_profile_id_idx on public.company_members (profile_id);
create index if not exists company_members_company_id_idx on public.company_members (company_id);

create index if not exists activity_log_task_id_idx     on public.activity_log (task_id);
create index if not exists activity_log_created_at_idx  on public.activity_log (created_at desc);

create index if not exists invoice_items_invoice_id_idx on public.invoice_items (invoice_id);
create index if not exists invoice_items_task_id_idx    on public.invoice_items (task_id);
create index if not exists invoices_company_id_idx      on public.invoices (company_id);
