import { CredentialType } from '@bubblelab/shared-schemas';
import { ServiceBubble } from '../../../types/service-bubble-class.js';
import { AssembledParamsSchema, AssembledResultSchema, } from './assembled.schema.js';
import { makeAssembledRequest } from './assembled.utils.js';
export class AssembledBubble extends ServiceBubble {
    static type = 'service';
    static service = 'assembled';
    static authType = 'apikey';
    static bubbleName = 'assembled';
    static schema = AssembledParamsSchema;
    static resultSchema = AssembledResultSchema;
    static shortDescription = 'Workforce management platform for scheduling, time off, and agent management';
    static longDescription = 'Assembled is a workforce management platform. This integration supports managing people/agents, activities/schedules, time off requests, queues, and teams via the Assembled REST API.';
    static alias = 'assembled';
    constructor(params, context) {
        super({
            ...params,
        }, context);
    }
    chooseCredential() {
        const { credentials } = this.params;
        return credentials?.[CredentialType.ASSEMBLED_CRED];
    }
    getApiKey() {
        const apiKey = this.chooseCredential();
        if (!apiKey) {
            throw new Error('Assembled API key not found. Please provide an ASSEMBLED_CRED credential.');
        }
        return apiKey;
    }
    async testCredential() {
        try {
            const apiKey = this.getApiKey();
            // Use list_queues as a lightweight credential test
            await makeAssembledRequest({
                method: 'GET',
                path: '/queues',
                apiKey,
            });
            return true;
        }
        catch {
            return false;
        }
    }
    async performAction(_context) {
        const params = this.params;
        const { operation } = params;
        try {
            switch (operation) {
                case 'list_people':
                    return (await this.listPeople(params));
                case 'get_person':
                    return (await this.getPerson(params));
                case 'create_person':
                    return (await this.createPerson(params));
                case 'update_person':
                    return (await this.updatePerson(params));
                case 'list_activities':
                    return (await this.listActivities(params));
                case 'create_activity':
                    return (await this.createActivity(params));
                case 'delete_activities':
                    return (await this.deleteActivities(params));
                case 'create_time_off':
                    return (await this.createTimeOff(params));
                case 'list_time_off':
                    return (await this.listTimeOff(params));
                case 'cancel_time_off':
                    return (await this.cancelTimeOff(params));
                case 'list_queues':
                    return (await this.listQueues());
                case 'list_teams':
                    return (await this.listTeams());
                default:
                    throw new Error(`Unsupported operation: ${operation}`);
            }
        }
        catch (error) {
            return {
                operation,
                success: false,
                error: error instanceof Error ? error.message : 'An unknown error occurred',
            };
        }
    }
    // ─── People operations ──────────────────────────────────────────────────
    async listPeople(params) {
        const apiKey = this.getApiKey();
        const data = await makeAssembledRequest({
            method: 'GET',
            path: '/people',
            apiKey,
            queryParams: {
                limit: params.limit,
                offset: params.offset,
                channel: params.channel,
                team: params.team,
                site: params.site,
                queue: params.queue,
                search: params.search,
            },
        });
        return {
            operation: 'list_people',
            success: true,
            error: '',
            people: data.people || [],
            total: data.total,
        };
    }
    async getPerson(params) {
        const apiKey = this.getApiKey();
        const data = await makeAssembledRequest({
            method: 'GET',
            path: `/people/${params.person_id}`,
            apiKey,
        });
        return {
            operation: 'get_person',
            success: true,
            error: '',
            person: data,
        };
    }
    async createPerson(params) {
        const apiKey = this.getApiKey();
        const body = {
            first_name: params.first_name,
            last_name: params.last_name,
        };
        if (params.email)
            body.email = params.email;
        if (params.imported_id)
            body.imported_id = params.imported_id;
        if (params.channels)
            body.channels = params.channels;
        if (params.teams)
            body.teams = params.teams;
        if (params.queues)
            body.queues = params.queues;
        if (params.site)
            body.site = params.site;
        if (params.timezone)
            body.timezone = params.timezone;
        if (params.roles)
            body.roles = params.roles;
        if (params.staffable !== undefined)
            body.staffable = params.staffable;
        const data = await makeAssembledRequest({
            method: 'POST',
            path: '/people',
            apiKey,
            body,
        });
        return {
            operation: 'create_person',
            success: true,
            error: '',
            person: data,
        };
    }
    async updatePerson(params) {
        const apiKey = this.getApiKey();
        const body = {};
        if (params.first_name)
            body.first_name = params.first_name;
        if (params.last_name)
            body.last_name = params.last_name;
        if (params.email)
            body.email = params.email;
        if (params.channels)
            body.channels = params.channels;
        if (params.teams)
            body.teams = params.teams;
        if (params.queues)
            body.queues = params.queues;
        if (params.site)
            body.site = params.site;
        if (params.timezone)
            body.timezone = params.timezone;
        if (params.staffable !== undefined)
            body.staffable = params.staffable;
        const data = await makeAssembledRequest({
            method: 'PATCH',
            path: `/people/${params.person_id}`,
            apiKey,
            body,
        });
        return {
            operation: 'update_person',
            success: true,
            error: '',
            person: data,
        };
    }
    // ─── Activities operations ──────────────────────────────────────────────
    async listActivities(params) {
        const apiKey = this.getApiKey();
        const queryParams = {
            start_time: params.start_time,
            end_time: params.end_time,
            include_agents: params.include_agents,
        };
        if (params.queue)
            queryParams.queue = params.queue;
        if (params.agent_ids?.length) {
            queryParams.agent_ids = params.agent_ids.join(',');
        }
        const data = await makeAssembledRequest({
            method: 'GET',
            path: '/activities',
            apiKey,
            queryParams,
        });
        return {
            operation: 'list_activities',
            success: true,
            error: '',
            activities: data.activities || {},
            agents: data.agents || {},
        };
    }
    async createActivity(params) {
        const apiKey = this.getApiKey();
        const body = {
            agent_id: params.agent_id,
            type_id: params.type_id,
            start_time: params.start_time,
            end_time: params.end_time,
        };
        if (params.channels)
            body.channels = params.channels;
        if (params.description)
            body.description = params.description;
        if (params.allow_conflicts)
            body.allow_conflicts = params.allow_conflicts;
        const data = await makeAssembledRequest({
            method: 'POST',
            path: '/activities',
            apiKey,
            body,
        });
        return {
            operation: 'create_activity',
            success: true,
            error: '',
            activity: data,
        };
    }
    async deleteActivities(params) {
        const apiKey = this.getApiKey();
        await makeAssembledRequest({
            method: 'DELETE',
            path: '/activities',
            apiKey,
            body: {
                agent_ids: params.agent_ids,
                start_time: params.start_time,
                end_time: params.end_time,
            },
        });
        return {
            operation: 'delete_activities',
            success: true,
            error: '',
        };
    }
    // ─── Time Off operations ────────────────────────────────────────────────
    async createTimeOff(params) {
        const apiKey = this.getApiKey();
        const body = {
            agent_id: params.agent_id,
            start_time: params.start_time,
            end_time: params.end_time,
        };
        if (params.type_id)
            body.type_id = params.type_id;
        if (params.status)
            body.status = params.status;
        if (params.notes)
            body.notes = params.notes;
        const data = await makeAssembledRequest({
            method: 'POST',
            path: '/time_off',
            apiKey,
            body,
        });
        return {
            operation: 'create_time_off',
            success: true,
            error: '',
            time_off: data,
        };
    }
    async listTimeOff(params) {
        const apiKey = this.getApiKey();
        const queryParams = {
            limit: params.limit,
            offset: params.offset,
        };
        if (params.status)
            queryParams.status = params.status;
        if (params.agent_ids?.length) {
            queryParams.agent_ids = params.agent_ids.join(',');
        }
        const data = await makeAssembledRequest({
            method: 'GET',
            path: '/time_off/requests',
            apiKey,
            queryParams,
        });
        return {
            operation: 'list_time_off',
            success: true,
            error: '',
            requests: data.requests || [],
        };
    }
    async cancelTimeOff(params) {
        const apiKey = this.getApiKey();
        await makeAssembledRequest({
            method: 'POST',
            path: `/time_off/${params.time_off_id}/cancel`,
            apiKey,
        });
        return {
            operation: 'cancel_time_off',
            success: true,
            error: '',
        };
    }
    // ─── Filter operations (queues, teams) ──────────────────────────────────
    async listQueues() {
        const apiKey = this.getApiKey();
        const data = await makeAssembledRequest({
            method: 'GET',
            path: '/queues',
            apiKey,
        });
        return {
            operation: 'list_queues',
            success: true,
            error: '',
            queues: data.queues || [],
        };
    }
    async listTeams() {
        const apiKey = this.getApiKey();
        const data = await makeAssembledRequest({
            method: 'GET',
            path: '/teams',
            apiKey,
        });
        return {
            operation: 'list_teams',
            success: true,
            error: '',
            teams: data.teams || [],
        };
    }
}
//# sourceMappingURL=assembled.js.map