import { z } from '@frontmcp/lazy-zod';
import { Tool, ToolContext } from '@frontmcp/sdk';

import EmployeeDirectoryProvider from '../providers/employee-directory.provider';

@Tool({
  name: 'list-employees',
  description: 'List employees, optionally filtered by site, with pagination',
  inputSchema: {
    siteId: z.string().min(1).optional(),
    search: z.string().optional(),
    offset: z.number().int().min(0).optional(),
    limit: z.number().int().min(1).max(200).optional(),
  },
  outputSchema: z.object({
    total: z.number().int().min(0),
    items: z.array(
      z.object({
        employeeId: z.string(),
        sites: z.array(z.string()),
      }),
    ),
    page: z.object({
      offset: z.number().int().min(0),
      limit: z.number().int().min(1).max(200),
    }),
  }),
})
export default class ListEmployeesTool extends ToolContext {
  async execute(input: { siteId?: string; search?: string; offset?: number; limit?: number }) {
    const dir = this.get(EmployeeDirectoryProvider);
    const { total, items } = await dir.listEmployees({
      siteId: input.siteId,
      search: input.search,
      offset: input.offset,
      limit: input.limit,
    });

    const withSites = await Promise.all(
      items.map(async (employeeId) => {
        const sites = await dir.listSitesForEmployee(employeeId);
        return { employeeId, sites };
      }),
    );

    return {
      total,
      items: withSites,
      page: {
        offset: input.offset ?? 0,
        limit: Math.min(200, Math.max(1, input.limit ?? 50)),
      },
    };
  }
}
