import { createAction, Property } from '@activepieces/pieces-framework';
import { makeAuthenticatedApiCall, formatErrorResponse, validateRequiredParams } from '../../common/utils';
import { API_ENDPOINTS, ISOLATION_STATUSES } from '../../common/constants';

/**
 * Action to isolate a host in CrowdStrike
 */
export const isolateHost = createAction({
  name: 'isolate_host',
  displayName: 'Isolate Host',
  description: 'Isolate a host from the network',
  
  props: {
    device_id: Property.ShortText({
      displayName: 'Device ID',
      description: 'CrowdStrike device ID to isolate',
      required: true,
    }),
  },
  
  async run(context) {
    try {
      const { auth, propsValue } = context;
      
      // Validate required parameters
      validateRequiredParams(propsValue, ['device_id']);
      
      // Make API call to isolate host
      const response = await makeAuthenticatedApiCall(
        auth,
        API_ENDPOINTS.DEVICES_ACTIONS,
        'POST',
        {
          ids: [propsValue.device_id],
          action_parameters: [
            {
              name: 'action_name',
              value: 'contain'
            }
          ]
        }
      );
      
      return {
        success: true,
        request_id: response.meta?.request_id || null,
        isolation_status: ISOLATION_STATUSES.PENDING_ISOLATION,
        device_id: propsValue.device_id,
        action_details: response.resources || [],
      };
    } catch (error) {
      return formatErrorResponse(error);
    }
  },
});
