import { z } from 'zod'

const utcDateTime = z.string().datetime({ offset: true })

export const holdingSchema = z.object({
  id: z.string().uuid(),
  securityId: z.string().uuid(),
  quantity: z.number().int(),
  weightedAvgCost: z.number(),
  totalCostBasis: z.number(),
  marketValue: z.number().nullable(),
  unrealizedGainLoss: z.number().nullable(),
  updatedAt: utcDateTime,
  correlationId: z.string().uuid()
})

export const holdingsResponseSchema = z.object({
  items: z.array(holdingSchema),
  total: z.number().int().nonnegative(),
  pages: z.number().int().positive()
})

export const mismatchSchema = z.object({
  id: z.string().uuid(),
  tradeId: z.string().uuid(),
  holdingId: z.string().uuid(),
  mismatchReason: z.string().nullable(),
  quantityBefore: z.number().int(),
  quantityAfter: z.number().int(),
  costBasisDelta: z.number(),
  detectedAt: utcDateTime
})

export const mismatchesResponseSchema = z.object({
  items: z.array(mismatchSchema),
  total: z.number().int().nonnegative(),
  pages: z.number().int().positive()
})

export type HoldingsResponse = z.infer<typeof holdingsResponseSchema>
export type MismatchesResponse = z.infer<typeof mismatchesResponseSchema>
