import { settingsStore } from '../config/state';

export type ModuleKey =
  | 'ticketsEnabled'
  | 'automodEnabled'
  | 'levelingEnabled'
  | 'musicEnabled'
  | 'welcomeEnabled'
  | 'dynamicVoiceEnabled'
  | 'statuspageEnabled'
  | 'birthdayEnabled'
  | 'reactionRolesEnabled'
  | 'eventsEnabled'
  | 'registerEnabled'
  | 'serverStatsEnabled';

export interface GuildModuleState {
  key: ModuleKey;
  name: string;
  description: string;
  enabled: boolean;
}

const MODULES: Record<ModuleKey, { name: string; description: string }> = {
  ticketsEnabled: { name: 'Ticketsystem', description: 'Ticket-Panel, Buttons, Transcript-Export.' },
  automodEnabled: { name: 'Automod', description: 'Linkfilter und Anti-Spam Timeout.' },
  levelingEnabled: { name: 'Leveling', description: 'XP/Level-Tracking und /rank.' },
  musicEnabled: { name: 'Musik', description: 'Wiedergabe, Queue, Loop, Pause/Resume.' },
  welcomeEnabled: { name: 'Willkommensnachrichten', description: 'Begruessungs-Embed beim Join.' },
  dynamicVoiceEnabled: { name: 'Dynamische Voice Channels', description: 'Erzeugt private Voice-Channels aus einer Lobby.' },
  statuspageEnabled: { name: 'Statuspage', description: 'Service Checks, Uptime und Status-Embed.' },
  birthdayEnabled: { name: 'Birthday', description: 'Geburtstage speichern und Glueckwuensche senden.' },
  reactionRolesEnabled: { name: 'Reaction Roles', description: 'Reaktionen vergeben und entfernen Rollen.' },
  eventsEnabled: { name: 'Termine', description: 'Events planen, erinnern und Anmeldungen sammeln.' },
  registerEnabled: { name: 'Register', description: 'Registrierungsformulare und Bewerbungen.' },
  serverStatsEnabled: { name: 'Server Stats', description: 'Zeigt Member-/Channel-Zahlen als Voice-Statistiken an.' }
};

export class BotModuleService {
  private hooks: Partial<Record<ModuleKey, { onEnable?: (guildId: string) => Promise<void> | void; onDisable?: (guildId: string) => Promise<void> | void }>> =
    {};

  public setHooks(hooks: Partial<Record<ModuleKey, { onEnable?: (guildId: string) => Promise<void> | void; onDisable?: (guildId: string) => Promise<void> | void }>>) {
    this.hooks = hooks;
  }

  public async getModulesForGuild(guildId: string): Promise<GuildModuleState[]> {
    const cfg = settingsStore.get(guildId) ?? {};
    return Object.entries(MODULES).map(([key, meta]) => {
      let enabled = cfg[key as ModuleKey] === true;
      if (key === 'automodEnabled') enabled = cfg.automodEnabled === true;
      if (key === 'welcomeEnabled') enabled = cfg.welcomeConfig?.enabled === true || cfg.automodConfig?.welcomeConfig?.enabled === true;
      if (key === 'dynamicVoiceEnabled') enabled = cfg.dynamicVoiceEnabled === true || (cfg.dynamicVoiceConfig as any)?.enabled === true;
      if (key === 'statuspageEnabled') enabled = cfg.statuspageEnabled === true || cfg.automodConfig?.statuspageEnabled === true;
      if (key === 'birthdayEnabled') enabled = cfg.birthdayEnabled === true || cfg.birthdayConfig?.enabled === true;
      if (key === 'reactionRolesEnabled') enabled = cfg.reactionRolesEnabled === true || cfg.reactionRolesConfig?.enabled === true;
      if (key === 'eventsEnabled') enabled = (cfg as any).eventsEnabled !== false;
      if (key === 'serverStatsEnabled') enabled = (cfg as any).serverStatsEnabled === true || (cfg as any).serverStatsConfig?.enabled === true;
      return {
        key: key as ModuleKey,
        name: meta.name,
        description: meta.description,
        enabled
      };
    });
  }

  public async enableModule(guildId: string, key: ModuleKey) {
    if (key === 'welcomeEnabled') {
      await settingsStore.set(guildId, { welcomeConfig: { enabled: true } } as any);
    } else {
      await settingsStore.set(guildId, { [key]: true } as any);
    }
    await this.hooks[key]?.onEnable?.(guildId);
  }

  public async disableModule(guildId: string, key: ModuleKey) {
    if (key === 'welcomeEnabled') {
      await settingsStore.set(guildId, { welcomeConfig: { enabled: false } } as any);
    } else {
      await settingsStore.set(guildId, { [key]: false } as any);
    }
    await this.hooks[key]?.onDisable?.(guildId);
  }
}
