import { z } from 'zod';

export const WORKSPACE_ROLES = ['owner', 'editor', 'viewer'] as const;
export type WorkspaceRole = (typeof WORKSPACE_ROLES)[number];

export interface WorkspaceMember {
  userId: string;
  role: WorkspaceRole;
}

export interface WorkspaceDoc {
  id: string;
  productId: string;
  userId: string;
  name: string;
  description?: string;
  members: WorkspaceMember[];
  createdAt: string;
  updatedAt: string;
  createdBy: string;
  updatedBy: string;
  _ts?: number;
  _etag?: string;
}

const WorkspaceMemberSchema = z.object({
  userId: z.string().min(1).max(128),
  role: z.enum(WORKSPACE_ROLES),
});

export const CreateWorkspaceSchema = z.object({
  id: z.string().min(1).max(128),
  name: z.string().min(1).max(200),
  description: z.string().max(2000).optional(),
  members: z.array(WorkspaceMemberSchema).default([]),
});

export const UpdateWorkspaceSchema = z.object({
  name: z.string().min(1).max(200).optional(),
  description: z.string().max(2000).optional(),
  members: z.array(WorkspaceMemberSchema).optional(),
});

export const ListWorkspacesQuerySchema = z.object({
  limit: z.coerce.number().int().min(1).max(100).default(50),
  offset: z.coerce.number().int().min(0).default(0),
});

export type CreateWorkspaceInput = z.infer<typeof CreateWorkspaceSchema>;
export type UpdateWorkspaceInput = z.infer<typeof UpdateWorkspaceSchema>;
export type ListWorkspacesQuery = z.infer<typeof ListWorkspacesQuerySchema>;
