import { IPayload } from "../SysAdminPayload/SysAdminPayload.interface";
import { IWapServerDocument } from "./WapServer.Adapter.Mongoose";

export type FindWapServerParams = {
    id?: string;
    name?: string;
    description?: string;
    countBots?: number;
    maxBots?: number;
    active?: boolean;
    countBotsFrom?: number;
    countBotsTo?: number;
};

export type AssingAvailableServerParams = {
    companyId: string;
    sessionUser: string;
};

export type BotEventParams = AssingAvailableServerParams & {};

export type BotRunningStatus =
    | "created"
    | "running"
    | "paused"
    | "restarting"
    | "removing"
    | "exited"
    | "dead";

export type BotEventError = {
    error: string;
    code: number;
};

export enum SERVER_STATE {
    "RUNNING" = "running",
    "NOT_WORKING" = "not_working",
    "NOT_ASSIGNED" = "not_assigned",
}

export enum BOT_STATE {
    "EXISTS" = "exists",
    "NOT_EXISTS" = "not_exists",
}

export enum BOT_SESSION_STATUS {
    "LOGED" = "ok",
    "UNLOGED" = "fail",
}

export type BotCheckStatus = {
    state: BOT_STATE;
    status: BotRunningStatus;
    serverState: SERVER_STATE;
};

export type BotCheckSession = {
    session: BOT_SESSION_STATUS;
};

export type SendBotMessageParams = {
    companyId: string;
    number: string;
    message: string;
    sessionUser: string;
};

export enum VALIDATE_BOT_ENVENT_ERRORS {
    "INVALID_USER" = "invalid_user",
    "INVALID_ORG" = "invalid_org",
    "NO_PERMISSION" = "no_permission",
    "NO_BOT_ALLOWED" = "no_bot_allowed",
    "NO_SUBSCRIPTION_ACTIVE" = "no_subscription_active",
    "NO_SERVER_ASSIGNED" = "no_server_assigned",
}
export interface ValidateBotEventStatus {
    status: boolean;
    error: string;
    serverUrl?: string;
    code?: VALIDATE_BOT_ENVENT_ERRORS;
}

export interface BotView {
    exists: boolean;
    runningState: BotRunningStatus;
    serverId?: string;
    serverName?: string;
    serverDescription?: string;
    serverState?: SERVER_STATE;
    botSession?: BOT_SESSION_STATUS;
    automaticNotifications: boolean;
}

export interface IWapServer {
    id?: string;
    name?: string;
    description?: string;
    ipv4: string;
    ipv6: string;
    countBots: number;
    maxBots: number;
    active: boolean;
}

export type PaginateWapServerParams = FindWapServerParams & {
    page: number;
    limit: number;
};

export type PaginateWapServerResults = {
    data: IWapServer[];
    page: number;
    pages: number;
};

export type SysAdminGetWapServerParams = PaginateWapServerParams & {
    payload: IPayload;
};

export interface IWapServerAdapter {
    find(data: FindWapServerParams): Promise<IWapServer[]>;
    findOne(data: FindWapServerParams): Promise<IWapServerDocument | null>;
    paginate(filters: PaginateWapServerParams): Promise<PaginateWapServerResults>;
}

export interface IWapServerManager {
    servers: IWapServerAdapter;
    findServers(): Promise<IWapServer[]>;
    getAvailableServer(): Promise<IWapServer>;
    assignAvailableServer(data: AssingAvailableServerParams): Promise<IWapServer>;
    quitServer(data: AssingAvailableServerParams): Promise<void>;
    createBot(data: BotEventParams): Promise<void>;
    startBot(data: BotEventParams): Promise<void>;
    stopBot(data: BotEventParams): Promise<void>;
    deleteBot(data: BotEventParams): Promise<void>;
    checkBot(data: BotEventParams): Promise<BotCheckStatus>;
    checkSession(data: BotEventParams): Promise<BotCheckSession>;
    botView(data: BotEventParams): Promise<BotView>;
    getQr(data: BotEventParams): Promise<string>;
    sendMessage(data: SendBotMessageParams): Promise<void>;
    sysAdminGetWapServers(data: PaginateWapServerParams): Promise<PaginateWapServerResults>;
}
