import { Document, Types } from 'mongoose';

export enum QRCodeStatus {
  ACTIVE = 'ACTIVE',
  INACTIVE = 'INACTIVE',
  MISSING = 'MISSING',
}

export enum QRCodeAccessType {
  PUBLIC = 'PUBLIC',
  PRIVATE = 'PRIVATE',
}

export enum QRCodeType {
  MEMORY_TAG = 'MEMORY_TAG',
  MEDICAL_QR = 'MEDICAL_QR',
  CAR = 'CAR',
  LUGGAGE = 'LUGGAGE',
  PET = 'PET',
  IDENTIFICATIONS = 'IDENTIFICATIONS',
  DOCUMENT = 'DOCUMENT',
  LINK = 'LINK',
  WIFI = 'WIFI',
}

export enum PrivacyAccessLevel {
  PUBLIC = 'PUBLIC',
  RESTRICTED = 'RESTRICTED',
}

export interface IPrivacyControls {
  access_level: PrivacyAccessLevel;
  access_password?: string;
  is_disabled: boolean;
  allow_anonymous_messaging: boolean;
  emergency_notify: {
    enabled: boolean;
    contact_email?: string;
    contact_phone?: string;
  };
}

export interface ILocation extends Document {
  _id: Types.ObjectId;
  latitude: number;
  longitude: number;
  City?: string;
  address?: string;
  country?: string;
  state?: string;
  postalCode?: string;
  street?: string;
  createdAt: Date;
  updatedAt: Date;
}

export interface IMemoryTag {
  location?: Types.ObjectId | ILocation;
  location_enabled: boolean;
  images: Types.ObjectId[] | IMedia[];
}

export interface IQRCode extends Document {
  _id: Types.ObjectId;
  title: string;
  type: QRCodeType;
  // Standardized fields for all QR code types
  item_name?: string;
  item_description?: string;
  images?: Types.ObjectId[] | IMedia[];
  // Location support for all QR code types
  location?: Types.ObjectId | ILocation;
  location_enabled?: boolean;
  // Legacy fields (kept for backward compatibility)
  name?: string;
  description?: string;
  lastKnownLocation?: string;
  notes?: string;
  memory_tag?: IMemoryTag;
  type_details?: any; // Type-specific data stored as JSON
  expired_at?: Date;
  status: QRCodeStatus;
  created_by: Types.ObjectId;
  access_type: QRCodeAccessType;
  pin?: string;
  color?: string;
  background_color?: string;
  size?: number;
  privacy_controls?: IPrivacyControls;
  created_at: Date;
  updated_at: Date;
}

export interface IMedia extends Document {
  _id: Types.ObjectId;
  originalName?: string;
  filePath?: string;
  mimeType?: string;
  size?: number;
  createdAt: Date;
  updatedAt: Date;
}

export interface IQRCodeUpload extends Document {
  _id: Types.ObjectId;
  media_id: Types.ObjectId;
  qrcode_id: Types.ObjectId;
  created_at: Date;
  updated_at: Date;
}

export interface CreateMemoryTagRequest {
  location_enabled?: boolean;
  location?: {
    latitude: number;
    longitude: number;
  };
  images?: Array<{
    originalName: string;
    filePath: string;
    mimeType: string;
    size: number;
  }>;
}

export interface CreateQRCodeRequest {
  title: string;
  type?: QRCodeType;
  // Standardized fields for all QR code types
  item_name?: string;
  item_description?: string;
  images?: Array<{
    originalName: string;
    filePath: string;
    mimeType: string;
    size: number;
  }>;
  // Location support for all QR code types
  location?: {
    latitude: number;
    longitude: number;
  };
  location_enabled?: boolean;
  // Legacy fields (kept for backward compatibility)
  name?: string;
  description?: string;
  lastKnownLocation?: string;
  notes?: string;
  memory_tag?: CreateMemoryTagRequest;
  type_details?: any; // Type-specific data
  expired_at?: Date;
  access_type?: QRCodeAccessType;
  pin?: string;
  color?: string;
  background_color?: string;
  size?: number;
  privacy_controls?: IPrivacyControls;
}

export interface UpdateMemoryTagRequest {
  location_enabled?: boolean;
  location?: {
    latitude: number;
    longitude: number;
  };
  images?: Array<{
    originalName: string;
    filePath: string;
    mimeType: string;
    size: number;
  }>;
}

export interface UpdateQRCodeRequest {
  title?: string;
  type?: QRCodeType;
  // Standardized fields for all QR code types
  item_name?: string;
  item_description?: string;
  images?: Array<{
    originalName: string;
    filePath: string;
    mimeType: string;
    size: number;
  }>;
  // Location support for all QR code types
  location?: {
    latitude: number;
    longitude: number;
  };
  location_enabled?: boolean;
  // Legacy fields (kept for backward compatibility)
  name?: string;
  description?: string;
  lastKnownLocation?: string;
  notes?: string;
  memory_tag?: UpdateMemoryTagRequest;
  type_details?: any; // Type-specific data
  expired_at?: Date;
  status?: QRCodeStatus;
  access_type?: QRCodeAccessType;
  pin?: string;
  color?: string;
  background_color?: string;
  size?: number;
  privacy_controls?: IPrivacyControls;
}

export interface PopulatedUser {
  _id: string;
  first_name?: string;
  last_name?: string;
  email?: string;
  profile_picture?: string;
  profile_picture_url?: string | null;
}

export interface MemoryTagResponse {
  location?: {
    _id: string;
    latitude: number;
    longitude: number;
    City?: string;
    address?: string;
    country?: string;
    state?: string;
    postalCode?: string;
    street?: string;
    createdAt: Date;
    updatedAt: Date;
  };
  location_enabled: boolean;
  images: {
    _id: string;
    originalName?: string;
    filePath?: string;
    mimeType?: string;
    size?: number;
    url?: string;
  }[];
}

export interface QRCodeResponse {
  _id: string;
  title: string;
  type: QRCodeType;
  // Standardized fields for all QR code types
  item_name?: string;
  item_description?: string;
  images?: {
    _id: string;
    originalName?: string;
    filePath?: string;
    mimeType?: string;
    size?: number;
    url?: string;
  }[];
  // Location support for all QR code types
  location?: {
    _id: string;
    latitude: number;
    longitude: number;
    City?: string;
    address?: string;
    country?: string;
    state?: string;
    postalCode?: string;
    street?: string;
    createdAt: Date;
    updatedAt: Date;
  };
  location_enabled?: boolean;
  // Legacy fields (kept for backward compatibility)
  name?: string;
  description?: string;
  lastKnownLocation?: string;
  notes?: string;
  memory_tag?: MemoryTagResponse;
  type_details?: any; // Type-specific data
  expired_at?: Date;
  status: QRCodeStatus;
  created_by: string | PopulatedUser;
  access_type: QRCodeAccessType;
  pin?: string;
  color?: string;
  background_color?: string;
  size?: number;
  privacy_controls?: IPrivacyControls;
  qr_code_url?: string;
  password_protected?: boolean; // Indicates if medical QR code with PRIVATE access requires password
  created_at: Date;
  updated_at: Date;
}

export interface QRCodeListResponse {
  qrcodes: QRCodeResponse[];
  total: number;
  page: number;
  limit: number;
  totalPages: number;
}
