import { CloverClient } from '../clients/clover.js';

export function createAppsTools(client: CloverClient) {
  return {
    clover_list_apps: {
      description: 'List all apps installed on the merchant account',
      inputSchema: {
        type: 'object',
        properties: {
          limit: {
            type: 'number',
            description: 'Maximum number of apps to return',
          },
        },
      },
      handler: async (args: any) => {
        const apps = await client.fetchPaginated<any>('/apps', {}, args.limit);
        return { apps, count: apps.length };
      },
    },

    clover_get_app: {
      description: 'Get information about a specific installed app',
      inputSchema: {
        type: 'object',
        properties: {
          appId: { type: 'string', description: 'App ID or UUID' },
        },
        required: ['appId'],
      },
      handler: async (args: any) => {
        return await client.get<any>(`/apps/${args.appId}`);
      },
    },

    clover_get_app_metered_events: {
      description: 'Get metered events for an app (billing/usage data)',
      inputSchema: {
        type: 'object',
        properties: {
          appId: { type: 'string', description: 'App ID' },
          startTime: {
            type: 'number',
            description: 'Start time for events (Unix timestamp)',
          },
          endTime: {
            type: 'number',
            description: 'End time for events (Unix timestamp)',
          },
        },
        required: ['appId'],
      },
      handler: async (args: any) => {
        const params: any = {};
        if (args.startTime) params.startTime = args.startTime;
        if (args.endTime) params.endTime = args.endTime;

        return await client.get<any>(`/apps/${args.appId}/metered_events`, params);
      },
    },

    clover_list_app_notifications: {
      description: 'List notifications sent by an app',
      inputSchema: {
        type: 'object',
        properties: {
          appId: { type: 'string', description: 'App ID' },
          limit: { type: 'number', description: 'Maximum number to return' },
        },
        required: ['appId'],
      },
      handler: async (args: any) => {
        const notifications = await client.fetchPaginated<any>(
          `/apps/${args.appId}/notifications`,
          {},
          args.limit
        );
        return { notifications, count: notifications.length };
      },
    },
  };
}
