import { z } from 'zod';

// URL validation schema
export const urlSchema = z.string().url('Invalid URL format');

// Crawler configuration validation
export const crawlerConfigSchema = z.object({
  maxDepth: z.number().min(1).max(10).default(3),
  maxPages: z.number().min(1).max(10000).default(100),
  respectRobots: z.boolean().default(true),
  rateLimit: z.number().min(100).max(10000).default(1000),
  userAgent: z.string().min(1).default('CloneWeb-Bot/1.0'),
  timeout: z.number().min(5000).max(60000).default(30000)
});

// Viewport validation
export const viewportSchema = z.object({
  width: z.number().min(320).max(3840),
  height: z.number().min(240).max(2160),
  deviceScaleFactor: z.number().min(1).max(3).default(1),
  isMobile: z.boolean().default(false)
});

// Screenshot configuration validation
export const screenshotConfigSchema = z.object({
  viewports: z.array(viewportSchema).min(1),
  fullPage: z.boolean().default(true),
  quality: z.number().min(1).max(100).default(90),
  format: z.enum(['png', 'jpeg', 'webp']).default('png')
});

// Generation configuration validation
export const generationConfigSchema = z.object({
  framework: z.enum(['vanilla', 'react', 'vue', 'angular']).default('vanilla'),
  cssFramework: z.enum(['none', 'tailwind', 'bootstrap']).default('none'),
  optimization: z.enum(['none', 'basic', 'aggressive']).default('basic'),
  accessibility: z.boolean().default(true),
  responsive: z.boolean().default(true)
});

// User registration validation
export const userRegistrationSchema = z.object({
  email: z.string().email('Invalid email format'),
  password: z.string().min(8, 'Password must be at least 8 characters'),
  firstName: z.string().min(1).optional(),
  lastName: z.string().min(1).optional()
});

// User login validation
export const userLoginSchema = z.object({
  email: z.string().email('Invalid email format'),
  password: z.string().min(1, 'Password is required')
});

// Project creation validation
export const projectCreationSchema = z.object({
  name: z.string().min(1, 'Project name is required').max(100),
  description: z.string().max(500).optional(),
  sourceUrl: urlSchema,
  settings: z.object({
    crawlerConfig: crawlerConfigSchema.optional(),
    generationConfig: generationConfigSchema.optional()
  }).optional()
});

// Asset validation
export const assetSchema = z.object({
  originalUrl: urlSchema,
  type: z.enum(['image', 'css', 'js', 'font', 'media', 'other']),
  size: z.number().min(0),
  hash: z.string().min(1)
});

// Component validation
export const componentSchema = z.object({
  name: z.string().min(1).max(100),
  type: z.enum(['navigation', 'header', 'footer', 'card', 'form', 'button', 'modal', 'sidebar', 'hero', 'gallery', 'other']),
  html: z.string().min(1),
  css: z.string().min(1),
  javascript: z.string().optional(),
  props: z.array(z.object({
    name: z.string().min(1),
    type: z.enum(['string', 'number', 'boolean', 'object']),
    required: z.boolean(),
    defaultValue: z.any().optional()
  })).default([])
});

// Pagination validation
export const paginationSchema = z.object({
  page: z.number().min(1).default(1),
  limit: z.number().min(1).max(100).default(20)
});

// Export validation functions
export const validateUrl = (url: string) => urlSchema.parse(url);
export const validateCrawlerConfig = (config: any) => crawlerConfigSchema.parse(config);
export const validateScreenshotConfig = (config: any) => screenshotConfigSchema.parse(config);
export const validateGenerationConfig = (config: any) => generationConfigSchema.parse(config);
export const validateUserRegistration = (data: any) => userRegistrationSchema.parse(data);
export const validateUserLogin = (data: any) => userLoginSchema.parse(data);
export const validateProjectCreation = (data: any) => projectCreationSchema.parse(data);
export const validateAsset = (data: any) => assetSchema.parse(data);
export const validateComponent = (data: any) => componentSchema.parse(data);
export const validatePagination = (data: any) => paginationSchema.parse(data);