import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js';
import { SlashCommand } from '../../utils/types';
import { normalizeBirthdayInput } from '../../services/birthdayService';
import { context } from '../../config/context';

const command: SlashCommand = {
  guildOnly: true,
  data: new SlashCommandBuilder()
    .setName('birthday')
    .setDescription('Speichert dein Geburtsdatum.')
    .addStringOption((opt) => opt.setName('datum').setDescription('Format: DD.MM.YYYY, DD.MM oder YYYY-MM-DD').setRequired(true)),
  async execute(interaction: ChatInputCommandInteraction) {
    if (!interaction.guildId) {
      await interaction.reply({ content: 'Dieser Befehl funktioniert nur auf Servern.', ephemeral: true });
      return;
    }
    const raw = interaction.options.getString('datum', true);
    const normalized = normalizeBirthdayInput(raw);
    if (!normalized) {
      await interaction.reply({ content: 'Bitte gib ein gueltiges Datum an (DD.MM.YYYY, DD.MM oder YYYY-MM-DD).', ephemeral: true });
      return;
    }
    await context.birthdays.setBirthday(interaction.guildId, interaction.user.id, normalized);
    const formatted = normalized.startsWith('--')
      ? normalized
          .replace('--', '')
          .split('-')
          .reverse()
          .join('.') + '.'
      : normalized.split('-').reverse().join('.');
    await interaction.reply({ content: `Geburtstag gespeichert: ${formatted}`, ephemeral: true });
  }
};

export default command;
