import { baseRequestClient, requestClient } from '#/api/request';

export namespace AuthApi {
  /** 登录接口参数 */
  export interface LoginParams {
    account: string;
    password?: string;
    code?: string;
    admin_id?: number;
    /** @deprecated 兼容旧客户端 */
    login_method?: 'phone' | 'login_account' | 'job_number';
    username?: string;
    phone?: string;
    login_account?: string;
    job_number?: string;
  }

  /** 登录接口返回值 */
  export interface AccountOption {
    account_type?: string;
    id: number;
    nick_name: string;
    role_name: string;
    store_name: string;
    login_account?: string;
    is_current?: boolean;
    password_matched?: boolean;
  }

  export interface LoginResult {
    token?: string;
    need_select?: boolean;
    accounts?: AccountOption[];
    default_account_id?: number;
    default_account_type?: string;
  }

  export interface RefreshTokenResult {
    data: string;
    status: number;
  }

  /** 发送验证码返回结果 */
  export interface SendCodeResult {
    code?: string;
    need_select?: boolean;
    accounts?: AccountOption[];
    default_account_id?: number;
    default_account_type?: string;
  }
}

/**
 * 登录
 */
export async function loginApi(data: AuthApi.LoginParams) {
  if (data.account) {
    return requestClient.post<AuthApi.LoginResult>('login', {
      account: data.account,
      password: data.password,
      code: data.code,
      admin_id: data.admin_id,
    });
  }

  const params: Record<string, unknown> = {
    login_method: data.login_method || 'phone',
    password: data.password,
    code: data.code,
  };

  if (data.login_method === 'phone') {
    params.phone = data.phone || data.username;
  } else if (data.login_method === 'login_account') {
    params.login_account = data.login_account;
  } else if (data.login_method === 'job_number') {
    params.job_number = data.job_number;
  }

  return requestClient.post<AuthApi.LoginResult>('login', params);
}

/**
 * 发送验证码
 */
export async function sendVerificationCode(params: {
  account?: string;
  password?: string;
  admin_id?: number;
  login_method?: 'phone' | 'login_account' | 'job_number';
  username?: string;
  phone?: string;
  login_account?: string;
  job_number?: string;
}) {
  if (params.account) {
    return requestClient.post<AuthApi.SendCodeResult>('send-verification-code', {
      account: params.account,
      password: params.password,
      admin_id: params.admin_id,
    });
  }

  const requestParams: Record<string, unknown> = {
    login_method: params.login_method || 'phone',
  };

  if (params.login_method === 'phone') {
    requestParams.phone = params.phone || params.username;
  } else if (params.login_method === 'login_account') {
    requestParams.login_account = params.login_account;
  } else if (params.login_method === 'job_number') {
    requestParams.job_number = params.job_number;
  }

  return requestClient.post<AuthApi.SendCodeResult>(
    'send-verification-code',
    requestParams,
  );
}

/**
 * 刷新accessToken
 */
export async function refreshTokenApi() {
  return baseRequestClient.post<AuthApi.RefreshTokenResult>('/auth/refresh', {
    withCredentials: true,
  });
}

/**
 * 退出登录
 */
export async function logoutApi() {
  return baseRequestClient.post('logout', {
    withCredentials: true,
  });
}

/**
 * 获取用户权限码
 */
export async function getAccessCodesApi() {
  return requestClient.get<string[]>('auth/codes');
}

/**
 * 同手机号可切换的管理员账号
 */
export async function getSiblingAccountsApi() {
  return requestClient.get<AuthApi.AccountOption[]>('auth/sibling-accounts');
}

/**
 * 切换管理员账号
 */
export async function switchAccountApi(data: {
  admin_id: number;
}) {
  return requestClient.post<AuthApi.LoginResult>('auth/switch-account', data);
}
