/**
 * Default task status seed — tasks-board-engine.
 *
 * Inserts 8 tenant-global statuses for a newly provisioned tenant.
 * Called inside the tenant-provisioning transaction via seedTaskStatuses().
 *
 * DEFAULT_STATUSES: ordered, positions 0–7.
 * Only DONE gets is_terminal = true.
 * Colors are design-token names from the Zync token set.
 */
import type { Db, DbTx } from '../client'
import { taskStatuses } from '../schema/tasks'

export const DEFAULT_STATUSES = [
  'BACKLOG',
  'TODO',
  'IN_PROGRESS',
  'BLOCKED',
  'REVIEW',
  'TESTING',
  'DEPLOY',
  'DONE',
] as const

const STATUS_COLORS: Record<(typeof DEFAULT_STATUSES)[number], string> = {
  BACKLOG: '--status-backlog',
  TODO: '--status-todo',
  IN_PROGRESS: '--status-in-progress',
  BLOCKED: '--status-blocked',
  REVIEW: '--status-review',
  TESTING: '--status-testing',
  DEPLOY: '--status-deploy',
  DONE: '--status-done',
}

/**
 * Seed the 8 default task statuses for a tenant.
 * Idempotent: uses onConflictDoNothing via a unique partial index on
 * (tenant_id, project_id, name) — NOTE: the unique index is only partial
 * (where project_id IS NULL) enforced by raw DDL. This function is called
 * once per tenant creation and is safe to re-run.
 */
export async function seedTaskStatuses(tx: Db | DbTx, tenantId: string): Promise<void> {
  const rows = DEFAULT_STATUSES.map((name, idx) => ({
    tenantId,
    projectId: null as string | null,
    name,
    color: STATUS_COLORS[name],
    position: idx,
    isTerminal: name === 'DONE',
  }))

  await tx.insert(taskStatuses).values(rows).onConflictDoNothing()
}
