import { type Pagination } from 'src/types/services';

import { type Links } from './actionLinks';

export enum ActionTagUser {
  DELETETAGUSER = 'deleteTagUser',
  ADDUSER = 'addTagUser',
}

export enum KEYS {
  BACKSPACE = 'Backspace',
}

export type TaggedUser = {
  start: number;
  length: number;
  type: {
    user: {
      id: number;
    };
  };
};

export type SegmentationItem = {
  id: number;
  groupId: number;
  name: string;
  position?: number;
  resetAcknowledgmentItem?: boolean;
  usersCount: number;
};

export type Segmentation = {
  id: number;
  name?: string;
  items?: SegmentationItem[];
  item?: SegmentationItem;
};

export type SocialNetworksType = {
  linkedIn?: string | null;
  facebook?: string | null;
  twitter?: string | null;
  instagram?: string | null;
  allowWhatsApp?: true | null;
};

/**
 * Source of truth for the authenticated user — fields returned by GET /janus/me.
 * All consumers that only need the logged-in user's data should use this type.
 */
export type MeUser = {
  id: number;
  employeeInternalId: string;
  firstName: string;
  lastName: string;
  email: string | null;
  language: string;
  deleted: boolean;
  profilePicture: string | null;
  phoneNumber: string | null;
  birthdate: string | null;
  hiringDate: string | null;
  hasBeenLogged: boolean;
  hasPasswordChanged: boolean;
  postTranslationLanguage: string | null;
  isAgent: boolean;
  lastTermsAndConditionsAccepted: boolean | null;
  acknowledgementsToGiveLeft: number;
  profileData: Record<string, unknown> | null;
  workdays: string[];
  socialNetworks: SocialNetworksType;
  department: { id: number; name: string } | null;
  jobPosition: { id: number; name: string } | null;
  color?: string;
};

/**
 * Full user shape from user-management endpoints (GET /users/:id, user lists, etc.).
 * Extends MeUser with fields that do NOT come from /me.
 */
export type User = Omit<MeUser, 'department'> & {
  bossId?: number;
  fullName?: string;
  isUserBirthday?: boolean;
  isUserAnniversary?: boolean;
  createdAt: string;
  active: boolean;
  coverPicture: string;
  nickname?: string;
  color: string;
  isAdmin?: boolean;
  isExternal: boolean;
  _links: Links;
  department: { id: number; name: string } | null;
};

export type UserDictionaryItem = {
  character: string;
  users: User[];
};

// Response
export type UserListResponse = Pagination<User[]>;

export type UserListSegmentationResponse = Pagination<User[]>;

export type SegmentationReachResponse = {
  total: number;
  active: number;
};

// Socket
export type UserDeletedSocket = {
  id: number;
};

// Data
export type SignUpWithInviteData = {
  firstName: string;
  lastName: string;
  inviteCode: string;
  language: string;
  passwordInput: string;
  repeatPasswordInput: string;
  orgName: string;
};

export type JoinWithInviteData = {
  firstName: string;
  lastName: string;
  email: string | null;
  inviteCode: string;
  language: string;
  passwordInput: string;
  repeatPasswordInput: string;
  employeeInternalId: string;
  orgName: string;
};

export type UserEditableData = {
  phoneNumber: string;
  linkedIn: string;
  instagram: string;
  facebook: string;
  twitter: string;
  firstName: string;
  lastName: string;
  nickname: string;
  profileData: object;
  birthdate: string;
  email: string;
  allowWhatsApp: boolean;
};

type ProfileDataField = {
  value: string | number | string[] | number[] | UserProfile | UserProfile[];
  name: string;
  type: string;
};

export type UserProfile = Pick<
  User,
  | 'birthdate'
  | 'coverPicture'
  | 'email'
  | 'firstName'
  | 'nickname'
  | 'hiringDate'
  | 'id'
  | 'lastName'
  | 'profilePicture'
  | 'socialNetworks'
  | 'jobPosition'
  | 'phoneNumber'
> & {
  profileData: Record<string, ProfileDataField>;
  bosses: UserProfile[];
  reviewer: UserProfile;
  segmentations: Required<Segmentation>[];
  department: User['jobPosition'];
  timeTrackingUserSettings?: { kioskPin?: string };
};
