import { ReviewDirection } from 'src/pages/dashboard/performance/types';

const PEER_REVIEW_DIRECTIONS: readonly ReviewDirection[] = [
  ReviewDirection.PEER_EXTERNAL_REVIEW,
  ReviewDirection.PEER_REVIEW,
  ReviewDirection.OTHERS_REVIEW,
];

export const AUTOSAVE_DEBOUNCE_MS = 500;

const REASON_TRANSLATION_KEYS = [
  'form.request_edit.reasons.typo',
  'form.request_edit.reasons.add_or_remove',
  'form.request_edit.reasons.rewrite',
  'form.request_edit.reasons.other',
] as const;

enum FinishReviewModalDescriptionKey {
  DESCRIPTION_SELF = 'form.finish_review_modal.new_descriptions.description_self',
  DESCRIPTION_REST_SHARED = 'form.finish_review_modal.new_descriptions.description_rest_shared',
  DESCRIPTION_REST_NOT_SHARED = 'form.finish_review_modal.new_descriptions.description_rest_not_shared',
  DESCRIPTION_DOWNWARD_NOT_SHARED = 'form.finish_review_modal.new_descriptions.description_downward_not_shared',
  DESCRIPTION_DOWNWARD_SHARED_BEFORE_CYCLE_FINISH = 'form.finish_review_modal.new_descriptions.description_downward_shared_before_cycle_finish',
  DESCRIPTION_DOWNWARD_SHARED = 'form.finish_review_modal.new_descriptions.description_downward_shared',
  DESCRIPTION_UPWARD_SHARED = 'form.finish_review_modal.new_descriptions.description_upward_shared',
  DESCRIPTION_UPWARD_NOT_SHARED = 'form.finish_review_modal.new_descriptions.description_upward_not_shared',
}

export const getReasonTranslationKey = (index: number): string => {
  return REASON_TRANSLATION_KEYS[index] || REASON_TRANSLATION_KEYS[3];
};

export const getFinishReviewModalDescriptionKey = (
  direction: ReviewDirection | undefined,
  canBeShared: boolean,
  canBeSharedBeforeCycleFinish: boolean,
): string => {
  // Handle undefined direction
  if (!direction) {
    return FinishReviewModalDescriptionKey.DESCRIPTION_REST_SHARED;
  }

  // Self-review (auto-review)
  if (direction === ReviewDirection.AUTO_REVIEW) {
    return FinishReviewModalDescriptionKey.DESCRIPTION_SELF;
  }

  // Peer reviews (external, peer, or others)
  if (PEER_REVIEW_DIRECTIONS.includes(direction)) {
    return canBeShared
      ? FinishReviewModalDescriptionKey.DESCRIPTION_REST_SHARED
      : FinishReviewModalDescriptionKey.DESCRIPTION_REST_NOT_SHARED;
  }

  // Downward review (subordinate review)
  if (direction === ReviewDirection.SUBORDINATE_REVIEW) {
    if (!canBeShared) {
      return FinishReviewModalDescriptionKey.DESCRIPTION_DOWNWARD_NOT_SHARED;
    }

    return canBeSharedBeforeCycleFinish
      ? FinishReviewModalDescriptionKey.DESCRIPTION_DOWNWARD_SHARED_BEFORE_CYCLE_FINISH
      : FinishReviewModalDescriptionKey.DESCRIPTION_DOWNWARD_SHARED;
  }

  // Upward review (boss review)
  if (direction === ReviewDirection.BOSS_REVIEW) {
    return canBeShared
      ? FinishReviewModalDescriptionKey.DESCRIPTION_UPWARD_SHARED
      : FinishReviewModalDescriptionKey.DESCRIPTION_UPWARD_NOT_SHARED;
  }

  // Fallback (should not happen, but TypeScript requires a return)
  return FinishReviewModalDescriptionKey.DESCRIPTION_REST_SHARED;
};
