import { Request, Response } from 'express';
import { WebhookEvent } from 'livekit-server-sdk';
import { container } from '../config/dependency-injector.config.js';
import { LivekitWebhookService } from '../services/livekit-webhook.service.js';
import { LoggerService } from '../services/logger.service.js';

export const lkWebhookHandler = async (req: Request, res: Response) => {
	const logger = container.get(LoggerService);

	try {
		const lkWebhookService = container.get(LivekitWebhookService);

		const webhookEvent: WebhookEvent | undefined = await lkWebhookService.getEventFromWebhook(
			req.body,
			req.get('Authorization')!
		);

		if (!webhookEvent) {
			logger.debug(`Webhook processing skipped: May another instance is processing it`);
			return res.status(200).send();
		}

		const { event: eventType, egressInfo, room, participant } = webhookEvent;

		const belongsToOpenViduMeet = await lkWebhookService.webhookEventBelongsToOpenViduMeet(webhookEvent);

		if (!belongsToOpenViduMeet) {
			logger.verbose(`Webhook skipped: ${eventType}. Not related to OpenVidu Meet.`);
			return res.status(200).send();
		}

		logger.info(`Webhook received: ${eventType}`);
		logger.debug(`Webhook event object: ${JSON.stringify(webhookEvent, null, 2)}`);

		switch (eventType) {
			case 'egress_started':
				await lkWebhookService.handleEgressStarted(egressInfo!);
				break;
			case 'egress_updated':
				await lkWebhookService.handleEgressUpdated(egressInfo!);
				break;
			case 'egress_ended':
				await lkWebhookService.handleEgressEnded(egressInfo!);
				break;
			case 'participant_joined':
				await lkWebhookService.handleParticipantJoined(room!, participant!);
				break;
			case 'participant_left':
				await lkWebhookService.handleParticipantLeft(room!, participant!);
				break;
			case 'room_started':
				await lkWebhookService.handleRoomStarted(room!);
				break;
			case 'room_finished':
				await lkWebhookService.handleRoomFinished(room!);
				break;
			default:
				break;
		}
	} catch (error) {
		logger.error(`Error handling webhook event: ${error}`);
	}

	return res.status(200).send();
};
