// From Redis objects
export class AuthError extends Error {
  constructor(message: string) {
    super(message);
    this.name = 'AuthError';
  }
}

interface ClientOnline {
  lastLogin: Date;
  lastLogout: Date;
  lastAction: Date;
  lastPage: string;
  userType: string;
  lang: string;
  timezone: string;
}

interface ClientPageConfig {
  mode: string;
  textFont: number;
  theme: string;
}

interface ClientHeader {
  header: any[];
  activeDomain: string;
  listOfDomains: string[];
  connections: any[];
}

interface ClientMenu {
  selectionList: string[];
  activeSelection: string;
}

interface ClientSelection {
  selectionList: any[];
  activeSelection: Record<string, any>;
}

interface ClientUser {
  uuid: string;
  avatar: string;
  email: string;
  phone_number: string;
  user_tag: string;
  password_expiry_begins: string;
  person: {
    uuid: string;
    firstname: string;
    surname: string;
    middle_name: string;
    sex_code: string;
    person_tag: string;
    country_code: string;
    birth_date: string;
  };
}

interface ClientSettings {
  lastOnline: Date;
  token: string;
}

interface LinkList {
  linkList: any[];
}

interface ClientRedisToken {
  online: ClientOnline;
  pageConfig: ClientPageConfig;
  menu: ClientMenu;
  header: ClientHeader;
  selection: ClientSelection;
  user: ClientUser;
  settings: ClientSettings;
  chatRoom: LinkList;
  notifications: LinkList;
  messages: LinkList;
}

const defaultClientOnline: ClientOnline = {
  lastLogin: new Date(),
  lastLogout: new Date(),
  lastAction: new Date(),
  lastPage: "/dashboard",
  userType: "employee",
  lang: "tr",
  timezone: "GMT+3",
};

const defaultClientPageConfig: ClientPageConfig = {
  mode: "light",
  textFont: 14,
  theme: "default",
};

const defaultClientMenu: ClientMenu = {
  selectionList: [],
  activeSelection: "/dashboard",
};

const defaultClientHeader: ClientHeader = {
  header: [],
  activeDomain: "",
  listOfDomains: [],
  connections: [],
};

const defaultClientSelection: ClientSelection = {
  selectionList: [],
  activeSelection: {},
};

const defaultClientUser: ClientUser = {
  uuid: "",
  avatar: "",
  email: "",
  phone_number: "",
  user_tag: "",
  password_expiry_begins: new Date().toISOString(),
  person: {
    uuid: "",
    firstname: "",
    surname: "",
    middle_name: "",
    sex_code: "",
    person_tag: "",
    country_code: "",
    birth_date: "",
  },
};

const defaultClientSettings: ClientSettings = {
  lastOnline: new Date(),
  token: "",
};

const defaultLinkList: LinkList = {
  linkList: [],
};

const defaultClientRedisToken: ClientRedisToken = {
  online: defaultClientOnline,
  pageConfig: defaultClientPageConfig,
  menu: defaultClientMenu,
  header: defaultClientHeader,
  selection: defaultClientSelection,
  user: defaultClientUser,
  settings: defaultClientSettings,
  chatRoom: defaultLinkList,
  notifications: defaultLinkList,
  messages: defaultLinkList,
};

const generateRedisKey = (userType: string, uuId: string) => {
  const userTypeToUpper = userType.toUpperCase();
  if (!userTypeToUpper || !uuId) throw new Error("Invalid user type or uuId");
  return `CLIENT:${userTypeToUpper}:${uuId}`;
};

const readRedisKey = (redisKey: string) => {
  if (redisKey.split(":").length !== 2) throw new Error("Invalid redis key");
  const userTypeToUpper = redisKey.split(":")[1];
  const uuId = redisKey.split(":")[2];
  if (!userTypeToUpper || !uuId) throw new Error("Invalid user type or uuId");
  return { userType: userTypeToUpper.toLowerCase(), uuId };
};

export {
  defaultClientOnline,
  defaultClientPageConfig,
  defaultClientMenu,
  defaultClientHeader,
  defaultClientSelection,
  defaultClientUser,
  defaultClientSettings,
  defaultLinkList,
  defaultClientRedisToken,
};
export type {
  ClientOnline,
  ClientPageConfig,
  ClientMenu,
  ClientHeader,
  ClientSelection,
  ClientUser,
  ClientSettings,
  LinkList,
  ClientRedisToken,
};
export { generateRedisKey, readRedisKey };
