'use client';

import type {
  ExchangeCodeResponse,
  MockLoginRequest,
  MockLoginResponse,
  RefreshTokenResponse,
} from '@pac/types';
import { api } from './api-client';

export async function exchangeCode(code: string): Promise<ExchangeCodeResponse> {
  return api.post<ExchangeCodeResponse>(
    '/pac/v1/auth/exchange-code',
    { code },
    { auth: false },
  );
}

export async function refreshToken(token: string): Promise<RefreshTokenResponse> {
  return api.post<RefreshTokenResponse>(
    '/pac/v1/auth/refresh',
    { refreshToken: token },
    { auth: false },
  );
}

/// Mock login(开发 / 试部署用)— 按 { tenant, role } 预制 user 直产 JWT
/// 生产 host SSO 接入后,后端 env PAC_ENABLE_MOCK_LOGIN=false 关闭,此调用会返 10107
export async function mockLogin(req: MockLoginRequest): Promise<MockLoginResponse> {
  return api.post<MockLoginResponse>('/pac/v1/auth/mock-login', req, { auth: false });
}
