import { Tool } from "@modelcontextprotocol/sdk/types.js";
import { GetJobDetailsArgsSchema, GetJobDetailsArgs } from "../../schemas/upwork.js";
import { UpworkClient } from "../../services/upwork-client.js";

export const getJobDetailsToolDefinition: Tool = {
  name: "upwork_get_job_details",
  description: "Get detailed information about a specific job",
  inputSchema: {
    type: "object",
    properties: {
      job_id: { type: "string", description: "Upwork job ID" }
    },
    required: ["job_id"]
  }
};

export async function getJobDetailsHandler(args: unknown, upworkClient: UpworkClient) {
  const validatedArgs = GetJobDetailsArgsSchema.parse(args);
  
  try {
    const response = await upworkClient.getClient().get(`/profiles/v1/jobs/${validatedArgs.job_id}`);
    
    return {
      content: [
        {
          type: "text",
          text: JSON.stringify({
            success: true,
            job: response.data.job || response.data
          }, null, 2)
        }
      ]
    };
  } catch (error) {
    throw upworkClient.handleError(error);
  }
}

export const getJobDetailsTool = {
  definition: getJobDetailsToolDefinition,
  handler: getJobDetailsHandler,
  schema: GetJobDetailsArgsSchema
};