import { type FC } from 'react';
import { type TFunction, Trans } from 'react-i18next';

import Stack from '@material-hu/mui/Stack';

import HuTitle from '@material-hu/components/design-system/Title';

import { useAuth } from 'src/contexts/JWTContext';
import {
  getStateName,
  getStatusDetails,
  getTaskID,
  ServiceManagementTaskStateName,
} from 'src/pages/dashboard/serviceManagement/utils';
import {
  type Event,
  EventsType,
  ServiceItemTypes,
  type UserTask,
} from 'src/pages/dashboard/serviceManagement/types';
import { getUserDisplayName } from 'src/pages/dashboard/serviceManagement/utils';
import { formatLocalizedDate } from 'src/utils/date';
import { useLokaliseTranslation } from 'src/utils/i18n';

type Props = {
  activity: Event;
};

const ActivityDescription: FC<Props> = ({ activity }) => {
  const { t } = useLokaliseTranslation('service_management');
  const { user: loggedUser } = useAuth();

  const getUserName = (user: UserTask) => {
    return getUserDisplayName(user, loggedUser!, t);
  };

  const renderDescription = () => {
    const taskId = getTaskID(activity?.task);
    const timestamp = formatLocalizedDate(new Date(activity?.timestamp), {
      includeTime: true,
    });
    const { metadata, taskType } = activity;

    switch (activity.type) {
      case EventsType.TASK_CLOSED_MANUALLY:
      case EventsType.TASK_CLOSED_AUTOMATICALLY:
        return (
          <HuTitle
            variant="S"
            copetin={timestamp}
            title={t(
              getStateName(activity.taskState.name) ===
                ServiceManagementTaskStateName.CANCELLED
                ? 'subtasks.activity.closed_automatically'
                : 'subtasks.activity.closed_manually',
              { taskName: activity.task?.name },
            )}
            description={taskId}
          />
        );
      case EventsType.TASK_STARTED:
        return (
          <HuTitle
            variant="S"
            copetin={timestamp}
            title={
              taskType === ServiceItemTypes.SUBTASK
                ? t('subtasks.activity.started_title', {
                    taskName: activity.task?.name,
                  })
                : t('request_started')
            }
            description={taskId}
          />
        );
      case EventsType.TASK_UPDATED: {
        const stateName = t(
          getStatusDetails(getStateName(activity.taskState.name))?.title,
        );
        return (
          <HuTitle
            variant="S"
            copetin={timestamp}
            title={
              taskType === ServiceItemTypes.SUBTASK ? (
                <Trans
                  i18nKey={'service_management:subtasks.activity.updated_title'}
                  values={{
                    stateName: stateName,
                    taskName: activity.task?.name,
                  }}
                  t={t as TFunction}
                />
              ) : (
                <Trans
                  i18nKey={'service_management:request_state_updated'}
                  values={{
                    state: stateName,
                  }}
                  t={t as TFunction}
                />
              )
            }
            description={taskId}
          />
        );
      }
      case EventsType.TASK_ASSIGNED:
        return (
          <HuTitle
            variant="S"
            copetin={timestamp}
            title={
              taskType === ServiceItemTypes.SUBTASK
                ? t('subtasks.activity.assigned_title')
                : t('agent_assigned_event_title')
            }
            description={
              <Trans
                i18nKey="service_management:agent_assigned_event_description"
                values={{
                  taskId: taskId,
                  agent: getUserName(activity.assignee as UserTask),
                }}
                t={t as TFunction}
              />
            }
          />
        );
      case EventsType.TASK_REASSIGNED:
        return (
          <HuTitle
            variant="S"
            copetin={timestamp}
            title={
              taskType === ServiceItemTypes.SUBTASK
                ? t('subtasks.activity.reassigned_title')
                : t('agent_reassigned_event_title')
            }
            description={
              activity.user?.id ? (
                <Trans
                  i18nKey="service_management:agent_reassigned_event_description"
                  values={{
                    user: getUserName(activity.user as UserTask),
                    agent: getUserName(activity.assignee as UserTask),
                  }}
                  t={t as TFunction}
                />
              ) : (
                <Trans
                  i18nKey="service_management:agent_automaticaly_reassigned_event_description"
                  values={{
                    agent: getUserName(activity.assignee as UserTask),
                  }}
                  t={t as TFunction}
                />
              )
            }
          />
        );
      case EventsType.APPROVAL_TASK_APPROVED:
        return (
          <HuTitle
            variant="S"
            copetin={timestamp}
            title={t('approvals.activity.approval_task_approved', {
              name: getUserName(activity.user as UserTask),
            })}
            description={metadata?.message}
            withEllipsis
            overflow="tooltip"
          />
        );
      case EventsType.APPROVAL_TASK_REJECTED:
        return (
          <HuTitle
            variant="S"
            copetin={timestamp}
            title={t('approvals.activity.approval_task_rejected', {
              name: getUserName(activity.user as UserTask),
            })}
            description={metadata?.message}
            withEllipsis
            overflow="tooltip"
          />
        );
      case EventsType.APPROVAL_GROUP_APPROVED:
        return (
          <HuTitle
            variant="S"
            copetin={timestamp}
            title={t('approvals.activity.approval_group_approved', {
              name: metadata?.approvalName,
            })}
          />
        );
      case EventsType.APPROVAL_GROUP_REJECTED:
        return (
          <HuTitle
            variant="S"
            copetin={timestamp}
            title={t('approvals.activity.approval_group_rejected', {
              name: metadata?.approvalName,
            })}
          />
        );
      case EventsType.APPROVAL_GROUP_CREATED:
        return (
          <HuTitle
            variant="S"
            copetin={timestamp}
            title={t('approvals.activity.approval_group_created', {
              name: metadata?.approvalName,
            })}
            description={t('approvals.activity.approvals_needed', {
              count: metadata?.approvalsNeeded,
            })}
          />
        );
      case EventsType.APPROVAL_GROUP_CANCELLED:
        return (
          <HuTitle
            variant="S"
            copetin={timestamp}
            title={t('approvals.activity.approval_group_cancelled', {
              name: metadata?.approvalName,
            })}
          />
        );
      default:
        // Return null for unknown activity types
        return null;
    }
  };

  const description = renderDescription();

  // Unknown activity types
  if (!description) {
    return null;
  }

  return <Stack>{description}</Stack>;
};

export default ActivityDescription;
