import {postJson, getJson} from '@/shared/services/httpClient';
import type {
  AuthSession,
  LoginCredentials,
  LoginError,
} from '@/features/auth/types/auth';

/**
 * TRANSPORT ONLY.
 *
 * This is the layer that knows URLs, verbs and status codes, and it is the
 * only one. Swapping the mock route handlers for `https://api.loyaly.ai` means
 * editing the three strings below — the service above it, the hook above that
 * and the form above that never learn where the data came from.
 *
 * It deliberately does NOT interpret results: mapping an HTTP failure onto a
 * field-addressed form error is a domain decision and belongs in the service.
 */

export interface AuthTransportResult<T> {
  ok: boolean;
  status: number;
  data?: T;
  message?: string;
  /** Present when the server attributed a failure to one input. */
  field?: LoginError['field'];
}

export const authRepository = {
  login(credentials: LoginCredentials) {
    return postJson<AuthSession>('/api/auth/login', credentials);
  },

  logout() {
    return postJson<{ok: boolean}>('/api/auth/logout', {});
  },

  /** Null data means "no session" — a normal answer, not a failure. */
  currentSession() {
    return getJson<AuthSession | null>('/api/auth/session');
  },
};
