/**
 * User entity matching database schema
 */
export interface User {
  id: string;
  authentik_sub: string;
  email: string;
  name: string | null;
  preferred_username: string | null;
  given_name: string | null;
  family_name: string | null;
  profile_picture_url: string | null;

  refresh_token: string | null;
  refresh_token_expires_at: Date | null;

  last_login_at: Date | null;
  created_at: Date;
  updated_at: Date;

  preferences: Record<string, any>;
  is_active: boolean;
}

/**
 * DTO for creating a new user
 */
export interface CreateUserDTO {
  authentik_sub: string;
  email: string;
  name?: string;
  preferred_username?: string;
  given_name?: string;
  family_name?: string;
  profile_picture_url?: string;
  preferences?: Record<string, any>;
}

/**
 * DTO for updating an existing user
 */
export interface UpdateUserDTO {
  name?: string;
  preferred_username?: string;
  given_name?: string;
  family_name?: string;
  profile_picture_url?: string;
  refresh_token?: string | null;
  refresh_token_expires_at?: Date | null;
  last_login_at?: Date;
  preferences?: Record<string, any>;
  is_active?: boolean;
}

/**
 * User response without sensitive fields
 * Used for API responses
 */
export interface UserResponse {
  id: string;
  email: string;
  name: string | null;
  preferred_username: string | null;
  given_name: string | null;
  family_name: string | null;
  profile_picture_url: string | null;
  last_login_at: Date | null;
  created_at: Date;
  updated_at: Date;
  preferences: Record<string, any>;
  is_active: boolean;
}

/**
 * Convert User to UserResponse by removing sensitive fields
 */
export function toUserResponse(user: User): UserResponse {
  return {
    id: user.id,
    email: user.email,
    name: user.name,
    preferred_username: user.preferred_username,
    given_name: user.given_name,
    family_name: user.family_name,
    profile_picture_url: user.profile_picture_url,
    last_login_at: user.last_login_at,
    created_at: user.created_at,
    updated_at: user.updated_at,
    preferences: user.preferences,
    is_active: user.is_active,
  };
}
