// src/tools/skills/get-skill.ts
// Copyright (C) 2025 DTP Technologies, LLC
// All Rights Reserved

import type { ToolDefinition } from "../../lib/ai-client.js";

import {
  DtpTool,
  type ToolArguments,
  type ToolContext,
} from "../../lib/tool.js";
import { AiSkill } from "../../models/ai-skill.js";
import { ChatSessionMode } from "../../models/chat-session.js";

export class GetSkillTool extends DtpTool {
  get name(): string {
    return "GetSkillTool";
  }
  get slug(): string {
    return "get-skill";
  }
  get metadata() {
    return {
      name: this.definition.function.name || "get_skill",
      category: "skills",
      tags: ["skill", "knowledge", "recipe", "get"],
      modes: [
        ChatSessionMode.Plan,
        ChatSessionMode.Build,
        ChatSessionMode.Test,
        ChatSessionMode.Ship,
        ChatSessionMode.Develop,
      ],
    };
  }

  public definition: ToolDefinition = {
    type: "function",
    function: {
      name: "get_skill",
      description:
        "Fetch a specific skill by its ID. Returns the skill's name, description, tags, modes, and content. Use this when you know the skill ID or need to retrieve a specific skill's full details.",
      parameters: {
        type: "object",
        properties: {
          skill_id: {
            type: "string",
            description: "The ID of the skill to fetch (MongoDB ObjectId).",
          },
        },
        required: ["skill_id"],
      },
    },
  };

  public async execute(
    _context: ToolContext,
    args: ToolArguments,
  ): Promise<string> {
    const skillId = args.skill_id as string | undefined;

    if (!skillId) {
      return this.error("INVALID_PARAMETER", "skill_id is required.", {
        parameter: "skill_id",
      });
    }

    try {
      const skill = await AiSkill.findById(skillId).lean();
      if (!skill) {
        return this.error("NOT_FOUND", `Skill not found: ${skillId}`);
      }
      return this.success({ skill }, `Skill: ${skill.name}`);
    } catch (error) {
      const errorMessage =
        error instanceof Error ? error.message : String(error);
      return this.error(
        "OPERATION_FAILED",
        `Failed to get skill: ${errorMessage}`,
      );
    }
  }
}

export default new GetSkillTool();
