import {
  type ApprovalGroupTask,
  ApprovalPolicyType,
  ApprovalStateName,
  type State,
  type StateTransition,
} from 'src/pages/dashboard/serviceManagement/types';
import { type LokaliseTFunction } from 'src/utils/i18n';

import DatePickerControl from '../../nemakEvents/components/formControls/DatePickerControl';

import { getStateName, ServiceManagementTaskStateName } from './state';

export const ServiceManagementApprovalStateDictionary: {
  [key: string]: ApprovalStateName;
} = {
  APROBADA: ApprovalStateName.APPROVED,
  APPROVED: ApprovalStateName.APPROVED,
  RECHAZADA: ApprovalStateName.REJECTED,
  REJECTED: ApprovalStateName.REJECTED,
  CANCELADA: ApprovalStateName.CANCELLED,
  CANCELLED: ApprovalStateName.CANCELLED,
  PENDING: ApprovalStateName.PENDING,
  PENDIENTE: ApprovalStateName.PENDING,
  SOLICITADA: ApprovalStateName.PENDING,
  REQUESTED: ApprovalStateName.PENDING,
};

export const getApprovalStateName = (value: string) => {
  return ServiceManagementApprovalStateDictionary[value] || undefined;
};

export const getApprovalPillType = (state: State) => {
  const stateName = getApprovalStateName(state?.name);
  switch (stateName) {
    case ApprovalStateName.APPROVED:
      return 'success';
    case ApprovalStateName.REJECTED:
      return 'error';
    case ApprovalStateName.CANCELLED:
      return 'disabled';
    default:
      return 'neutral';
  }
};

export const getApprovalPillDescription = (
  state: State | ApprovalStateName,
  user: string | null,
  date: string,
  t: LokaliseTFunction,
) => {
  const stateKey = typeof state === 'string' ? state : state.name;
  const stateName = getApprovalStateName(stateKey);
  switch (stateName) {
    case ApprovalStateName.APPROVED:
      return t(`approvals.approved_at`, { date });
    case ApprovalStateName.REJECTED:
      return t(`approvals.rejected_at`, { date });
    case ApprovalStateName.CANCELLED:
      return t(`approvals.cancelled_at`, { date });
    case ApprovalStateName.PENDING:
      return !user ? t(`approvals.requested_at`, { date }) : date;
    default:
      return '';
  }
};

export const getApprovalPolicy = (
  status: ApprovalStateName,
  policyType: ApprovalPolicyType,
) => {
  const policyKey = policyType === ApprovalPolicyType.ALL ? 'all' : 'any';

  let statusKey: string;
  switch (status) {
    case ApprovalStateName.APPROVED:
      statusKey = 'approved';
      break;
    case ApprovalStateName.REJECTED:
      statusKey = 'rejected';
      break;
    case ApprovalStateName.PENDING:
    default:
      statusKey = 'pending';
      break;
  }

  return `approvals.policies.${policyKey}.${statusKey}`;
};

export const calculateGroupApprovalStatus = (
  tasks: ApprovalGroupTask[],
  policyType: ApprovalPolicyType,
): ApprovalStateName => {
  switch (policyType) {
    case ApprovalPolicyType.ALL: {
      const hasRejected = tasks.some(
        task =>
          getApprovalStateName(task.state?.name) === ApprovalStateName.REJECTED,
      );
      if (hasRejected) return ApprovalStateName.REJECTED;

      const allApproved = tasks.every(
        task =>
          getApprovalStateName(task.state?.name) === ApprovalStateName.APPROVED,
      );
      if (allApproved) return ApprovalStateName.APPROVED;
      break;
    }
    default:
    case ApprovalPolicyType.ANY: {
      const hasApproved = tasks.some(
        task =>
          getApprovalStateName(task.state?.name) === ApprovalStateName.APPROVED,
      );
      if (hasApproved) return ApprovalStateName.APPROVED;

      const allRejected = tasks.every(
        task =>
          getApprovalStateName(task.state?.name) === ApprovalStateName.REJECTED,
      );
      if (allRejected) return ApprovalStateName.REJECTED;
    }
  }
  return ApprovalStateName.PENDING;
};

export const getApprovalAlertProps = (
  state: string,
): {
  title: string;
  severity: 'success' | 'error' | 'info';
} => {
  switch (getApprovalStateName(state)) {
    case ApprovalStateName.APPROVED:
      return {
        title: 'approvals.approved_request',
        severity: 'success',
      };
    case ApprovalStateName.REJECTED:
      return {
        title: 'approvals.rejected_request',
        severity: 'error',
      };
    default:
    case ApprovalStateName.CANCELLED:
      return {
        title: 'approvals.cancelled_request',
        severity: 'info',
      };
  }
};

export const isTerminal = (newState: StateTransition) => {
  return [
    ServiceManagementTaskStateName.CANCELLED,
    ServiceManagementTaskStateName.CLOSED,
  ].includes(getStateName(newState.stateName));
};
