/**
 * Auth resource: login, logout, refresh
 */

import type { AxiosInstance } from 'axios';
import { API_PATHS } from '@effigenix/config';

export interface LoginRequest {
  username: string;
  password: string;
}

export interface LoginResponse {
  accessToken: string;
  tokenType: string;
  expiresIn: number;
  expiresAt: string;
  refreshToken: string;
}

export interface RefreshTokenRequest {
  refreshToken: string;
}

export function createAuthResource(client: AxiosInstance) {
  return {
    async login(request: LoginRequest): Promise<LoginResponse> {
      const response = await client.post<LoginResponse>(API_PATHS.auth.login, request);
      return response.data;
    },

    async logout(): Promise<void> {
      await client.post(API_PATHS.auth.logout);
    },

    async refresh(request: RefreshTokenRequest): Promise<LoginResponse> {
      const response = await client.post<LoginResponse>(API_PATHS.auth.refresh, request);
      return response.data;
    },
  };
}

export type AuthResource = ReturnType<typeof createAuthResource>;
