import { type TablerIcon } from '@material-hu/icons/tabler';

import { type Pagination } from 'src/utils/tableUtils';

import {
  type LearningSegmentation,
  type SessionsAudienceResponse,
} from '../common/types';

import {
  type SessionModes,
  type SessionStatus,
  SessionSteps,
} from './constants';

export type SessionModeType = (typeof SessionModes)[keyof typeof SessionModes];

export type SessionStatusType =
  (typeof SessionStatus)[keyof typeof SessionStatus];

export type SessionStepsType = (typeof SessionSteps)[keyof typeof SessionSteps];

export type SessionsStats = {
  userAssistedCount: number;
  totalUserFinishedSessionsCount: number;
  virtualCount: number;
  presentialCount: number;
  draftCount: number;
  finishedCount: number;
  publishedCount: number;
  totalCount: number;
};

export type SessionTableItem = {
  id: number;
  title: string;
  type: SessionModeType;
  status: SessionStatusType;
  localDate?: string;
  startTime?: string;
  endTime?: string;
  timeZone?: string;
  totalConfirmed?: string;
  totalAssisted?: string;
};

export type SessionAttendanceStats = {
  totalAssigned: number;
  confirmed: number;
  notConfirmed: number;
  assisted: number;
  notAssisted: number;
  pendingAssistance: number;
};

export type EditPartialSessionData = {
  sessionId: number;
  data: Partial<SessionDetail>;
};

type SessionBaseFields = {
  title: string;
  localDate: string;
  startTime: string;
  endTime: string;
  timeZone: string;
  description?: string;
  type: SessionModeType;
  address?: string;
  meetingLink?: string;
  collaborator?: string;
  sendNotification: boolean;
};

export type CreateLearningSession = SessionBaseFields & {
  pictureId?: number | null;
};

export type UpdateLearningSession = Partial<SessionBaseFields> & {
  pictureId?: number | null;
  status?: SessionStatusType;
};

export type SessionDetail = Omit<SessionBaseFields, 'sendNotification'> & {
  id: number;
  pictureUrl?: string;
  status: SessionStatusType;
  creatorId: number;
  instanceId: number;
  endDate: string;
  startDate: string;
  audiencesQuery: SessionsAudienceResponse | null;
  isAudienceDefined: boolean;
  isPublic: boolean;
  isObligatory: boolean;
  sendNotification?: boolean;
  issueCertificate: boolean;
};

export type SessionBasicInformation = {
  id?: number;
  title: string;
  description?: string;
  coverPicture?: SessionCoverPicture;
  localDate: Date | null;
  startTime: Date | null;
  endTime: Date | null;
  timeZone: string;
  // The next types are used to check conditions, not used on the form
  status?: SessionStatusType;
  isAudienceDefined?: boolean;
};

type SessionCoverPicture = {
  original?: File;
  cropped?: File;
  url?: string;
};

export type SessionConfiguration = {
  type: SessionModeType;
  address?: string;
  meetingLink?: string;
  collaborator?: string;
  sendNotification: boolean;
  isObligatory: boolean;
  issueCertificate: boolean;
};

export type SessionAttendance = {
  assisted: boolean;
  confirmedAssistance: boolean;
  fullName: string;
  id: number;
  learningSessionId: number;
  userId: number;
};

// Values
export type NewSessionFormValues = {
  [SessionSteps.BASIC_INFORMATION]: SessionBasicInformation;
  [SessionSteps.CONFIGURATION]: SessionConfiguration;
  [SessionSteps.SEGMENTATION]: LearningSegmentation | null;
};

// Params
export type SessionsPaginationParams = Pick<Pagination, 'page' | 'limit' | 'q'>;

export type AttendanceSidebarItem = {
  id: string;
  copetin: string;
  title?: string;
  icon: TablerIcon;
  action?: {
    Icon: TablerIcon;
    onClick: () => void;
  };
  enabled?: boolean;
};

export type UpdateSessionAttendanceParams = {
  userIds: number[];
  assisted: boolean;
};

export type DownloadSessionReportParams = {
  sessionId: number;
  email: string;
  includeGroups?: number[];
  includeUserFields?: string[];
};

export type DownloadReportFnParams = {
  email: string;
  includeGroups: number[];
  includeUserFields: string[];
};

export type DownloadReportFormValues = {
  includeGroups: boolean;
  includedGroups: number[];
  includeUserFields: boolean;
  includedUserFields: string[];
  email: string;
};
