import { z } from 'zod';

const supportedLanguages = [
  'en',
  'fr',
  'es',
  'de',
  'it',
  'pt',
  'ru',
  'ja',
  'zh',
  'ko',
] as const;
const supportedThemes = ['light', 'dark', 'auto'] as const;

export const settingsSchema = z.object({
  notifications: z.object({
    email_notifications: z.boolean(),
    push_notifications: z.boolean(),
    browser_notifications: z.boolean(),
    email_on_follow: z.boolean(),
    email_on_like: z.boolean(),
    email_on_comment: z.boolean(),
    email_on_message: z.boolean(),
    email_on_mention: z.boolean(),
    email_marketing: z.boolean(),
  }),
  privacy: z.object({
    allow_search_indexing: z.boolean(),
    show_activity: z.boolean(),
  }),
  content: z.object({
    explicit_content: z.boolean(),
    autoplay: z.boolean(),
  }),
  preferences: z.object({
    language: z.enum(supportedLanguages, {
      errorMap: () => ({
        message: 'Langue non supportée. Utilisez un code ISO 639-1 valide.',
      }),
    }),
    timezone: z.string().refine(
      (tz) => {
        // Valider IANA timezone
        try {
          Intl.DateTimeFormat(undefined, { timeZone: tz });
          return true;
        } catch {
          return false;
        }
      },
      { message: 'Fuseau horaire IANA invalide' },
    ),
    theme: z.enum(supportedThemes, {
      errorMap: () => ({
        message: 'Thème invalide. Valeurs autorisées: light, dark, auto',
      }),
    }),
  }),
});

export type SettingsFormData = z.infer<typeof settingsSchema>;
