import { ToastStyles } from "@/twui/components/elements/Toast";
import { DATASQUIREL_LoggedInUser } from "@moduletrace/datasquirel/dist/package-shared/types";
import useAppInit from "../hooks/use-app-init";
import { ServerWebSocket } from "bun";
import { ChildProcess } from "child_process";
import { NSQLITE_TURBOCI_ADMIN_USERS } from "../db/types";
import {
    NormalizedServerObject,
    ParsedDeploymentServiceConfig,
    TCIGlobalConfig,
} from "./turboci";

export type User = DATASQUIREL_LoggedInUser & {
    super_admin?: boolean;
};

export type ServiceScriptObject = {
    sh: string;
    service_name: string;
    deployment_name: string;
    work_dir?: string;
};

export type URLQueryType = {
    service_name?: string | null;
    deployment_user_id?: string | null;
};

export type PagePropsType = {
    deployment?: TCIGlobalConfig | null;
    query?: URLQueryType | null;
    user: User;
    pageUrl?: string | null;
    deployment_id?: string | null;
    service?: ParsedDeploymentServiceConfig | null;
    children_services?: ParsedDeploymentServiceConfig[] | null;
    deployment_users?: NSQLITE_TURBOCI_ADMIN_USERS[] | null;
    deployment_user?: NSQLITE_TURBOCI_ADMIN_USERS | null;
};

export type APIReqObject = {
    username?: string;
    email?: string;
    password?: string;
    new_user?: TurboCISignupFormObject;
    user_id?: string | number;
};

export type LoginFormData = {
    username?: string;
    email?: string;
    password?: string;
};

export type AlertObject = {
    text?: string;
    field_name?: string;
};

export type WebSocketData = {
    user: User;
};

export type TurboCISignupFormObject = {
    first_name?: string;
    last_name?: string;
    email?: string;
    password?: string;
    confirmed_password?: string;
    username?: string;
    image?: string;
};

export type TurboCIAdminAppContextType = ReturnType<typeof useAppInit>;

export const WebSocketEvents = [
    "client:ping",
    "client:service-server-logs",
    "client:kill-port",
    "client:kill-all-ports",
    "client:service-server-shell",

    "server:ping",
    "server:error",
    "server:message",
    "server:ready",
    "server:success",
    "server:update",
    "server:service-server-logs",
    "server:killed-port",
    "server:killed-all-ports",
    "server:service-server-shell",
] as const;

export type WebSocketDataType = {
    event: (typeof WebSocketEvents)[number];
    message?: string;
    service?: Omit<ParsedDeploymentServiceConfig, "servers">;
    server?: NormalizedServerObject;
    ttyd?: TtydInfoObject;
    port?: string | number;
    cmd?: string;
};

export type WebSocketMessageParam = {
    ws: ServerWebSocket<WebSocketData>;
    message?: string | Buffer;
    data?: WebSocketDataType;
};

export type WebSocketType = {
    socket?: WebSocket;
    data?: WebSocketDataType | null;
    message?: string;
    sendData?: (data: WebSocketDataType) => void;
};

export type ToastType = {
    toastStyle?: (typeof ToastStyles)[number];
    toastMessage?: string;
    toastOpen: boolean;
    closeDelay?: number;
};

export type TtydInfoObject = {
    url: string;
    href?: string;
    port: number;
};

export const PrivateServerTtydParadigms = [
    {
        name: "logs",
    },
    {
        name: "terminal",
    },
] as const;

export type WebSocketConnectedUserData = {
    user: User;
    child_processes: ChildProcess[];
    ports: (string | number)[];
};

export const ServerTerminalTargets = [
    { name: "logs" },
    { name: "shell" },
] as const;
