import { z } from 'zod';

// Base provider configuration
export const baseProviderConfigSchema = z.object({
  name: z.string(),
  enabled: z.boolean().default(true),
  priority: z.number().default(0),
  rateLimit: z
    .object({
      maxRequests: z.number().default(100),
      windowMs: z.number().default(60000),
    })
    .optional(),
  timeout: z.number().default(30000),
  retries: z.number().default(3),
});

// EOD Historical Data provider
export const eodProviderConfigSchema = baseProviderConfigSchema.extend({
  apiKey: z.string(),
  baseUrl: z.string().default('https://eodhistoricaldata.com/api'),
  tier: z.enum(['free', 'fundamentals', 'all-in-one']).default('free'),
});

// Interactive Brokers provider
export const ibProviderConfigSchema = baseProviderConfigSchema.extend({
  gateway: z.object({
    host: z.string().default('localhost'),
    port: z.number().default(5000),
    clientId: z.number().default(1),
  }),
  account: z.string().optional(),
  marketDataType: z.enum(['live', 'delayed', 'frozen']).default('delayed'),
});

// QuoteMedia provider
export const qmProviderConfigSchema = baseProviderConfigSchema.extend({
  username: z.string(),
  password: z.string(),
  baseUrl: z.string().default('https://app.quotemedia.com/quotetools'),
  webmasterId: z.string(),
});

// Yahoo Finance provider
export const yahooProviderConfigSchema = baseProviderConfigSchema.extend({
  baseUrl: z.string().default('https://query1.finance.yahoo.com'),
  cookieJar: z.boolean().default(true),
  crumb: z.string().optional(),
});

// Combined provider configuration
export const providersSchema = z.object({
  eod: eodProviderConfigSchema.optional(),
  ib: ibProviderConfigSchema.optional(),
  qm: qmProviderConfigSchema.optional(),
  yahoo: yahooProviderConfigSchema.optional(),
});

// Dynamic provider configuration type
export type ProviderName = 'eod' | 'ib' | 'qm' | 'yahoo';

export const providerSchemas = {
  eod: eodProviderConfigSchema,
  ib: ibProviderConfigSchema,
  qm: qmProviderConfigSchema,
  yahoo: yahooProviderConfigSchema,
} as const;
