/**
 * TypeScript interfaces for AccountViewView component
 * Provides type safety for settings, profile data, and component state
 */

import { EndorserRateLimits, ImageRateLimits } from "./index";
import { LeafletMouseEvent } from "leaflet";

/**
 * BoundingBox type describes the geographical bounding box coordinates.
 */
export type BoundingBox = {
  eastLong: number; // Eastern longitude
  maxLat: number; // Maximum (Northernmost) latitude
  minLat: number; // Minimum (Southernmost) latitude
  westLong: number; // Western longitude
};

/**
 * Interface for account settings retrieved from database
 */
export interface AccountSettings {
  activeDid?: string;
  apiServer?: string;
  firstName?: string;
  lastName?: string;
  hideRegisterPromptOnNewContact?: boolean;
  isRegistered?: boolean;
  searchBoxes?: Array<{
    name: string;
    bbox: BoundingBox;
  }>;
  notifyingNewActivityTime?: string;
  notifyingReminderMessage?: string;
  notifyingReminderTime?: string;
  partnerApiServer?: string;
  profileImageUrl?: string;
  showContactGivesInline?: boolean;
  passkeyExpirationMinutes?: number;
  showGeneralAdvanced?: boolean;
  showShortcutBvc?: boolean;
  warnIfProdServer?: boolean;
  warnIfTestServer?: boolean;
  webPushServer?: string;
}

/**
 * Interface for user profile data from API
 */
export interface UserProfileData {
  description?: string;
  locLat?: number;
  locLon?: number;
}

/**
 * Interface for API response containing user profile
 */
export interface UserProfileResponse {
  data: UserProfileData;
}

/**
 * Interface for component state related to profile management
 */
export interface ProfileState {
  userProfileDesc: string;
  userProfileLatitude: number;
  userProfileLongitude: number;
  includeUserProfileLocation: boolean;
  savingProfile: boolean;
  profileImageUrl?: string;
}

/**
 * Interface for component state related to notifications
 */
export interface NotificationState {
  notifyingNewActivity: boolean;
  notifyingNewActivityTime: string;
  notifyingReminder: boolean;
  notifyingReminderMessage: string;
  notifyingReminderTime: string;
  subscription: PushSubscription | null;
}

/**
 * Interface for component state related to settings
 */
export interface SettingsState {
  activeDid: string;
  apiServer: string;
  apiServerInput: string;
  partnerApiServer: string;
  partnerApiServerInput: string;
  webPushServer: string;
  webPushServerInput: string;
  passkeyExpirationMinutes: number;
  previousPasskeyExpirationMinutes: number;
  passkeyExpirationDescription: string;
  hideRegisterPromptOnNewContact: boolean;
  isRegistered: boolean;
  isSearchAreasSet: boolean;
  showContactGives: boolean;
  showGeneralAdvanced: boolean;
  showShortcutBvc: boolean;
  warnIfProdServer: boolean;
  warnIfTestServer: boolean;
}

/**
 * Interface for component state related to UI display
 */
export interface UIState {
  loadingProfile: boolean;
  loadingLimits: boolean;
  showAdvanced: boolean;
  showB64Copy: boolean;
  showDidCopy: boolean;
  showDerCopy: boolean;
  showPubCopy: boolean;
  showLargeIdenticonId?: string;
  showLargeIdenticonUrl?: string;
  downloadUrl: string;
  zoom: number;
}

/**
 * Interface for component state related to limits and validation
 */
export interface LimitsState {
  endorserLimits: EndorserRateLimits | null;
  imageLimits: ImageRateLimits | null;
  limitsMessage: string;
  publicHex: string;
  publicBase64: string;
  derivationPath: string;
}

/**
 * Interface for component state related to identity
 */
export interface IdentityState {
  givenName: string;
}

/**
 * Complete interface for AccountViewView component state
 */
export interface AccountViewState
  extends ProfileState,
    NotificationState,
    SettingsState,
    UIState,
    LimitsState,
    IdentityState {}

/**
 * Interface for clipboard copy operations
 */
export interface ClipboardOperation {
  text: string;
  callback: () => void;
}

/**
 * Interface for notification permission callback
 */
export interface NotificationPermissionCallback {
  success: boolean;
  timeText: string;
  message?: string;
}

/**
 * Interface for import/export operations
 */
export interface ImportExportState {
  inputImportFileNameRef?: Blob;
}

/**
 * Type for API error responses
 */
export interface ApiErrorResponse {
  response?: {
    data?: {
      error?: { message?: string } | string;
    };
    status?: number;
  };
}

/**
 * Type guard for API errors
 */
export function isApiError(error: unknown): error is ApiErrorResponse {
  return typeof error === "object" && error !== null && "response" in error;
}

/**
 * Type guard for standard errors
 */
export function isError(error: unknown): error is Error {
  return error instanceof Error;
}

/**
 * Interface for file import content structure
 */
export interface ImportContent {
  data?: {
    data?: Array<{
      tableName: string;
      rows: Array<unknown>;
    }>;
  };
}

/**
 * Interface for map ready callback
 */
export interface MapReadyCallback {
  (map: L.Map): void;
}

/**
 * Interface for mouse event handlers
 */
export interface MouseEventHandler {
  (event: LeafletMouseEvent): void;
}
