/**
 * Zod schemas for run-related API endpoints
 */

import { z } from 'zod';

/**
 * Schema for creating a new run (POST /api/runs)
 */
export const createRunSchema = z.object({
  name: z.string().min(1, 'Name is required').max(200, 'Name too long'),
  input: z.unknown().optional(), // Accept any JSON for input
  clientToken: z.string().min(1).max(200).optional(),
  tags: z.array(z.string().min(1).max(50)).max(20, 'Too many tags').optional(),
});

export type CreateRunInput = z.infer<typeof createRunSchema>;

/**
 * Schema for listing runs (GET /api/runs query parameters)
 */
export const listRunsQuerySchema = z.object({
  status: z.enum(['created', 'running', 'completed', 'failed']).optional(),
  tag: z.string().max(50).optional(),
  clientToken: z.string().max(200).optional(),
  limit: z
    .union([z.string(), z.number()])
    .optional()
    .default('100')
    .transform((v) => typeof v === 'number' ? v : parseInt(v, 10))
    .refine((n) => !Number.isNaN(n) && n > 0 && n <= 1000, {
      message: 'limit must be between 1 and 1000',
    }),
  offset: z
    .union([z.string(), z.number()])
    .optional()
    .default('0')
    .transform((v) => typeof v === 'number' ? v : parseInt(v, 10))
    .refine((n) => !Number.isNaN(n) && n >= 0, {
      message: 'offset must be >= 0',
    }),
});

export type ListRunsQuery = z.infer<typeof listRunsQuerySchema>;

/**
 * Schema for run ID parameter (GET /api/runs/:id)
 * Accepts both UUID format and custom run-{timestamp}-{random} format
 */
export const runIdParamSchema = z.object({
  id: z.string().refine(
    (val) => {
      // Accept UUID format
      const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
      // Accept custom format: run-{timestamp}-{random}
      const customRegex = /^run-\d+-[a-z0-9]+$/i;
      return uuidRegex.test(val) || customRegex.test(val);
    },
    { message: 'Invalid run ID format' }
  ),
});

export type RunIdParam = z.infer<typeof runIdParamSchema>;

/**
 * Schema for updating run status (PATCH /api/runs/:id/status)
 */
export const updateRunStatusSchema = z.object({
  status: z.enum(['created', 'running', 'completed', 'failed'], {
    errorMap: () => ({ message: 'status must be one of: created, running, completed, failed' }),
  }),
  output: z.unknown().optional(),
  error: z.string().optional(),
});

export type UpdateRunStatusInput = z.infer<typeof updateRunStatusSchema>;

/**
 * Schema for workspace plan request (POST /api/workspace/plan)
 */
export const workspacePlanSchema = z.object({
  prompt: z.string().min(1, 'Prompt is required').max(10000, 'Prompt too long'),
  context: z.record(z.unknown()).optional(),
  options: z.object({
    temperature: z.number().min(0).max(2).optional(),
    maxTokens: z.number().min(1).max(100000).optional(),
    model: z.string().optional(),
  }).optional(),
});

export type WorkspacePlanInput = z.infer<typeof workspacePlanSchema>;

/**
 * Schema for registry publish request (POST /api/registry/publish)
 */
export const registryPublishSchema = z.object({
  name: z.string().min(1).max(100),
  version: z.string().regex(/^\d+\.\d+\.\d+$/, 'Version must be in semver format (e.g., 1.0.0)'),
  description: z.string().max(500).optional(),
  code: z.string().min(1),
  metadata: z.record(z.unknown()).optional(),
});

export type RegistryPublishInput = z.infer<typeof registryPublishSchema>;

/**
 * Schema for privacy toggle request (POST /api/privacy/toggle)
 */
export const privacyToggleSchema = z.object({
  enabled: z.boolean(),
  level: z.enum(['strict', 'moderate', 'permissive']).optional(),
});

export type PrivacyToggleInput = z.infer<typeof privacyToggleSchema>;

