import { z } from 'zod';
import {
  baseAppSchema,
  dragonflyConfigSchema,
  mongodbConfigSchema,
  postgresConfigSchema,
  questdbConfigSchema,
} from '@stock-bot/config';
import { featuresSchema } from './features.schema';
import { providersSchema } from './providers.schema';

/**
 * Stock trading application configuration schema
 */
export const stockAppSchema = baseAppSchema.extend({
  // Stock app uses all databases
  database: z.object({
    postgres: postgresConfigSchema,
    mongodb: mongodbConfigSchema,
    questdb: questdbConfigSchema,
    dragonfly: dragonflyConfigSchema,
  }),

  // Stock-specific providers
  providers: providersSchema,

  // Feature flags
  features: featuresSchema,

  // Service-specific configurations
  services: z
    .object({
      dataIngestion: z
        .object({
          port: z.number().default(2001),
          workers: z.number().default(4),
          queues: z
            .record(
              z.object({
                concurrency: z.number().default(1),
              })
            )
            .optional(),
        })
        .optional(),
      dataPipeline: z
        .object({
          port: z.number().default(2002),
          workers: z.number().default(2),
          batchSize: z.number().default(1000),
          processingInterval: z.number().default(60000),
          queues: z
            .record(
              z.object({
                concurrency: z.number().default(1),
              })
            )
            .optional(),
          syncOptions: z
            .object({
              maxRetries: z.number().default(3),
              retryDelay: z.number().default(5000),
              timeout: z.number().default(300000),
            })
            .optional(),
        })
        .optional(),
      webApi: z
        .object({
          port: z.number().default(2003),
          cache: z
            .object({
              ttl: z.number().default(300),
              checkPeriod: z.number().default(60),
            })
            .optional(),
          cors: z
            .object({
              origins: z.array(z.string()).default(['http://localhost:3000']),
              credentials: z.boolean().default(true),
            })
            .optional(),
        })
        .optional(),
      orchestrator: z
        .object({
          port: z.number().default(3002),
          defaultMode: z.enum(['backtest', 'paper', 'live']).default('paper'),
          paperTradingCapital: z.number().default(100000),
          enableWebSocket: z.boolean().default(true),
          backtesting: z
            .object({
              maxConcurrent: z.number().default(5),
              defaultSpeed: z.string().default('max'),
              dataResolutions: z.array(z.string()).default(['1m', '5m', '15m', '1h', '1d']),
            })
            .optional(),
          strategies: z
            .object({
              maxActive: z.number().default(10),
              defaultTimeout: z.number().default(30000),
            })
            .optional(),
        })
        .optional(),
    })
    .optional(),
});

export type StockAppConfig = z.infer<typeof stockAppSchema>;
