import { prisma } from '@/lib/prisma'
import { productAccessFilter } from '@/lib/product-access'

export type SprintStatusGroup = 'TO_DO' | 'IN_PROGRESS' | 'DONE'

export interface StatusCount {
  status: SprintStatusGroup
  count: number
}

// Maps REVIEW → IN_PROGRESS so donut shows 3 buckets only
function toGroup(status: string): SprintStatusGroup {
  if (status === 'DONE') return 'DONE'
  if (status === 'TO_DO') return 'TO_DO'
  return 'IN_PROGRESS'
}

export async function getSprintStatusBreakdown(userId: string): Promise<StatusCount[]> {
  const tasks = await prisma.task.findMany({
    where: {
      story: {
        sprint: {
          status: 'OPEN',
          product: productAccessFilter(userId),
        },
      },
    },
    select: { status: true },
  })

  const counts: Record<SprintStatusGroup, number> = { TO_DO: 0, IN_PROGRESS: 0, DONE: 0 }
  for (const t of tasks) {
    counts[toGroup(t.status)]++
  }

  return (Object.entries(counts) as [SprintStatusGroup, number][])
    .filter(([, count]) => count > 0)
    .map(([status, count]) => ({ status, count }))
}
