// Copyright (C) 2026 Rob Colbert <rob.colbert@openplatform.us>
// Licensed under the Apache License, Version 2.0

import type { IAiLogger, IToolArguments } from "@gadget/ai";
import { DroneTool } from "../tool.ts";

export class ListSkillsTool extends DroneTool {
  get name(): string {
    return "list_skills";
  }

  get category(): string {
    return "project";
  }

  public definition = {
    type: "function" as const,
    function: {
      name: this.name,
      description:
        "List all skills defined on the current project that are available for the current mode. Skills contain project-specific knowledge and instructions that can help you work more effectively. Use read_skill(index) to read a skill's full content.",
      parameters: {
        type: "object",
        properties: {},
        required: [],
      },
    },
  };

  public async execute(_args: IToolArguments, _logger: IAiLogger): Promise<string> {
    const project = this.toolbox.env.project;
    if (!project) {
      return "No project is configured for this session.";
    }

    const mode = this.toolbox.env.chatSessionMode;
    const skills = project.skills.filter(
      (s) => mode && s.modes.includes(mode),
    );

    if (skills.length === 0) {
      return mode
        ? `Project Skills (0 available in ${mode} mode)\n\nNo skills are defined for this project in ${mode} mode.`
        : "No skills are defined for this project.";
    }

    const lines: string[] = [
      `Project Skills (${skills.length} available in ${mode} mode):`,
      "",
    ];

    for (let i = 0; i < skills.length; i++) {
      const skill = skills[i];
      const preview = skill.content.length > 80
        ? skill.content.slice(0, 80).trim() + "..."
        : skill.content.trim();
      lines.push(`[#${i}] ${skill.name} — ${preview}`);
    }

    return lines.join("\n");
  }
}
