import { SkillCommandSource } from '@robota-sdk/agent-framework';

import { executeSkillsCommand, SKILLS_COMMAND_DESCRIPTION } from './skills-command.js';

import type { ICommandModule, ISystemCommand } from '@robota-sdk/agent-framework';
import type { ICommand, ICommandSource } from '@robota-sdk/agent-interface-transport';

export interface ISkillsCommandModuleOptions {
  readonly cwd: string;
}

export function createSkillsCommandEntry(): ICommand {
  return {
    name: 'skills',
    displayName: 'Skills',
    description: SKILLS_COMMAND_DESCRIPTION,
    source: 'skills',
    modelInvocable: true,
    userInvocable: true,
    argumentHint: '[list | <skill-name> [args]]',
    safety: 'read-only',
  };
}

function createSkillsSystemCommand(): ISystemCommand {
  const entry = createSkillsCommandEntry();
  return {
    name: entry.name,
    displayName: entry.displayName,
    description: entry.description,
    requiresPermission: false,
    userInvocable: true,
    modelInvocable: true,
    argumentHint: entry.argumentHint,
    safety: entry.safety,
    lifecycle: 'inline',
    execute: executeSkillsCommand,
  };
}

export class SkillsCommandSource implements ICommandSource {
  readonly name = 'skills';

  getCommands(): ICommand[] {
    return [createSkillsCommandEntry()];
  }
}

export function createSkillsCommandModule(options: ISkillsCommandModuleOptions): ICommandModule {
  const commandSources: ICommandSource[] = [new SkillsCommandSource()];
  commandSources.push(new SkillCommandSource(options.cwd));

  return {
    name: 'agent-command-skills',
    commandSources,
    systemCommands: [createSkillsSystemCommand()],
  };
}
