import { z } from 'zod';

export const NOTE_STATUSES = ['draft', 'active', 'archived'] as const;
export type NoteStatus = (typeof NOTE_STATUSES)[number];

export interface NoteDoc {
  id: string;
  productId: string;
  workspaceId: string;
  userId: string;
  title: string;
  body: string;
  status: NoteStatus;
  tags: string[];
  links: string[];
  sourceType?: string;
  sourceUri?: string;
  createdAt: string;
  updatedAt: string;
  createdBy: string;
  updatedBy: string;
  agentId?: string;
  _ts?: number;
  _etag?: string;
}

export const CreateNoteSchema = z.object({
  id: z.string().min(1).max(128),
  workspaceId: z.string().min(1).max(128),
  title: z.string().min(1).max(500),
  body: z.string().min(1).max(50000),
  tags: z.array(z.string().min(1).max(64)).default([]),
  links: z.array(z.string().min(1).max(512)).default([]),
  sourceType: z.string().max(128).optional(),
  sourceUri: z.string().max(2000).optional(),
  agentId: z.string().max(128).optional(),
});

export const UpdateNoteSchema = z.object({
  title: z.string().min(1).max(500).optional(),
  body: z.string().min(1).max(50000).optional(),
  status: z.enum(NOTE_STATUSES).optional(),
  tags: z.array(z.string().min(1).max(64)).optional(),
  links: z.array(z.string().min(1).max(512)).optional(),
  sourceType: z.string().max(128).optional(),
  sourceUri: z.string().max(2000).optional(),
  agentId: z.string().max(128).optional(),
});

export const ListNotesQuerySchema = z.object({
  workspaceId: z.string().min(1).max(128).optional(),
  status: z.enum(NOTE_STATUSES).optional(),
  tag: z.string().min(1).max(64).optional(),
  search: z.string().min(1).max(200).optional(),
  limit: z.coerce.number().int().min(1).max(100).default(50),
  offset: z.coerce.number().int().min(0).default(0),
});

export type CreateNoteInput = z.infer<typeof CreateNoteSchema>;
export type UpdateNoteInput = z.infer<typeof UpdateNoteSchema>;
export type ListNotesQuery = z.infer<typeof ListNotesQuerySchema>;
