import { getDB } from '@/lib/db';
import { Constants } from '@projectdysnomia/dysnomia';
import {
  createChatCommand,
  integerOption,
  stringOption,
  subCommandGroupOption,
  subCommandRouter,
  type CommandContext,
  type ExecutableInteraction,
} from '@star-kitten/lib/discord';
import { numberOption, subCommandOption } from '@star-kitten/lib/discord';

export default createChatCommand(
  {
    name: 'route',
    description: 'Routes',
    defaultMemberPermissions: Constants.Permissions.administrator,
    options: [
      subCommandGroupOption({
        name: 'group',
        description: 'a group',
        options: [
          subCommandOption({
            name: 'add',
            description: 'add a route',
            options: [
              stringOption({
                name: 'start',
                description: 'starting location',
                autocomplete: true,
                required: true,
              }),
              stringOption({
                name: 'end',
                description: 'end location',
                autocomplete: true,
                required: true,
              }),
              integerOption({
                name: 'isk-per-m3',
                description: 'ISK per m3',
                required: true,
              }),
              numberOption({
                name: 'collateral-percent',
                description: 'percentage of collateral to add onto the cost.',
                required: true,
              }),
              integerOption({
                name: 'max-volume',
                description: 'Maximum volume allowed for this route',
                required: true,
              }),
              integerOption({
                name: 'min-reward',
                description: 'Minimum required reward for this route',
                required: true,
              }),
              integerOption({
                name: 'expiration',
                description: 'Expiration the client should set on the contract',
                required: true,
              }),
              integerOption({
                name: 'completion',
                description: 'Days to complete time the client should set on the contract',
                required: true,
              }),
              integerOption({
                name: 'max-collateral',
                description: 'Maximum collateral allowed for this route',
              }),
            ],
          }),
          subCommandOption({
            name: 'remove',
            description: 'remove a route',
            options: [
              integerOption({
                name: 'id',
                description: 'ID of the route to remove',
                required: true,
              }),
            ],
          }),
        ],
      }),
    ],
  },
  subCommandRouter({
    group: {
      add: async (interaction, ctx, data) => {
        console.log(`in add`);
        if (interaction.isAutocomplete()) {
          const focused = data.options?.find((opt) => opt.focused);
          console.log(`focused`, focused);
          if (focused) {
            switch (focused.name) {
              case 'start': {
                const locations = getDB().getAllLocations();
                console.log(JSON.stringify(locations.length));
                return await interaction.result(
                  locations.map((l) => ({ name: l.short_name, value: String(l.location_id) })),
                );
              }
              case 'end': {
                const locations = getDB().getAllLocations();
                console.log(JSON.stringify(locations.length));
                return await interaction.result(
                  locations.map((l) => ({ name: l.short_name, value: String(l.location_id) })),
                );
              }
            }
          }
        }
      },
      remove: (interaction, ctx) => {
        console.log('remove handler');
        if (interaction.isApplicationCommand()) {
          interaction.createMessage(`Thanks`);
        }
      },
    },
  }),
);
