import { type ICannedResponse } from '../../ICannedResponse';
import { type ILivechatAgent } from '../../ILivechatAgent';
import { type ILivechatDepartment } from '../../ILivechatDepartment';
import { type ILivechatDepartmentAgents } from '../../ILivechatDepartmentAgents';
import { type ILivechatMonitor } from '../../ILivechatMonitor';
import { type ILivechatTag } from '../../ILivechatTag';
import { type ILivechatVisitor, type ILivechatVisitorDTO } from '../../ILivechatVisitor';
import { type IMessage } from '../../IMessage';
import { type IOmnichannelRoom, type IServerRoom } from '../../IRoom';
import { type ISetting } from '../../ISetting';
import { type PaginatedRequest } from '../helpers/PaginatedRequest';
import { type PaginatedResult } from '../helpers/PaginatedResult';

type booleanString = 'true' | 'false';

export type OmnichannelEndpoints = {
	'livechat/appearance': {
		GET: () => {
			appearance: ISetting[];
		};
	};
	'livechat/config/routing': {
		GET: () => {
			config: {
				previewRoom: boolean;
				showConnecting: boolean;
				showQueue: boolean;
				showQueueLink: boolean;
				returnQueue: boolean;
				enableTriggerAction: boolean;
				autoAssignAgent: boolean;
			};
		};
	};
	'livechat/visitors.info': {
		GET: (params: { visitorId: string }) => {
			visitor: ILivechatVisitor;
		};
	};
	'livechat/room.onHold': {
		POST: (params: { roomId: IServerRoom['_id'] }) => void;
	};
	'livechat/monitors.list': {
		GET: (params: PaginatedRequest<{ text: string }>) => PaginatedResult<{
			monitors: ILivechatMonitor[];
		}>;
	};
	'livechat/tags.list': {
		GET: (params: PaginatedRequest<{ text: string }, 'name'>) => PaginatedResult<{
			tags: ILivechatTag[];
		}>;
	};
	'livechat/department': {
		GET: (
			params: PaginatedRequest<{
				text: string;
				onlyMyDepartments?: booleanString;
				enabled?: boolean;
				excludeDepartmentId?: string;
			}>
		) => PaginatedResult<{
			departments: ILivechatDepartment[];
		}>;
		POST: (params: { department: Partial<ILivechatDepartment>; agents: string[] }) => {
			department: ILivechatDepartment;
			agents: any[];
		};
	};
	'livechat/department/:_id': {
		GET: (params: { onlyMyDepartments?: booleanString; includeAgents?: booleanString }) => {
			department: ILivechatDepartment | null;
			agents?: any[];
		};
		PUT: (params: { department: Partial<ILivechatDepartment>[]; agents: any[] }) => {
			department: ILivechatDepartment;
			agents: any[];
		};
		DELETE: () => void;
	};
	'livechat/department.autocomplete': {
		GET: (params: { selector: string; onlyMyDepartments: booleanString }) => {
			items: ILivechatDepartment[];
		};
	};
	'livechat/department/:departmentId/agents': {
		GET: (params: { sort: string }) => PaginatedResult<{ agents: ILivechatDepartmentAgents[] }>;
		POST: (params: { upsert: string[]; remove: string[] }) => void;
	};
	'livechat/department/:departmentId/?includeAgents=false': {
		GET: () => PaginatedResult<{ department: ILivechatDepartment }>;
	};
	'livechat/departments.available-by-unit/:id': {
		GET: (params: PaginatedRequest<{ text: string }>) => PaginatedResult<{
			departments: ILivechatDepartment[];
		}>;
	};
	'livechat/departments.by-unit/': {
		GET: (params: PaginatedRequest<{ text: string }>) => PaginatedResult<{
			departments: ILivechatDepartment[];
		}>;
	};

	'livechat/departments.by-unit/:id': {
		GET: (params: PaginatedRequest<{ text: string }>) => PaginatedResult<{
			departments: ILivechatDepartment[];
		}>;
	};

	'livechat/department.listByIds': {
		GET: (params: { ids: string[]; fields?: Record<string, unknown> }) => { departments: ILivechatDepartment[] };
	};

	'livechat/custom-fields': {
		GET: (params: PaginatedRequest<{ text: string }>) => PaginatedResult<{
			customFields: [
				{
					_id: string;
					label: string;
					visibility?: string;
					scope?: string;
				}
			];
		}>;
	};

	'livechat/inquiries.queued': {
		GET: () => PaginatedResult<{
			inquiries: IOmnichannelRoom[];
		}>;
	};

	'livechat/inquiries.queuedForUser': {
		GET: () => PaginatedResult<{
			inquiries: IOmnichannelRoom[];
		}>;
	};

	'livechat/inquiries.take': {
		POST: (params: { inquiryId: string; userId?: string }) => {
			success: boolean;
		};
	};

	'livechat/inquiries.returnAsInquiry': {
		POST: (params: { roomId: string; departmentId?: string }) => {
			result: boolean;
			success: boolean;
		};
	};

	'livechat/rooms': {
		GET: (params: {
			guest: string;
			fname: string;
			servedBy: string[];
			status: string;
			department: string;
			from: string;
			to: string;
			customFields: any;
			current: number;
			itemsPerPage: number;
			tags: string[];
		}) => PaginatedResult<{
			rooms: IOmnichannelRoom[];
		}>;
	};
	'livechat/:rid/messages': {
		GET: (params: PaginatedRequest<{ query: string }>) => PaginatedResult<{
			messages: IMessage[];
		}>;
	};
	'livechat/users/agent': {
		GET: (params: PaginatedRequest<{ text?: string }>) => PaginatedResult<{
			users: {
				_id: string;
				emails: {
					address: string;
					verified: boolean;
				}[];
				status: string;
				name: string;
				username: string;
				statusLivechat: string;
				livechat: {
					maxNumberSimultaneousChat: number;
				};
			}[];
		}>;
	};

	'livechat/visitor': {
		POST: (params: { visitor: ILivechatVisitorDTO }) => { visitor: ILivechatVisitor };
	};

	'livechat/visitor/:token': {
		GET: (params: { token: string }) => { visitor: ILivechatVisitor };
		DELETE: (params: { token: string }) => { visitor: { _id: string; ts: string } };
	};

	'livechat/visitor/:token/room': {
		GET: (params: { token: string }) => { rooms: IOmnichannelRoom[] };
	};

	'livechat/visitor.callStatus': {
		POST: (params: { token: string; callStatus: string; rid: string; callId: string }) => {
			token: string;
			callStatus: string;
		};
	};

	'livechat/visitor.status': {
		POST: (params: { token: string; status: string }) => { token: string; status: string };
	};

	'livechat/queue': {
		GET: (params: {
			agentId?: ILivechatAgent['_id'];
			includeOfflineAgents?: boolean;
			departmentId?: ILivechatAgent['_id'];
			offset: number;
			count: number;
			sort: string | { uploadedAt: number };
		}) => {
			queue: {
				chats: number;
				department: { _id: string; name: string };
				user: { _id: string; username: string; status: string };
			}[];
			count: number;
			offset: number;
			total: number;
		};
	};

	'livechat/agents/:uid/departments?enabledDepartmentsOnly=true': {
		GET: () => { departments: ILivechatDepartment[] };
	};

	'canned-responses': {
		GET: (params: PaginatedRequest<{ scope?: string; departmentId?: string; text?: string }>) => PaginatedResult<{
			cannedResponses: ICannedResponse[];
		}>;
	};

	'livechat/room.saveInfo': {
		POST: (params: {
			guestData: { _id: string; name?: string; email?: string; phone?: string; livechatData?: Record<string, string> };
			roomData: {
				_id: string;
				topic?: string;
				tags?: string[];
				livechatData?: Record<string, string>;
				priorityId?: string;
				slaId?: string;
			};
		}) => void;
	};
	'livechat/agent.status': {
		POST: (params: { agentId?: string }) => {
			status: string;
			success: boolean;
		};
	};
	'livechat/room.closeByUser': {
		POST: (params: {
			rid: string;
			comment?: string;
			forceClose?: boolean;
			tags?: string[];
			generateTranscriptPdf?: boolean;
			transcriptEmail?: { sendToVisitor: boolean; requestData?: { email?: string; subject?: string } };
		}) => {
			success: boolean;
		};
	};
	'livechat/room.forward': {
		POST: (params: { roomId: string; userId?: string; departmentId?: string }) => {
			success: boolean;
		};
	};
	'livechat/tags': {
		GET: () => {
			tags: ILivechatTag[];
			success: boolean;
		};
	};
	'livechat/room.resumeOnHold': {
		POST: (params: { roomId: string }) => {
			success: boolean;
		};
	};
};
