import { createAction, Property } from '@activepieces/pieces-framework';
import { mycaseAuth } from '../auth';
import { createMyCaseApi } from '../common/mycase-api';

export const findLocation = createAction({
  auth: mycaseAuth,
  name: 'find_location',
  displayName: 'Find Location',
  description: 'Searches for locations with optional filters',
  audience: 'both',
  aiMetadata: { description: 'List or search MyCase office/event locations, optionally filtering by last-updated date and limiting page size. Use to resolve a location name to its ID before referencing it elsewhere (e.g. when creating an event). Read-only and idempotent.', idempotent: true },
  props: {
    updated_after: Property.DateTime({
      displayName: 'Updated After',
      description: 'Filter locations updated after this date and time',
      required: false,
    }),
    page_size: Property.Number({
      displayName: 'Page Size',
      description: 'Number of results per page (1-1000)',
      required: false,
      defaultValue: 25,
    }),
  },
  async run(context) {
    const api = createMyCaseApi(context.auth);
    
    const queryParams: Record<string, string> = {};
    
    if (context.propsValue.updated_after) {
      queryParams['filter[updated_after]'] = context.propsValue.updated_after;
    }
    
    if (context.propsValue.page_size) {
      queryParams['page_size'] = context.propsValue.page_size.toString();
    }

    try {
      const response = await api.get('/locations', queryParams);
      
      if (response.success) {
        return {
          success: true,
          locations: response.data,
          count: Array.isArray(response.data) ? response.data.length : 0,
        };
      } else {
        return {
          success: false,
          error: response.error,
          details: response.details,
        };
      }
    } catch (error) {
      return {
        success: false,
        error: 'Failed to find locations',
        details: error instanceof Error ? error.message : String(error),
      };
    }
  },
});
