import dayjs from 'dayjs';
import { getStatusTagType } from '@/constants/status-tag';

export interface PerformanceCreateDraft {
  periodMonth: string;
  employeeId: string;
  employeeName: string;
}

export const PerformancePermission = {
  TemplateQuery: 'project:performance-template:query',
  TemplateUpdate: 'project:performance-template:update',
  SheetQuery: 'project:performance-sheet:query',
  SheetCreate: 'project:performance-sheet:create',
  SheetUpdate: 'project:performance-sheet:update',
  SheetDelete: 'project:performance-sheet:delete',
  SheetConfirm: 'project:performance-sheet:confirm',
  SheetReject: 'project:performance-sheet:reject',
  SheetExport: 'project:performance-sheet:export',
  TeamDashboard: 'project:performance-sheet:team-dashboard'
} as const;

export const performanceStatusOptions: Array<{
  label: string;
  value: Api.Performance.Common.SheetStatusCode;
}> = [
  { label: '待发送', value: 'draft' },
  { label: '待确认', value: 'sent' },
  { label: '已确认', value: 'confirmed' },
  { label: '已退回', value: 'rejected' }
];

export function getPerformanceStatusOptions(
  statusList: Api.Performance.Sheet.StatusDict[] = []
): Array<{ label: string; value: string }> {
  if (!statusList.length) {
    return performanceStatusOptions.map(item => ({ ...item, value: String(item.value) }));
  }

  return statusList.map(item => ({
    label: item.statusName,
    value: String(item.statusCode)
  }));
}

export const performanceActionNameMap: Record<string, string> = {
  send: '发送',
  resend: '重新发送',
  confirm: '确认',
  reject: '退回',
  delete: '删除'
};

export function getPerformanceStatusLabel(statusCode?: string | null, statusName?: string | null) {
  return statusName || performanceStatusOptions.find(item => item.value === statusCode)?.label || statusCode || '--';
}

export function resolvePerformanceStatusTagType(statusCode?: string | null) {
  return getStatusTagType('performanceSheet', statusCode);
}

export function getPerformanceActionLabel(actionCode?: string | null) {
  if (!actionCode) return '--';

  return performanceActionNameMap[actionCode] || actionCode;
}

export function formatDateTime(value?: string | null) {
  if (!value) return '--';

  const target = dayjs(value);

  return target.isValid() ? target.format('YYYY-MM-DD HH:mm') : value;
}

export function formatDate(value?: string | null) {
  if (!value) return '--';

  const target = dayjs(value);

  return target.isValid() ? target.format('YYYY-MM-DD') : value;
}

export function formatScore(value?: string | number | null) {
  if (value === null || value === undefined || value === '') return '--';

  const numberValue = Number(value);

  return Number.isFinite(numberValue) ? numberValue.toFixed(2) : String(value);
}

export function normalizeScoreText(value: unknown) {
  const text = String(value ?? '').trim();
  if (!text) return '';

  const normalized = text.replace(/,/g, '');
  const numberValue = Number(normalized);

  return Number.isFinite(numberValue) ? numberValue.toFixed(2) : text;
}

export function downloadBlob(blob: Blob, fileName: string) {
  const url = URL.createObjectURL(blob);
  const link = document.createElement('a');

  link.href = url;
  link.download = fileName;
  link.click();

  URL.revokeObjectURL(url);
}

export function getSheetExportName(row: Api.Performance.Sheet.Sheet) {
  return row.fileName || `${row.periodMonth}月-绩效表_${row.employeeName}.xlsx`;
}

export function createDefaultPeriodMonth() {
  return dayjs().format('YYYY-MM');
}

export function getDeptOrgTypeLabel(value?: string | null) {
  const map: Record<string, string> = {
    direction: '方向',
    function: '职能',
    dept: '部门',
    team: '团队',
    company: '公司'
  };

  return map[value || ''] || value || '--';
}
