// Theme Configuration
// Light/dark themes and participant-specific theme customization

import { ParticipantType } from '@/core/operations/permissions/permission-types';

export type ThemeMode = 'light' | 'dark' | 'auto';

export interface ThemeColors {
  background: {
    primary: string;
    secondary: string;
    tertiary: string;
  };
  text: {
    primary: string;
    secondary: string;
    tertiary: string;
    inverse: string;
  };
  border: {
    primary: string;
    secondary: string;
  };
  interactive: {
    default: string;
    hover: string;
    active: string;
    disabled: string;
  };
}

export interface ParticipantTheme {
  primary: string;
  secondary: string;
  accent: string;
  name: string;
}

export interface ThemeConfig {
  mode: ThemeMode;
  participant: ParticipantType;
  colors: ThemeColors;
  participantTheme: ParticipantTheme;
}

const lightThemeColors: ThemeColors = {
  background: {
    primary: '#FFFFFF',
    secondary: '#F5F5F5',
    tertiary: '#EEEEEE',
  },
  text: {
    primary: '#212121',
    secondary: '#616161',
    tertiary: '#9E9E9E',
    inverse: '#FFFFFF',
  },
  border: {
    primary: '#E0E0E0',
    secondary: '#BDBDBD',
  },
  interactive: {
    default: '#2196F3',
    hover: '#1976D2',
    active: '#0D47A1',
    disabled: '#BDBDBD',
  },
};

const darkThemeColors: ThemeColors = {
  background: {
    primary: '#121212',
    secondary: '#1E1E1E',
    tertiary: '#2C2C2C',
  },
  text: {
    primary: '#FFFFFF',
    secondary: '#BDBDBD',
    tertiary: '#757575',
    inverse: '#212121',
  },
  border: {
    primary: '#424242',
    secondary: '#616161',
  },
  interactive: {
    default: '#42A5F5',
    hover: '#64B5F6',
    active: '#90CAF9',
    disabled: '#616161',
  },
};

const participantThemes: Record<ParticipantType, ParticipantTheme> = {
  DBIS: {
    primary: '#0D47A1',
    secondary: '#1565C0',
    accent: '#2196F3',
    name: 'DBIS',
  },
  SCB: {
    primary: '#7B1FA2',
    secondary: '#8E24AA',
    accent: '#9C27B0',
    name: 'SCB',
  },
  FI: {
    primary: '#388E3C',
    secondary: '#43A047',
    accent: '#4CAF50',
    name: 'FI',
  },
};

export class ThemeService {
  /**
   * Get theme configuration
   */
  static getTheme(
    mode: ThemeMode = 'light',
    participant: ParticipantType = 'DBIS'
  ): ThemeConfig {
    const colors = mode === 'dark' ? darkThemeColors : lightThemeColors;
    const participantTheme = participantThemes[participant];

    return {
      mode,
      participant,
      colors,
      participantTheme,
    };
  }

  /**
   * Get participant-specific theme
   */
  static getParticipantTheme(participant: ParticipantType): ParticipantTheme {
    return participantThemes[participant];
  }

  /**
   * Apply theme to CSS variables (for CSS-in-JS or style injection)
   */
  static getThemeCSSVariables(theme: ThemeConfig): Record<string, string> {
    return {
      '--theme-bg-primary': theme.colors.background.primary,
      '--theme-bg-secondary': theme.colors.background.secondary,
      '--theme-bg-tertiary': theme.colors.background.tertiary,
      '--theme-text-primary': theme.colors.text.primary,
      '--theme-text-secondary': theme.colors.text.secondary,
      '--theme-text-tertiary': theme.colors.text.tertiary,
      '--theme-text-inverse': theme.colors.text.inverse,
      '--theme-border-primary': theme.colors.border.primary,
      '--theme-border-secondary': theme.colors.border.secondary,
      '--theme-interactive-default': theme.colors.interactive.default,
      '--theme-interactive-hover': theme.colors.interactive.hover,
      '--theme-interactive-active': theme.colors.interactive.active,
      '--theme-interactive-disabled': theme.colors.interactive.disabled,
      '--theme-participant-primary': theme.participantTheme.primary,
      '--theme-participant-secondary': theme.participantTheme.secondary,
      '--theme-participant-accent': theme.participantTheme.accent,
    };
  }

  /**
   * Detect system theme preference
   */
  static detectSystemTheme(): ThemeMode {
    if (typeof window !== 'undefined' && window.matchMedia) {
      return window.matchMedia('(prefers-color-scheme: dark)').matches
        ? 'dark'
        : 'light';
    }
    return 'light';
  }

  /**
   * Resolve theme mode (auto -> light/dark based on system)
   */
  static resolveThemeMode(mode: ThemeMode): 'light' | 'dark' {
    if (mode === 'auto') {
      return this.detectSystemTheme();
    }
    return mode;
  }
}

