"use strict";Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;const db_1=require("@b/db"),error_1=require("@b/utils/error"),query_1=require("@b/utils/query"),console_1=require("@b/utils/console");exports.metadata={summary:"Assigns or unassigns an agent to a support ticket",operationId:"assignSupportTicketAgent",tags:["Admin","CRM","Support Ticket"],parameters:[{index:0,name:"id",in:"path",required:!0,description:"ID of the support ticket",schema:{type:"string"}}],requestBody:{required:!0,content:{"application/json":{schema:{type:"object",properties:{agentId:{type:"string",nullable:!0,description:"ID of the agent to assign, or null to unassign"}},required:["agentId"]}}}},responses:{200:{description:"Agent assigned/unassigned successfully",content:{"application/json":{schema:{type:"object",properties:{message:{type:"string"},data:{type:"object",properties:{id:{type:"string"},agentId:{type:"string",nullable:!0},agentName:{type:"string",nullable:!0}}}}}}}},401:query_1.unauthorizedResponse,404:(0,query_1.notFoundMetadataResponse)("Support ticket not found"),500:query_1.serverErrorResponse},requiresAuth:!0,permission:"edit.support.ticket",logModule:"ADMIN_SUP",logTitle:"Assign agent to ticket"};exports.default=async e=>{const{params:t,body:s,ctx:r}=e,{agentId:n}=s;try{null==r||r.step("Fetching ticket");const e=await db_1.models.supportTicket.findOne({where:{id:t.id}});if(!e){null==r||r.fail("Ticket not found");throw(0,error_1.createError)({statusCode:404,message:"Support ticket not found"})}let s=null;if(n){null==r||r.step("Verifying agent");const e=await db_1.models.user.findOne({where:{id:n},attributes:["id","firstName","lastName"]});if(!e){null==r||r.fail("Agent not found");throw(0,error_1.createError)({statusCode:404,message:"Agent not found"})}s=`${e.firstName} ${e.lastName}`.trim()}null==r||r.step("Updating ticket assignment");await e.update({agentId:n||null,agentName:s,status:n?"OPEN":"PENDING"});null==r||r.success(n?"Agent assigned successfully":"Agent unassigned successfully");return{message:n?"Agent assigned successfully":"Agent unassigned successfully",data:{id:e.id,agentId:n||null,agentName:s}}}catch(e){console_1.logger.error("SUPPORT","Error assigning agent to ticket",e);if(e.statusCode)throw e;null==r||r.fail("Internal server error");throw(0,error_1.createError)({statusCode:500,message:"Internal server error"})}};