/**
 * Extended API Types
 *
 * This file contains type extensions for the auto-generated types when the OpenAPI
 * spec doesn't fully capture the actual backend response structure.
 *
 * @module lib/api/types
 */

import type { Token } from './generated/types.gen';

/**
 * Token with User Response
 *
 * Alias for Token type which now includes user and expires_in fields.
 * Kept for backwards compatibility with existing type guards.
 */
export type TokenWithUser = Token;

/**
 * Success Response (for operations that return success messages)
 */
export interface SuccessResponse {
  success: true;
  message: string;
}

/**
 * Type guard to check if response includes user data
 */
export function isTokenWithUser(token: unknown): token is TokenWithUser {
  const obj = token as Record<string, unknown>;
  return (
    typeof token === 'object' &&
    token !== null &&
    'access_token' in token &&
    'user' in token &&
    typeof obj.access_token === 'string' &&
    typeof obj.user === 'object' &&
    obj.user !== null &&
    !Array.isArray(obj.user)
  );
}

/**
 * Type guard to check if response is a success message
 */
export function isSuccessResponse(response: unknown): response is SuccessResponse {
  const obj = response as Record<string, unknown>;
  return (
    typeof response === 'object' &&
    response !== null &&
    'success' in response &&
    'message' in response &&
    obj.success === true &&
    typeof obj.message === 'string'
  );
}
