-- V2.6 hot query indexes for performance harness, query budget audit,
-- and background Xiaobao summary refresh scans.

CREATE INDEX IF NOT EXISTS "projects_product_created_at_idx"
  ON "projects"("product_id", "created_at" DESC);

CREATE INDEX IF NOT EXISTS "versions_product_created_at_idx"
  ON "versions"("product_id", "created_at" DESC);

CREATE INDEX IF NOT EXISTS "versions_product_project_created_at_idx"
  ON "versions"("product_id", "project_id", "created_at" DESC);

CREATE INDEX IF NOT EXISTS "version_plans_owner_open_due_idx"
  ON "version_plans"("owner_id", "expected_end_at" ASC, "updated_at" DESC)
  WHERE "status" <> 'completed';

CREATE INDEX IF NOT EXISTS "dev_tasks_assignee_open_priority_idx"
  ON "dev_tasks"("assignee_id", "priority" ASC, "updated_at" DESC)
  WHERE "status" <> 'submitted';

CREATE INDEX IF NOT EXISTS "test_cases_assignee_open_priority_idx"
  ON "test_cases"("assignee_id", "priority" ASC, "updated_at" DESC)
  WHERE "status" NOT IN ('passed', 'failed', 'blocked');

CREATE INDEX IF NOT EXISTS "bugs_version_status_priority_updated_at_idx"
  ON "bugs"("version_id", "status" ASC, "priority" ASC, "updated_at" DESC);

CREATE INDEX IF NOT EXISTS "bugs_assignee_open_priority_idx"
  ON "bugs"("assignee_id", "priority" ASC, "updated_at" DESC)
  WHERE "status" IN ('open', 'fixing', 'fixed', 'verifying');

CREATE INDEX IF NOT EXISTS "test_cases_version_round_status_updated_at_desc_idx"
  ON "test_cases"("version_id", "round_no" DESC, "status" ASC, "updated_at" DESC);

CREATE INDEX IF NOT EXISTS "xiaobao_risk_summaries_warning_score_idx"
  ON "xiaobao_risk_summaries"("risk_score" DESC, "updated_at" DESC)
  WHERE "risk_level" <> 'on_track';

CREATE INDEX IF NOT EXISTS "xiaobao_risk_summaries_dirty_updated_at_idx"
  ON "xiaobao_risk_summaries"("updated_at" ASC)
  WHERE "dirty" = true;

CREATE INDEX IF NOT EXISTS "work_activities_version_occurred_at_idx"
  ON "work_activities"("version_id", "occurred_at" DESC);
