import { initContract } from '@ts-rest/core'; import { z } from 'zod'; import { shopInsightSchema } from '../schemas/shopkeeper-insights.schemas'; import { errorResponseSchema, paginationQuerySchema, paginationResponseSchema } from '../schemas/common.schemas'; const c = initContract(); export const shopkeeperInsightsContract = c.router({ /** List insights for the shop (device pulls for sync / offline use). */ list: { method: 'GET', path: '/shopkeeper/insights', query: z .object({ since: z.coerce.date().optional(), unreadOnly: z.coerce.boolean().optional().default(false), }) .merge(paginationQuerySchema), responses: { 200: z.object({ insights: z.array(shopInsightSchema), pagination: paginationResponseSchema, }), 401: errorResponseSchema, }, summary: 'List cloud-generated insights for device sync', }, /** Mark an insight as read (device has shown it to user). */ markRead: { method: 'PATCH', path: '/shopkeeper/insights/:id/read', pathParams: z.object({ id: z.string().uuid() }), body: z.object({}), responses: { 200: shopInsightSchema, 404: errorResponseSchema, 401: errorResponseSchema, }, summary: 'Mark insight as read', }, /** Persist how the shop owner engaged with an insight. */ engageInsight: { method: 'PATCH', path: '/shopkeeper/insights/:id/engage', pathParams: z.object({ id: z.string().uuid() }), body: z.object({ engagementType: z.enum(['acted_on', 'dismissed', 'ignored']), }), responses: { 200: shopInsightSchema, 404: errorResponseSchema, 401: errorResponseSchema, }, summary: 'Record insight engagement (acted on / dismissed / ignored)', }, /** Aggregate trade agent usage (intents, daily counts, token spend from audit logs). */ agentUsage: { method: 'GET', path: '/shopkeeper/insights/agent-usage', query: z.object({ from: z.coerce.date().optional(), to: z.coerce.date().optional(), limitDays: z.coerce.number().int().min(1).max(90).optional(), }), responses: { 200: z.object({ byIntent: z.record(z.number().int()), byDay: z.array(z.object({ date: z.string(), count: z.number().int(), tokensUsed: z.number().int() })), totalSessions: z.number().int(), totalTokensUsed: z.number().int(), }), 401: errorResponseSchema, }, summary: 'Get trade agent usage (intents, daily counts, token spend)', }, });