import { type Theme } from '@material-hu/mui/styles';

import { type PillsProps } from '@material-hu/components/design-system/Pills/types';

import { type MeUser } from 'src/types/user';
import { type TCustomFunction } from 'src/utils/i18n';
import { type PaginatedResponse, type Pagination } from 'src/utils/tableUtils';

import {
  type CertificateAction,
  type CertificateAlignment,
  type CertificateColor,
  type CertificateInUseToastNeutral,
  type CertificateStatus,
  type CertificateType,
  type ListLearningCertificatesOrderBy,
} from './constants';

export type CertificateActionType =
  (typeof CertificateAction)[keyof typeof CertificateAction];

export type CertificateStatusAction =
  | typeof CertificateAction.ACTIVATE
  | typeof CertificateAction.DEACTIVATE;

export type CertificateInUseAction =
  | typeof CertificateAction.DEACTIVATE
  | typeof CertificateAction.DELETE;

export type CertificateInUseToastAction =
  | CertificateInUseAction
  | typeof CertificateInUseToastNeutral;

export type CertificateStatusInfo = {
  type: PillsProps['type'];
  labelKey: 'general:active' | 'general:inactive';
};

export type CertificateStatusType =
  (typeof CertificateStatus)[keyof typeof CertificateStatus];

export type ListLearningCertificatesOrderByValue =
  (typeof ListLearningCertificatesOrderBy)[keyof typeof ListLearningCertificatesOrderBy];

export type LearningCertificateType =
  (typeof CertificateType)[keyof typeof CertificateType];

export type LearningCertificateAlignment =
  (typeof CertificateAlignment)[keyof typeof CertificateAlignment];

export type LearningCertificateColor =
  (typeof CertificateColor)[keyof typeof CertificateColor];

export type CertificateSignature = {
  fileAssetId: number;
  signerName: string | null;
  signerTitle: string | null;
};

export type ToBackendOrderByArgs = {
  orderBy: string;
  order: string;
};

export type CertificateFileAsset = {
  id: number;
  url: string;
};

export type LearningCertificateListItem = {
  id: number;
  name: string;
  title: string;
  subtitle: string | null;
  description: string | null;
  type: LearningCertificateType;
  pictureId: number | null;
  pictureUrl: string | null;
  color: LearningCertificateColor | null;
  alignment: LearningCertificateAlignment;
  signatures: CertificateSignature[] | null;
  logoFileAssetIds: number[] | null;
  status: CertificateStatusType;
  instanceId: number;
  createdAt: string;
  updatedAt: string;
  signaturesFileAssets: CertificateFileAsset[] | null;
  logoFileAssets: CertificateFileAsset[] | null;
  coursesCount: number;
  learningPathsCount: number;
  learningSessionsCount: number;
};

export type ListLearningCertificatesParams = Pick<
  Pagination,
  'page' | 'limit' | 'q'
> & {
  orderBy?: ListLearningCertificatesOrderByValue;
};

export type ListLearningCertificatesResponse =
  PaginatedResponse<LearningCertificateListItem>;

export type LearningCertificateDTO = Omit<
  LearningCertificateListItem,
  'coursesCount' | 'learningPathsCount' | 'learningSessionsCount'
>;

export type LearningCertificateUsage = Pick<
  LearningCertificateListItem,
  'coursesCount' | 'learningPathsCount' | 'learningSessionsCount'
>;

/** Signature entry accepted by the certificate PATCH endpoint. */
export type CertificateSignaturePayload = {
  fileAssetId: number;
  signerName?: string | null;
  signerTitle?: string | null;
};

/** Body for `POST /learning-certificates`. Creates the certificate draft. */
export type CreateLearningCertificateRequest = {
  name: string;
  type: LearningCertificateType;
  title: string;
  subtitle?: string | null;
  description?: string | null;
  pictureId?: number | null;
  color?: LearningCertificateColor | null;
  status?: CertificateStatusType;
};

/**
 * Body for `PATCH /learning-certificates/:id`. All fields optional; also
 * accepts design fields the POST rejects (alignment, signatures, logos).
 */
export type UpdateLearningCertificateRequest = {
  name?: string;
  type?: LearningCertificateType;
  title?: string;
  subtitle?: string | null;
  description?: string | null;
  pictureId?: number | null;
  color?: LearningCertificateColor | null;
  alignment?: LearningCertificateAlignment;
  signatures?: CertificateSignaturePayload[];
  logoFileAssetIds?: number[];
  status?: CertificateStatusType;
};

export type UpdateLearningCertificateParams = {
  certificateId: number;
  data: UpdateLearningCertificateRequest;
};

export type PatchLearningCertificateStatusParams = {
  certificateId: number;
  certificateName: string;
  status: CertificateStatusType;
};

export type DeleteLearningCertificateParams = {
  certificateId: number;
  certificateName: string;
};

export type LearningCertificatesListItemsTransform = (
  items: LearningCertificateListItem[],
) => LearningCertificateListItem[];

export type CertificatePreviewContext = {
  user: MeUser | null;
  instanceColor: string | undefined;
  theme: Theme;
  t: TCustomFunction;
};
