import { createTrigger, TriggerStrategy } from '@activepieces/pieces-framework';
import { assembledCommon } from '../common';
import { HttpMethod } from '@activepieces/pieces-common';
import { assembledAuth } from '../common/auth';

export const newTimeOffRequest = createTrigger({
  auth: assembledAuth,
  name: 'new_OOO_request',
  displayName: 'New OOO Request',
  description: 'Triggers when a new OOO request is created.',
  aiMetadata: {
    description: 'Fires when a new time-off (out-of-office) request is created in Assembled, polling for time-off requests updated since the last check. Use to react to newly submitted OOO requests, for example to route them for approval.',
  },
  type: TriggerStrategy.POLLING,
  props: {},
  sampleData: {
    id: '<uuid>',
    agent_id: '<uuid>',
    start_time: 1546303260,
    end_time: 1546303270,
    created_at: 1546303260,
    description: 'Going to the dentist',
    status: 'approved',
    activity_type_id: '<uuid>',
  },
  async onEnable(context) {
    await context.store.put('lastCheck', Math.floor(Date.now() / 1000));
  },
  async onDisable() {
    // Cleanup if needed
  },
  async run(context) {
    const lastCheck = await context.store.get('lastCheck') || Math.floor(Date.now() / 1000) - 86400; 
    
    const response = await assembledCommon.makeRequest(
      context.auth.secret_text,
      HttpMethod.GET,
      `/time_off/requests?updated_since=${lastCheck}&limit=100`
    );
    
    // Handle the documented response structure
    const timeOffRequests = response.body.time_off_requests || {};
    const newRequests = Object.values(timeOffRequests);
    
    if (newRequests.length > 0) {
      await context.store.put('lastCheck', Math.floor(Date.now() / 1000));
    }
    
    return newRequests;
  },
});