import { ref, onUnmounted } from 'vue';
import api from '../api';

const COOLDOWN_SECONDS = 60;

export function useSmsCode() {
  const sessionId = ref<string | null>(null);
  const countdown = ref(0);
  const sending = ref(false);
  const error = ref<string | null>(null);
  let timer: ReturnType<typeof setInterval> | null = null;

  function clearTimer() {
    if (timer) {
      clearInterval(timer);
      timer = null;
    }
  }

  function startCountdown() {
    clearTimer();
    countdown.value = COOLDOWN_SECONDS;
    timer = setInterval(() => {
      if (countdown.value <= 1) {
        clearTimer();
        countdown.value = 0;
      } else {
        countdown.value -= 1;
      }
    }, 1000);
  }

  async function send(
    phone: string,
    countryCode: string,
    purpose: 'register' | 'reset_password' = 'register',
  ) {
    if (countdown.value > 0 || sending.value) return;
    const trimmed = phone.trim();
    const dial = countryCode.replace(/\D/g, '');
    if (!trimmed || !dial) {
      error.value = 'phone_required';
      return;
    }
    sending.value = true;
    error.value = null;
    try {
      const locale = localStorage.getItem('locale') || 'zh-CN';
      const { data } = await api.post('/player/sms/send', {
        phone: trimmed,
        countryCode: dial,
        locale,
        purpose,
      });
      sessionId.value = data.data.sessionId;
      startCountdown();
    } catch (e: unknown) {
      const msg = (e as { response?: { data?: { error?: string } } })?.response?.data?.error;
      error.value = msg || 'send_failed';
    } finally {
      sending.value = false;
    }
  }

  onUnmounted(clearTimer);

  return { sessionId, countdown, sending, error, send };
}
