import {
  LoginFormData,
  LoginResponseData,
  SignInResponseData
} from '@/types/api/system/login';
import request from '@/utils/request';
import { AxiosPromise } from 'axios';

/**
 * 第一步：校验用户名密码+图形验证码
 * 返回 tempToken（Redis 存储,5分钟有效）+ googleState
 */
export function login(data: LoginFormData): AxiosPromise<LoginResponseData> {
  return request({
    url: 'admin/system/employee/check',
    method: 'post',
    params: data
  });
}

/**
 * 未绑定谷歌验证时：凭 tempToken 完成登录，返回 authToken
 */
export function signIn(tempToken: string): AxiosPromise<SignInResponseData> {
  return request({
    url: 'admin/system/employee/sign/in',
    method: 'post',
    params: { tempToken },
  });
}

/**
 * 获取谷歌验证二维码和密钥（用于首次绑定展示）
 */
export function getGoogleInfo(): AxiosPromise<{ secret: string; qrUrl: string }> {
  return request({
    url: 'admin/system/employee/google/info',
    method: 'get',
  });
}

/**
 * 绑定谷歌验证（登录后首次）
 */
export function bindGoogle(googleCode: string): AxiosPromise<any> {
  return request({
    url: 'admin/system/employee/google/bind',
    method: 'post',
    params: { googleCode },
  });
}

/**
 * 已绑定谷歌验证时：凭 tempToken + 谷歌验证码登录，返回 authToken
 */
export function signInGoogle(tempToken: string, googleCode: string): AxiosPromise<SignInResponseData> {
  return request({
    url: 'admin/system/employee/sign/in/google',
    method: 'post',
    params: { tempToken, googleCode },
  });
}
