import { z } from 'zod';

// ─────────────────────────────────────────────
// Environment configuration with Zod validation
// ─────────────────────────────────────────────

const envSchema = z.object({
  // Discord
  DISCORD_TOKEN: z.string().min(1, 'DISCORD_TOKEN is required'),
  DISCORD_CLIENT_ID: z.string().min(1, 'DISCORD_CLIENT_ID is required'),
  DISCORD_GUILD_ID: z.string().min(1, 'DISCORD_GUILD_ID is required'),

  // Channel IDs — map factory events to Discord channels
  FACTORY_FEED_CHANNEL_ID: z.string().min(1, 'FACTORY_FEED_CHANNEL_ID is required'),
  FACTORY_TASKS_CHANNEL_ID: z.string().min(1, 'FACTORY_TASKS_CHANNEL_ID is required'),
  FACTORY_DEPLOYS_CHANNEL_ID: z.string().min(1, 'FACTORY_DEPLOYS_CHANNEL_ID is required'),
  FACTORY_ALERTS_CHANNEL_ID: z.string().min(1, 'FACTORY_ALERTS_CHANNEL_ID is required'),

  // Factory API
  FACTORY_API_URL: z.string().url().default('https://api.goosefactory.dev/v1'),
  FACTORY_WS_URL: z.string().default('wss://api.goosefactory.dev/v1/ws'),
  FACTORY_API_KEY: z.string().min(1, 'FACTORY_API_KEY is required'),

  // Optional
  LOG_LEVEL: z.enum(['debug', 'info', 'warn', 'error']).default('info'),
  NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),
});

export type EnvConfig = z.infer<typeof envSchema>;

function loadConfig(): EnvConfig {
  const result = envSchema.safeParse(process.env);
  if (!result.success) {
    const missing = result.error.issues.map(
      (i) => `  • ${i.path.join('.')}: ${i.message}`
    );
    console.error('❌ Missing or invalid environment variables:\n' + missing.join('\n'));
    console.error('\nCopy .env.example to .env and fill in the values.');
    process.exit(1);
  }
  return result.data;
}

export const config = loadConfig();

// ─────────────────────────────────────────────
// Channel routing map
// ─────────────────────────────────────────────

export const CHANNEL_MAP = {
  'factory-feed': config.FACTORY_FEED_CHANNEL_ID,
  'factory-tasks': config.FACTORY_TASKS_CHANNEL_ID,
  'factory-deploys': config.FACTORY_DEPLOYS_CHANNEL_ID,
  'factory-alerts': config.FACTORY_ALERTS_CHANNEL_ID,
} as const;

export type ChannelKey = keyof typeof CHANNEL_MAP;

// ─────────────────────────────────────────────
// Priority → Display config
// ─────────────────────────────────────────────

export const PRIORITY_CONFIG = {
  critical: { emoji: '🔴', color: 0xDC2626, label: 'Critical' },
  high:     { emoji: '🟠', color: 0xF97316, label: 'High' },
  medium:   { emoji: '🟡', color: 0xEAB308, label: 'Medium' },
  low:      { emoji: '🟢', color: 0x22C55E, label: 'Low' },
} as const;

export type Priority = keyof typeof PRIORITY_CONFIG;

// ─────────────────────────────────────────────
// Stage display names
// ─────────────────────────────────────────────

export const STAGE_DISPLAY: Record<string, string> = {
  intake: '📥 Intake',
  scaffolding: '🏗️ Scaffolding',
  building: '🔨 Building',
  testing: '🧪 Testing',
  review: '👀 Review',
  staging: '🎭 Staging',
  production: '🚀 Production',
  published: '✅ Published',
};

// ─────────────────────────────────────────────
// Colors
// ─────────────────────────────────────────────

export const COLORS = {
  success: 0x22C55E,    // green
  danger: 0xDC2626,     // red
  warning: 0xF97316,    // orange
  info: 0x3B82F6,       // blue
  neutral: 0x6B7280,    // gray
  action: 0xFF6B35,     // GooseFactory orange
  slaWarning: 0xFBBF24, // amber
  slaBreach: 0xEF4444,  // bright red
} as const;
