/**
 * Tracker item types — bugs, feature requests, tasks.
 * Product-agnostic: every item carries a productId for multi-product support.
 */

import { z } from 'zod';

export const ITEM_TYPES = ['bug', 'feature', 'task'] as const;
export const ITEM_STATUSES = ['open', 'in_progress', 'done', 'closed', 'wont_fix'] as const;
export const ITEM_PRIORITIES = ['critical', 'high', 'medium', 'low'] as const;
export const ITEM_SOURCES = ['internal', 'user_submitted', 'auto_detected'] as const;
export const ITEM_VISIBILITIES = ['internal', 'public'] as const;

export type ItemType = (typeof ITEM_TYPES)[number];
export type ItemStatus = (typeof ITEM_STATUSES)[number];
export type ItemPriority = (typeof ITEM_PRIORITIES)[number];
export type ItemSource = (typeof ITEM_SOURCES)[number];
export type ItemVisibility = (typeof ITEM_VISIBILITIES)[number];

export const PRIORITY_ORDER: Record<string, number> = { critical: 0, high: 1, medium: 2, low: 3 };

export interface TrackerItemDoc {
  id: string;
  productId: string;
  type: ItemType;
  status: ItemStatus;
  priority: ItemPriority;
  title: string;
  description: string;
  labels: string[];
  assignee: string | null;
  reportedBy: string;
  source: ItemSource;
  visibility: ItemVisibility;
  voteCount: number;
  commentCount: number;
  priorityOrder: number;
  targetRelease: string | null;
  createdAt: string;
  updatedAt: string;
}

export const CreateItemSchema = z.object({
  productId: z.string().min(1).optional(),
  type: z.enum(ITEM_TYPES),
  priority: z.enum(ITEM_PRIORITIES).default('medium'),
  title: z.string().min(1).max(500),
  description: z.string().default(''),
  labels: z.array(z.string()).default([]),
  assignee: z.string().nullable().default(null),
  source: z.enum(ITEM_SOURCES).default('internal'),
  visibility: z.enum(ITEM_VISIBILITIES).default('internal'),
  targetRelease: z.string().nullable().default(null),
});

export const UpdateItemSchema = z.object({
  type: z.enum(ITEM_TYPES).optional(),
  priority: z.enum(ITEM_PRIORITIES).optional(),
  title: z.string().min(1).max(500).optional(),
  description: z.string().optional(),
  labels: z.array(z.string()).optional(),
  assignee: z.string().nullable().optional(),
  visibility: z.enum(ITEM_VISIBILITIES).optional(),
  targetRelease: z.string().nullable().optional(),
});

export const UpdateStatusSchema = z.object({
  status: z.enum(ITEM_STATUSES),
});

export const ListItemsQuerySchema = z.object({
  productId: z.string().optional(),
  type: z.enum(ITEM_TYPES).optional(),
  status: z.enum(ITEM_STATUSES).optional(),
  priority: z.enum(ITEM_PRIORITIES).optional(),
  label: z.string().optional(),
  assignee: z.string().optional(),
  visibility: z.enum(ITEM_VISIBILITIES).optional(),
  q: z.string().optional(),
  sortBy: z.enum(['createdAt', 'updatedAt', 'voteCount', 'priority']).default('createdAt'),
  sortOrder: z.enum(['asc', 'desc']).default('desc'),
  limit: z.coerce.number().min(1).max(100).default(50),
  offset: z.coerce.number().min(0).default(0),
});

export type CreateItemInput = z.infer<typeof CreateItemSchema>;
export type UpdateItemInput = z.infer<typeof UpdateItemSchema>;
export type ListItemsQuery = z.infer<typeof ListItemsQuerySchema>;
