/**
 * Standardized API Response Types for FitAI Admin API
 *
 * These types ensure consistent response structures across all API endpoints.
 */

/**
 * Standard success response wrapper
 */
export interface ApiSuccessResponse<T = unknown> {
  success: true;
  data: T;
  meta?: ResponseMeta;
}

/**
 * Standard error response wrapper
 */
export interface ApiErrorResponse {
  success: false;
  error: {
    message: string;
    code?: string;
    details?: Record<string, string[]>;
  };
  meta?: ResponseMeta;
}

/**
 * Union type for all API responses
 */
export type ApiResponse<T = unknown> = ApiSuccessResponse<T> | ApiErrorResponse;

/**
 * Pagination metadata for list endpoints
 */
export interface PaginationMeta {
  page: number;
  pageSize: number;
  totalItems: number;
  totalPages: number;
  hasNextPage: boolean;
  hasPreviousPage: boolean;
}

/**
 * Response metadata included in all responses
 */
export interface ResponseMeta {
  timestamp: string;
  requestId?: string;
  pagination?: PaginationMeta;
}

/**
 * Paginated list response
 */
export interface PaginatedResponse<T> {
  success: true;
  data: T[];
  meta: ResponseMeta & {
    pagination: PaginationMeta;
  };
}

/**
 * Common API entity types
 */

export interface UserResponse {
  id: string;
  email: string;
  firstName: string;
  lastName: string;
  role: string;
  phone?: string | null;
  gymId?: string | null;
  gymName?: string | null;
  createdAt: string;
  updatedAt: string;
  // Client-specific fields
  client?: ClientResponse | null;
  // Attendance fields
  isCheckedIn?: boolean;
  checkInTime?: string | null;
  lastCheckInTime?: string | null;
  checkInsThisWeek?: number;
  checkInsThisMonth?: number;
}

export interface ClientResponse {
  id: string;
  userId: string;
  membershipType: string;
  membershipStatus: string;
  joinDate: string;
}

export interface FitnessGoalResponse {
  id: string;
  userId: string;
  fitnessProfileId?: string | null;
  goalType: string;
  title: string;
  description: string;
  targetValue: number;
  currentValue: number;
  unit: string;
  startDate: string;
  targetDate?: string | null;
  completedDate?: string | null;
  status: string;
  progress: number;
  priority: string;
  notes?: string | null;
  createdAt: string;
  updatedAt: string;
}

export interface FitnessProfileResponse {
  id: string;
  userId: string;
  age: number;
  gender: string;
  height: number;
  weight: number;
  activityLevel: string;
  goals: string;
  medicalConditions?: string | null;
  injuries?: string | null;
  createdAt: string;
  updatedAt: string;
}

export interface RecommendationResponse {
  id: string;
  userId: string;
  fitnessProfileId?: string | null;
  type: string;
  recommendationText: string;
  activityPlan?: string | null;
  dietPlan?: string | null;
  status: string;
  createdAt: string;
  updatedAt: string;
}

export interface AttendanceResponse {
  id: string;
  userId: string;
  gymId?: string | null;
  checkInTime: string;
  checkOutTime?: string | null;
  duration?: number | null;
  createdAt: string;
  updatedAt: string;
}

export interface GymResponse {
  id: string;
  name: string;
  address?: string | null;
  city?: string | null;
  state?: string | null;
  zipCode?: string | null;
  phone?: string | null;
  email?: string | null;
  createdAt: string;
  updatedAt: string;
}

/**
 * Common error codes
 */
export enum ApiErrorCode {
  // Authentication & Authorization
  UNAUTHORIZED = "UNAUTHORIZED",
  FORBIDDEN = "FORBIDDEN",
  INVALID_TOKEN = "INVALID_TOKEN",

  // Validation
  VALIDATION_ERROR = "VALIDATION_ERROR",
  INVALID_INPUT = "INVALID_INPUT",
  MISSING_REQUIRED_FIELD = "MISSING_REQUIRED_FIELD",

  // Resource errors
  NOT_FOUND = "NOT_FOUND",
  ALREADY_EXISTS = "ALREADY_EXISTS",
  CONFLICT = "CONFLICT",

  // Server errors
  INTERNAL_ERROR = "INTERNAL_ERROR",
  DATABASE_ERROR = "DATABASE_ERROR",
  EXTERNAL_SERVICE_ERROR = "EXTERNAL_SERVICE_ERROR",

  // Business logic errors
  INSUFFICIENT_PERMISSIONS = "INSUFFICIENT_PERMISSIONS",
  INVALID_OPERATION = "INVALID_OPERATION",
  RESOURCE_LOCKED = "RESOURCE_LOCKED",
}
