import { computed } from 'vue';
import { resolveAdminMessage } from '../i18n/resolve-message';
import type { AdminLocale } from '../i18n/admin-messages';
import { getAdminLocale } from '../i18n';
import { useAdminLocale } from '../composables/useAdminLocale';

export type BetTagType = '' | 'info' | 'success' | 'warning' | 'danger';

const STATUS_TAG: Record<string, BetTagType> = {
  PENDING: 'warning',
  WON: 'success',
  LOST: 'danger',
  VOID: 'info',
  REFUNDED: 'info',
};

function translateKey(key: string, locale?: AdminLocale): string {
  return resolveAdminMessage(key, locale) ?? key;
}

export function betStatusLabel(status: string, locale?: AdminLocale) {
  const key = `bet.status.${status}`;
  const v = translateKey(key, locale);
  return v !== key ? v : status;
}

export function betStatusTagType(status: string): BetTagType {
  return STATUS_TAG[status] ?? 'info';
}

export function betTypeLabel(betType: string, locale?: AdminLocale) {
  const key = `bet.type.${betType}`;
  const v = translateKey(key, locale);
  return v !== key ? v : betType;
}

export function betSettlementLabel(v: string | null | undefined, locale?: AdminLocale) {
  if (!v) return '—';
  const key = `bet.settlement.${v}`;
  const label = translateKey(key, locale);
  return label !== key ? label : v;
}

export function betResultLabel(s: string | null | undefined, locale?: AdminLocale) {
  if (!s) return '—';
  const key = `bet.result.${s}`;
  const label = translateKey(key, locale);
  return label !== key ? label : s;
}

export function useBetFilterOptions() {
  const { t } = useAdminLocale();
  const statusOptions = computed(() => [
    { value: '', label: t('common.all') },
    { value: 'PENDING', label: t('bet.status.PENDING') },
    { value: 'WON', label: t('bet.status.WON') },
    { value: 'LOST', label: t('bet.status.LOST') },
    { value: 'VOID', label: t('bet.status.VOID') },
    { value: 'REFUNDED', label: t('bet.status.REFUNDED') },
  ]);
  const typeOptions = computed(() => [
    { value: '', label: t('common.all') },
    { value: 'SINGLE', label: t('bet.type.SINGLE') },
    { value: 'PARLAY', label: t('bet.type.PARLAY') },
  ]);
  return { statusOptions, typeOptions };
}

/** @deprecated 使用 useBetFilterOptions() */
export const BET_STATUS_OPTIONS = [
  { value: '', label: '全部' },
  { value: 'PENDING', label: '待结算' },
  { value: 'WON', label: '已赢' },
  { value: 'LOST', label: '已输' },
  { value: 'VOID', label: '作废' },
  { value: 'REFUNDED', label: '已退款' },
];

/** @deprecated 使用 useBetFilterOptions() */
export const BET_TYPE_OPTIONS = [
  { value: '', label: '全部' },
  { value: 'SINGLE', label: '单关' },
  { value: 'PARLAY', label: '串关' },
];
