import { z } from 'zod'

/**
 * Request Reporting API Schemas
 *
 * Request/response validation for /api/requests and /api/external/requests surfaces.
 * Used by both the API (routes.ts, external-routes.ts) and the CLI.
 *
 * Table mapping:
 *   reported_requests -> RequestSchemas
 */

// ---------------------------------------------------------------------------
// Enum literals (must match DB CHECK constraints exactly)
// ---------------------------------------------------------------------------

export const RequestSeverityEnum = z.enum(['critical', 'warning', 'info'])
export const RequestCategoryEnum = z.enum([
  'ui_bug',
  'data_inconsistency',
  'performance',
  'error_pattern',
  'configuration',
  'feature_request',
  'api_design',
  'documentation',
  'integration_request',
  'other'
])
export const RequestStatusEnum = z.enum(['open', 'investigating', 'resolved', 'wont_fix'])
export const RequestSourceEnum = z.enum(['agent', 'cli', 'api', 'webhook', 'user', 'external'])
export const RequestTypeEnum = z.enum(['bug', 'feature', 'question', 'other'])

// ---------------------------------------------------------------------------
// Input schemas
// ---------------------------------------------------------------------------

/**
 * Body for POST /api/requests and POST /api/external/requests.
 * `source` and `organization_id` are server-set — never accepted from the client.
 */
export const CreateRequestInputSchema = z
  .object({
    type: RequestTypeEnum,
    title: z.string().min(1),
    description: z.string().min(1),
    severity: RequestSeverityEnum,
    category: RequestCategoryEnum,
    affected_page: z.string().nullish(),
    evidence: z.record(z.string(), z.unknown()).nullish(),
    context: z.record(z.string(), z.unknown()).nullish(),
    project_id: z.string().uuid().nullish(),
    task_id: z.string().uuid().nullish()
  })
  .strict()

export const ListRequestsQuerySchema = z.object({
  status: RequestStatusEnum.optional(),
  severity: RequestSeverityEnum.optional(),
  project_id: z.string().uuid().optional(),
  limit: z.coerce.number().int().min(1).max(200).default(50)
})

// ---------------------------------------------------------------------------
// Bundled export (mirrors ProjectSchemas pattern)
// ---------------------------------------------------------------------------

export const RequestSchemas = {
  CreateRequestInput: CreateRequestInputSchema,
  ListRequestsQuery: ListRequestsQuerySchema
}

// ---------------------------------------------------------------------------
// Inferred types
// ---------------------------------------------------------------------------

export type RequestSeverity = z.infer<typeof RequestSeverityEnum>
export type RequestCategory = z.infer<typeof RequestCategoryEnum>
export type RequestStatus = z.infer<typeof RequestStatusEnum>
export type RequestSource = z.infer<typeof RequestSourceEnum>
export type RequestType = z.infer<typeof RequestTypeEnum>
export type CreateRequestInput = z.infer<typeof CreateRequestInputSchema>
export type ListRequestsQuery = z.infer<typeof ListRequestsQuerySchema>
