import type { ToolConfig } from '@/tools/types'

interface ListForksParams {
  owner: string
  repo: string
  sort?: 'newest' | 'oldest' | 'stargazers' | 'watchers'
  per_page?: number
  page?: number
  apiKey: string
}

interface ListForksResponse {
  success: boolean
  output: {
    content: string
    metadata: {
      forks: Array<{
        id: number
        full_name: string
        html_url: string
        owner: { login: string }
        stargazers_count: number
        forks_count: number
        created_at: string
        updated_at: string
        default_branch: string
      }>
      count: number
    }
  }
}

export const listForksTool: ToolConfig<ListForksParams, ListForksResponse> = {
  id: 'github_list_forks',
  name: 'GitHub List Forks',
  description: 'List forks of a repository',
  version: '1.0.0',

  params: {
    owner: {
      type: 'string',
      required: true,
      visibility: 'user-or-llm',
      description: 'Repository owner',
    },
    repo: {
      type: 'string',
      required: true,
      visibility: 'user-or-llm',
      description: 'Repository name',
    },
    sort: {
      type: 'string',
      required: false,
      visibility: 'user-or-llm',
      description: 'Sort by: newest, oldest, stargazers, watchers (default: newest)',
      default: 'newest',
    },
    per_page: {
      type: 'number',
      required: false,
      visibility: 'user-or-llm',
      description: 'Results per page (max 100, default: 30)',
      default: 30,
    },
    page: {
      type: 'number',
      required: false,
      visibility: 'user-or-llm',
      description: 'Page number (default: 1)',
      default: 1,
    },
    apiKey: {
      type: 'string',
      required: true,
      visibility: 'user-only',
      description: 'GitHub API token',
    },
  },

  request: {
    url: (params) => {
      const url = new URL(`https://api.github.com/repos/${params.owner}/${params.repo}/forks`)
      if (params.sort) url.searchParams.append('sort', params.sort)
      if (params.per_page) url.searchParams.append('per_page', String(params.per_page))
      if (params.page) url.searchParams.append('page', String(params.page))
      return url.toString()
    },
    method: 'GET',
    headers: (params) => ({
      Accept: 'application/vnd.github.v3+json',
      Authorization: `Bearer ${params.apiKey}`,
      'X-GitHub-Api-Version': '2022-11-28',
    }),
  },

  transformResponse: async (response) => {
    const data = await response.json()

    const forks = data.map((f: any) => ({
      id: f.id,
      full_name: f.full_name,
      html_url: f.html_url,
      owner: { login: f.owner?.login ?? 'unknown' },
      stargazers_count: f.stargazers_count,
      forks_count: f.forks_count,
      created_at: f.created_at,
      updated_at: f.updated_at,
      default_branch: f.default_branch,
    }))

    const content = `Found ${forks.length} fork(s):
${forks
  .map(
    (f: any) =>
      `${f.full_name} ⭐ ${f.stargazers_count}
  Owner: ${f.owner.login}
  ${f.html_url}`
  )
  .join('\n\n')}`

    return {
      success: true,
      output: {
        content,
        metadata: {
          forks,
          count: forks.length,
        },
      },
    }
  },

  outputs: {
    content: { type: 'string', description: 'Human-readable fork list' },
    metadata: {
      type: 'object',
      description: 'Forks metadata',
      properties: {
        forks: {
          type: 'array',
          description: 'Array of forks',
          items: {
            type: 'object',
            properties: {
              id: { type: 'number', description: 'Repository ID' },
              full_name: { type: 'string', description: 'Full name' },
              html_url: { type: 'string', description: 'Web URL' },
              owner: { type: 'object', description: 'Owner info' },
              stargazers_count: { type: 'number', description: 'Star count' },
              forks_count: { type: 'number', description: 'Fork count' },
              created_at: { type: 'string', description: 'Creation date' },
              updated_at: { type: 'string', description: 'Update date' },
              default_branch: { type: 'string', description: 'Default branch' },
            },
          },
        },
        count: { type: 'number', description: 'Number of forks returned' },
      },
    },
  },
}

export const listForksV2Tool: ToolConfig<ListForksParams, any> = {
  id: 'github_list_forks_v2',
  name: listForksTool.name,
  description: listForksTool.description,
  version: '2.0.0',
  params: listForksTool.params,
  request: listForksTool.request,

  transformResponse: async (response: Response) => {
    const data = await response.json()
    return {
      success: true,
      output: {
        items: data,
        count: data.length,
      },
    }
  },

  outputs: {
    items: {
      type: 'array',
      description: 'Array of fork repository objects from GitHub API',
      items: {
        type: 'object',
        properties: {
          id: { type: 'number', description: 'Repository ID' },
          node_id: { type: 'string', description: 'GraphQL node ID' },
          name: { type: 'string', description: 'Repository name' },
          full_name: { type: 'string', description: 'Full name (owner/repo)' },
          private: { type: 'boolean', description: 'Whether repository is private' },
          description: { type: 'string', description: 'Repository description', optional: true },
          html_url: { type: 'string', description: 'GitHub web URL' },
          url: { type: 'string', description: 'API URL' },
          fork: { type: 'boolean', description: 'Whether this is a fork' },
          created_at: { type: 'string', description: 'Creation timestamp' },
          updated_at: { type: 'string', description: 'Last update timestamp' },
          pushed_at: { type: 'string', description: 'Last push timestamp', optional: true },
          size: { type: 'number', description: 'Repository size in KB' },
          stargazers_count: { type: 'number', description: 'Number of stars' },
          watchers_count: { type: 'number', description: 'Number of watchers' },
          forks_count: { type: 'number', description: 'Number of forks' },
          open_issues_count: { type: 'number', description: 'Number of open issues' },
          language: { type: 'string', description: 'Primary programming language', optional: true },
          default_branch: { type: 'string', description: 'Default branch name' },
          visibility: { type: 'string', description: 'Repository visibility' },
          archived: { type: 'boolean', description: 'Whether repository is archived' },
          disabled: { type: 'boolean', description: 'Whether repository is disabled' },
          owner: {
            type: 'object',
            description: 'Fork owner',
            properties: {
              login: { type: 'string', description: 'Username' },
              id: { type: 'number', description: 'User ID' },
              node_id: { type: 'string', description: 'GraphQL node ID' },
              avatar_url: { type: 'string', description: 'Avatar image URL' },
              url: { type: 'string', description: 'API URL' },
              html_url: { type: 'string', description: 'Profile page URL' },
              type: { type: 'string', description: 'User or Organization' },
              site_admin: { type: 'boolean', description: 'GitHub staff indicator' },
            },
          },
        },
      },
    },
    count: { type: 'number', description: 'Number of forks returned' },
  },
}
