import {
  SlashCommandBuilder,
  type SlashCommandIntegerOption,
  type SlashCommandStringOption,
  type SlashCommandUserOption
} from 'discord.js';
import {
  applyCommandDescription,
  applyOptionDescription,
  localizedChoice,
  type CommandLocaleKey
} from '@nexumi/shared';

function questionOpt(o: SlashCommandStringOption) {
  return applyOptionDescription(o.setName('question').setRequired(true), 'fun.8ball.options.question');
}

function sidesOpt(o: SlashCommandIntegerOption) {
  return applyOptionDescription(
    o.setName('sides').setMinValue(2).setMaxValue(100),
    'fun.dice.options.sides'
  );
}

function countOpt(o: SlashCommandIntegerOption) {
  return applyOptionDescription(
    o.setName('count').setMinValue(1).setMaxValue(10),
    'fun.dice.options.count'
  );
}

function rpsChoiceOpt(o: SlashCommandStringOption) {
  return applyOptionDescription(o.setName('choice').setRequired(true), 'fun.rps.options.choice').addChoices(
    localizedChoice('fun.rps.choice.rock', 'rock'),
    localizedChoice('fun.rps.choice.paper', 'paper'),
    localizedChoice('fun.rps.choice.scissors', 'scissors')
  );
}

function optionsOpt(o: SlashCommandStringOption) {
  return applyOptionDescription(o.setName('options').setRequired(true), 'fun.choose.options.options');
}

function opponentOpt(o: SlashCommandUserOption) {
  return applyOptionDescription(o.setName('user').setRequired(true), 'fun.common.options.opponent');
}

export const eightBallCommandData = applyCommandDescription(
  new SlashCommandBuilder().setName('8ball'),
  'fun.8ball.description'
).addStringOption((o) => questionOpt(o));

export const diceCommandData = applyCommandDescription(
  new SlashCommandBuilder().setName('dice'),
  'fun.dice.description'
)
  .addIntegerOption((o) => sidesOpt(o))
  .addIntegerOption((o) => countOpt(o));

export const flipCommandData = applyCommandDescription(
  new SlashCommandBuilder().setName('flip'),
  'fun.flip.description'
);

export const rpsCommandData = applyCommandDescription(
  new SlashCommandBuilder().setName('rps'),
  'fun.rps.description'
).addStringOption((o) => rpsChoiceOpt(o));

export const chooseCommandData = applyCommandDescription(
  new SlashCommandBuilder().setName('choose'),
  'fun.choose.description'
).addStringOption((o) => optionsOpt(o));

export const triviaCommandData = applyCommandDescription(
  new SlashCommandBuilder().setName('trivia'),
  'fun.trivia.description'
);

export const tictactoeCommandData = applyCommandDescription(
  new SlashCommandBuilder().setName('tictactoe'),
  'fun.tictactoe.description'
).addUserOption((o) => opponentOpt(o));

export const connect4CommandData = applyCommandDescription(
  new SlashCommandBuilder().setName('connect4'),
  'fun.connect4.description'
).addUserOption((o) => opponentOpt(o));

export const hangmanCommandData = applyCommandDescription(
  new SlashCommandBuilder().setName('hangman'),
  'fun.hangman.description'
);

export const memeCommandData = applyCommandDescription(
  new SlashCommandBuilder().setName('meme'),
  'fun.meme.description'
);

export const catCommandData = applyCommandDescription(
  new SlashCommandBuilder().setName('cat'),
  'fun.cat.description'
);

export const dogCommandData = applyCommandDescription(
  new SlashCommandBuilder().setName('dog'),
  'fun.dog.description'
);

export type RpsChoice = 'rock' | 'paper' | 'scissors';

export type FunCommandLocaleKey = CommandLocaleKey;
