import { z } from 'zod';

// PostgreSQL configuration
export const postgresConfigSchema = z.object({
  enabled: z.boolean().default(true),
  host: z.string().default('localhost'),
  port: z.number().default(5432),
  database: z.string(),
  user: z.string(),
  password: z.string(),
  ssl: z.boolean().default(false),
  poolSize: z.number().min(1).max(100).default(10),
  connectionTimeout: z.number().default(30000),
  idleTimeout: z.number().default(10000),
});

// QuestDB configuration
export const questdbConfigSchema = z.object({
  enabled: z.boolean().default(true),
  host: z.string().default('localhost'),
  ilpPort: z.number().default(9009),
  httpPort: z.number().default(9000),
  pgPort: z.number().default(8812),
  database: z.string().default('questdb'),
  user: z.string().optional(), // No default - QuestDB doesn't require auth by default
  password: z.string().optional(), // No default - QuestDB doesn't require auth by default
  bufferSize: z.number().default(65536),
  flushInterval: z.number().default(1000),
});

// MongoDB configuration
export const mongodbConfigSchema = z.object({
  enabled: z.boolean().default(true),
  uri: z.string().url(), // URI is required and contains all connection info
  database: z.string(), // Database name for reference
  poolSize: z.number().min(1).max(100).default(10),
  // Optional fields for cases where URI parsing might fail
  host: z.string().default('localhost').optional(),
  port: z.number().default(27017).optional(),
  user: z.string().optional(),
  password: z.string().optional(),
  authSource: z.string().default('admin').optional(),
  replicaSet: z.string().optional(),
});

// Dragonfly/Redis configuration
export const dragonflyConfigSchema = z.object({
  enabled: z.boolean().default(true),
  host: z.string().default('localhost'),
  port: z.number().default(6379),
  password: z.string().optional(),
  db: z.number().min(0).max(15).default(0),
  keyPrefix: z.string().optional(),
  ttl: z.number().optional(),
  maxRetries: z.number().default(3),
  retryDelay: z.number().default(100),
});

// Combined database configuration
export const databaseConfigSchema = z.object({
  postgres: postgresConfigSchema,
  questdb: questdbConfigSchema,
  mongodb: mongodbConfigSchema,
  dragonfly: dragonflyConfigSchema,
});
