import { z } from 'zod';

export const NotificationConfigSchema = z.object({
  resend: z.object({
    apiKey: z.string().min(1, 'RESEND_API_KEY required'),
    baseUrl: z.string().default('https://api.resend.com'),
  }),
  fcm: z.object({
    privateKey: z.string().min(1, 'FCM_PRIVATE_KEY required'),
    projectId: z.string().min(1, 'FCM_PROJECT_ID required'),
    clientEmail: z.string().email(),
  }),
  apns: z.object({
    key: z.string().min(1, 'APNS_KEY required'),
    keyId: z.string().min(1, 'APNS_KEY_ID required'),
    teamId: z.string().min(1, 'APNS_TEAM_ID required'),
    bundleId: z.string().min(1, 'APNS_BUNDLE_ID required'),
  }),
  twilio: z.object({
    accountSid: z.string().min(1, 'TWILIO_ACCOUNT_SID required'),
    authToken: z.string().min(1, 'TWILIO_AUTH_TOKEN required'),
    messagingServiceSid: z.string().min(1, 'TWILIO_MESSAGING_SERVICE_SID required'),
  }),
  rateLimits: z.object({
    emailPerMinute: z.number().default(60),
    smsPerMinute: z.number().default(30),
    pushPerMinute: z.number().default(100),
    windowSeconds: z.number().default(60),
  }),
  redis: z.object({
    url: z.string().default('redis://localhost:6379'),
    dedupWindowSeconds: z.number().default(300),
  }),
});

export type NotificationConfig = z.infer<typeof NotificationConfigSchema>;

export const loadNotificationConfig = (): NotificationConfig => {
  const config = {
    resend: {
      apiKey: process.env.RESEND_API_KEY!,
      baseUrl: process.env.RESEND_BASE_URL || 'https://api.resend.com',
    },
    fcm: {
      privateKey: process.env.FCM_PRIVATE_KEY!,
      projectId: process.env.FCM_PROJECT_ID!,
      clientEmail: process.env.FCM_CLIENT_EMAIL!,
    },
    apns: {
      key: process.env.APNS_KEY!,
      keyId: process.env.APNS_KEY_ID!,
      teamId: process.env.APNS_TEAM_ID!,
      bundleId: process.env.APNS_BUNDLE_ID!,
    },
    twilio: {
      accountSid: process.env.TWILIO_ACCOUNT_SID!,
      authToken: process.env.TWILIO_AUTH_TOKEN!,
      messagingServiceSid: process.env.TWILIO_MESSAGING_SERVICE_SID!,
    },
    rateLimits: {
      emailPerMinute: parseInt(process.env.EMAIL_RATE_LIMIT || '60', 10),
      smsPerMinute: parseInt(process.env.SMS_RATE_LIMIT || '30', 10),
      pushPerMinute: parseInt(process.env.PUSH_RATE_LIMIT || '100', 10),
      windowSeconds: parseInt(process.env.RATE_LIMIT_WINDOW_SECONDS || '60', 10),
    },
    redis: {
      url: process.env.REDIS_URL || 'redis://localhost:6379',
      dedupWindowSeconds: parseInt(process.env.DEDUP_WINDOW_SECONDS || '300', 10),
    },
  };

  return NotificationConfigSchema.parse(config);
};
