import type { GustoClient } from '../clients/gusto.js';
import type { Company, Location, Department } from '../types/index.js';

export function createCompanyTools(client: GustoClient) {
  return {
    // Get company information
    get_company: async (args: { company_id: string }) => {
      const company = await client.get<Company>(`/companies/${args.company_id}`);
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(company, null, 2),
          },
        ],
      };
    },

    // List locations for a company
    list_locations: async (args: { company_id: string; page?: number; per?: number }) => {
      const locations = await client.getPaginated<Location>(
        `/companies/${args.company_id}/locations`,
        { page: args.page, per: args.per }
      );
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(locations, null, 2),
          },
        ],
      };
    },

    // Get a specific location
    get_location: async (args: { location_id: string }) => {
      const location = await client.get<Location>(`/locations/${args.location_id}`);
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(location, null, 2),
          },
        ],
      };
    },

    // Create a new location
    create_location: async (args: {
      company_id: string;
      phone_number?: string;
      street_1?: string;
      street_2?: string;
      city?: string;
      state?: string;
      zip?: string;
      country?: string;
    }) => {
      const location = await client.post<Location>(
        `/companies/${args.company_id}/locations`,
        {
          phone_number: args.phone_number,
          street_1: args.street_1,
          street_2: args.street_2,
          city: args.city,
          state: args.state,
          zip: args.zip,
          country: args.country,
        }
      );
      return {
        content: [
          {
            type: 'text',
            text: `Location created successfully:\n${JSON.stringify(location, null, 2)}`,
          },
        ],
      };
    },

    // Update a location
    update_location: async (args: {
      location_id: string;
      version: string;
      phone_number?: string;
      street_1?: string;
      street_2?: string;
      city?: string;
      state?: string;
      zip?: string;
      country?: string;
    }) => {
      const location = await client.put<Location>(`/locations/${args.location_id}`, {
        version: args.version,
        phone_number: args.phone_number,
        street_1: args.street_1,
        street_2: args.street_2,
        city: args.city,
        state: args.state,
        zip: args.zip,
        country: args.country,
      });
      return {
        content: [
          {
            type: 'text',
            text: `Location updated successfully:\n${JSON.stringify(location, null, 2)}`,
          },
        ],
      };
    },

    // List departments for a company
    list_departments: async (args: { company_id: string }) => {
      const departments = await client.get<Department[]>(
        `/companies/${args.company_id}/departments`
      );
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(departments, null, 2),
          },
        ],
      };
    },
  };
}

export const companyToolDefinitions = [
  {
    name: 'get_company',
    description: 'Get detailed information about a company',
    inputSchema: {
      type: 'object',
      properties: {
        company_id: { type: 'string', description: 'Company ID' },
      },
      required: ['company_id'],
    },
  },
  {
    name: 'list_locations',
    description: 'List all locations for a company',
    inputSchema: {
      type: 'object',
      properties: {
        company_id: { type: 'string', description: 'Company ID' },
        page: { type: 'number', description: 'Page number (default: 1)' },
        per: { type: 'number', description: 'Results per page (default: 25)' },
      },
      required: ['company_id'],
    },
  },
  {
    name: 'get_location',
    description: 'Get detailed information about a specific location',
    inputSchema: {
      type: 'object',
      properties: {
        location_id: { type: 'string', description: 'Location ID' },
      },
      required: ['location_id'],
    },
  },
  {
    name: 'create_location',
    description: 'Create a new location for a company',
    inputSchema: {
      type: 'object',
      properties: {
        company_id: { type: 'string', description: 'Company ID' },
        phone_number: { type: 'string', description: 'Phone number' },
        street_1: { type: 'string', description: 'Street address line 1' },
        street_2: { type: 'string', description: 'Street address line 2' },
        city: { type: 'string', description: 'City' },
        state: { type: 'string', description: 'State (2-letter code)' },
        zip: { type: 'string', description: 'ZIP code' },
        country: { type: 'string', description: 'Country (default: USA)' },
      },
      required: ['company_id'],
    },
  },
  {
    name: 'update_location',
    description: 'Update an existing location',
    inputSchema: {
      type: 'object',
      properties: {
        location_id: { type: 'string', description: 'Location ID' },
        version: { type: 'string', description: 'Version for optimistic locking' },
        phone_number: { type: 'string', description: 'Phone number' },
        street_1: { type: 'string', description: 'Street address line 1' },
        street_2: { type: 'string', description: 'Street address line 2' },
        city: { type: 'string', description: 'City' },
        state: { type: 'string', description: 'State (2-letter code)' },
        zip: { type: 'string', description: 'ZIP code' },
        country: { type: 'string', description: 'Country' },
      },
      required: ['location_id', 'version'],
    },
  },
  {
    name: 'list_departments',
    description: 'List all departments for a company',
    inputSchema: {
      type: 'object',
      properties: {
        company_id: { type: 'string', description: 'Company ID' },
      },
      required: ['company_id'],
    },
  },
];
