"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SupportAssignmentService = void 0;
class SupportAssignmentService {
    agentRepository;
    settingsRepository;
    logger;
    roundRobinIndex = 0;
    constructor(agentRepository, settingsRepository, logger) {
        this.agentRepository = agentRepository;
        this.settingsRepository = settingsRepository;
        this.logger = logger;
    }
    async assignTicket(ticket) {
        const settings = await this.settingsRepository.get();
        const mode = settings?.assignmentMode ?? "ROUND_ROBIN";
        const agents = await this.agentRepository.findOnline();
        const eligibleAgents = agents.filter((a) => a.active && a.currentTickets < a.maxConcurrentTickets);
        if (eligibleAgents.length === 0) {
            this.logger.warn("No eligible online agents for assignment.", { ticketId: ticket.id });
            return null;
        }
        let selected;
        switch (mode) {
            case "LEAST_BUSY":
                selected = eligibleAgents.sort((a, b) => a.currentTickets - b.currentTickets)[0];
                break;
            case "RANDOM":
                selected = eligibleAgents[Math.floor(Math.random() * eligibleAgents.length)];
                break;
            case "ROUND_ROBIN":
            default:
                this.roundRobinIndex = this.roundRobinIndex % eligibleAgents.length;
                selected = eligibleAgents[this.roundRobinIndex];
                this.roundRobinIndex++;
                break;
        }
        this.logger.info("Auto-assigned ticket.", { ticketId: ticket.id, agentId: selected.id, mode });
        return selected;
    }
}
exports.SupportAssignmentService = SupportAssignmentService;
