import mongoose from 'mongoose';

/**
 * TypeScript interfaces generated from Mongoose schemas
 * These interfaces represent the structure of documents in the database
 */

// ============ USER INTERFACE ============
export interface IUser extends mongoose.Document {
    _id: mongoose.Types.ObjectId;
    first_name?: string;
    last_name?: string;
    email?: string;
    password?: string;
    phone_number?: string;
    isProfileComplete: boolean;
    country?: string;
    State?: string;
    bio?: string;
    City?: string;
    role: 'USER' | 'ADMIN' | 'SCANNER';
    profile_picture?: string;
    media_id?: mongoose.Types.ObjectId;
    notificationForQrdCode: boolean;
    smsAlert: boolean;
    emergencyScanNotification: boolean;
    pushNotificationsEnabled: boolean;
    profile?: string;
    fcmToken?: string;
    deviceToken?: string;
    fcmTokens?: Array<{
        token: string;
        deviceId?: string;
        platform?: 'android' | 'ios' | 'web';
        createdAt: Date;
    }>;
    stripe_customer_id?: string;
    subscription_tier: 'free' | 'personal' | 'family' | 'professional';
    is_delete: boolean;
    status: 'ACTIVE' | 'INACTIVE' | 'SUSPENDED' | 'PENDING';
    familyId?: mongoose.Types.ObjectId;
    familyRole?: 'OWNER' | 'MEMBER';
    isLogin: boolean;
    createdAt: Date;
    updatedAt: Date;
}

// ============ ADMIN INTERFACE ============
export interface IAdmin extends mongoose.Document {
    _id: mongoose.Types.ObjectId;
    full_name?: string | null;
    email: string;
    password: string;
    two_factor_enabled: boolean;
    two_factor_secret?: string | null;
    last_login?: Date | null;
    createdAt: Date;
    updatedAt: Date;
}

// ============ CONTACT INTERFACE ============
export interface IContact extends mongoose.Document {
    _id: mongoose.Types.ObjectId;
    name: string;
    email: string;
    message: string;
    contact_status: 'pending' | 'resolved' | 'closed';
    contact_priority: 'high' | 'medium' | 'low';
    scan_id?: mongoose.Types.ObjectId;
    qrcode_id?: mongoose.Types.ObjectId;
    user_id?: mongoose.Types.ObjectId;
    phone?: string;
    subject: string;
    createdAt: Date;
    updatedAt: Date;
}

export type ICreateContactData = Partial<IContact>;

// ============ QR CODE INTERFACE ============
export interface IQRCode extends mongoose.Document {
    _id: mongoose.Types.ObjectId;
    title: string;
    type: 'MEMORY_TAG' | 'MEDICAL_QR' | 'CAR' | 'LUGGAGE' | 'PET' | 'IDENTIFICATIONS' | 'DOCUMENT';
    item_name?: string;
    item_description?: string;
    images?: mongoose.Types.ObjectId[];
    location?: mongoose.Types.ObjectId;
    location_enabled: boolean;
    name?: string;
    description?: any; // JSON type
    lastKnownLocation?: string;
    notes?: string;
    memory_tag?: any;
    type_details?: any;
    status: 'ACTIVE' | 'INACTIVE' | 'MISSING';
    access_type: 'PUBLIC' | 'PRIVATE';
    pin?: string;
    created_by: mongoose.Types.ObjectId;
    scansCount?: number;
    isDelete: boolean;
    expires_at?: Date;
    created_at: Date;
    updated_at: Date;
}

// ============ SCAN INTERFACE ============
export interface IScan extends mongoose.Document {
    _id: mongoose.Types.ObjectId;
    qrcode_id: mongoose.Types.ObjectId;
    coordinate_id?: mongoose.Types.ObjectId | null;
    scanned_by?: mongoose.Types.ObjectId;
    scan_location?: {
        latitude?: number;
        longitude?: number;
        country?: string | null;
        city?: string | null;
        address?: string | null;
    };
    notes?: string;
    is_anonymous: boolean;
    device_info?: string;
    device_type: 'mobile' | 'tablet' | 'desktop' | 'unknown';
    isPublic: boolean;
    scan_status?: string;
    createdAt: Date;
    updatedAt: Date;
}

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

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

// ============ NOTIFICATION INTERFACE ============
export interface INotification extends mongoose.Document {
    _id: mongoose.Types.ObjectId;
    title: string;
    message: string;
    type: 'MESSAGE' | 'EMERGENCY' | 'QRSCAN' | 'MEMORY' | 'DEFAULT';
    link?: string | null;
    is_read: boolean;
    user_id: mongoose.Types.ObjectId;
    qrcode_id?: mongoose.Types.ObjectId;
    scan_id?: mongoose.Types.ObjectId;
    notification_type?: 'MEMORY_TAG' | 'MEDICAL_QR' | 'CAR' | 'LUGGAGE' | 'PET' | 'IDENTIFICATIONS' | 'DOCUMENT';
    push_sent: boolean;
    push_sent_at?: Date | null;
    push_error?: string | null;
    createdAt: Date;
    updatedAt: Date;
}

// ============ SUBSCRIPTION INTERFACE ============
export interface ISubscription extends mongoose.Document {
    _id: mongoose.Types.ObjectId;
    tier: 'free' | 'personal' | 'family' | 'professional';
    stripe_subscription_id?: string;
    user_id: mongoose.Types.ObjectId;
    stripe_customer_id?: string;
    product_id?: string;
    price_id?: string;
    status: 'incomplete' | 'incomplete_expired' | 'trialing' | 'active' | 'past_due' | 'canceled' | 'unpaid' | 'paused';
    current_period_start: Date;
    current_period_end: Date;
    amount?: number;
    currency?: string;
    interval?: string;
    cancel_at_period_end?: boolean;
    canceled_at?: Date;
    trial_start?: Date;
    trial_end?: Date;
    createdAt: Date;
    updatedAt: Date;
}

// ============ MESSAGE INTERFACE ============
export interface IMessage extends mongoose.Document {
    _id: mongoose.Types.ObjectId;
    conversation_id: mongoose.Types.ObjectId;
    sender_id?: mongoose.Types.ObjectId | null;
    sender_display?: string | null;
    content: string;
    is_flagged: boolean;
    is_read: boolean;
    scan_id?: mongoose.Types.ObjectId;
    notification_id?: mongoose.Types.ObjectId;
    deleted_for?: mongoose.Types.ObjectId[];
    edited: boolean;
    createdAt: Date;
    updatedAt: Date;
}

// ============ CONVERSATION INTERFACE ============
export interface IConversation extends mongoose.Document {
    _id: mongoose.Types.ObjectId;
    participants: mongoose.Types.ObjectId[];
    anonymous: boolean;
    last_message?: mongoose.Types.ObjectId | null;
    scan_id?: mongoose.Types.ObjectId | null;
    createdAt: Date;
    updatedAt: Date;
}

// ============ MEMORY TAG INTERFACE ============
export interface IMemoryTag extends mongoose.Document {
    _id: mongoose.Types.ObjectId;
    itemName: string;
    category: string;
    description: string;
    lastKnownLocation: string;
    ReminderDays: string;
    shareWithCareGiver: boolean;
    notes: boolean;
    userId: mongoose.Types.ObjectId;
    createdAt: Date;
    updatedAt: Date;
}

// ============ AUDIT LOG INTERFACE ============
export type AuditActionType =
    | 'USER_LOGIN' | 'USER_LOGOUT' | 'USER_REGISTER' | 'USER_PROFILE_UPDATE'
    | 'USER_PASSWORD_CHANGE' | 'USER_PASSWORD_RESET' | 'USER_EMAIL_VERIFY'
    | 'USER_OTP_VERIFY' | 'USER_STATUS_CHANGE' | 'USER_DELETE' | 'USER_SUSPEND'
    | 'USER_ACTIVATE' | 'QRCODE_CREATE' | 'QRCODE_UPDATE' | 'QRCODE_DELETE'
    | 'QRCODE_VIEW' | 'QRCODE_STATUS_CHANGE' | 'QRCODE_ACCESS_TYPE_CHANGE'
    | 'QRCODE_PIN_CHANGE' | 'QRCODE_PRIVACY_UPDATE' | 'QRCODE_EXPIRE'
    | 'QRCODE_SCAN' | 'QRCODE_SCAN_ANONYMOUS' | 'QRCODE_SCAN_PRIVATE'
    | 'QRCODE_SCAN_FAILED' | 'QRCODE_PIN_VERIFY' | 'QRCODE_PIN_VERIFY_FAILED'
    | 'SCAN_UPDATE' | 'SCAN_DELETE' | 'MEMORY_TAG_CREATE' | 'MEMORY_TAG_UPDATE'
    | 'MEMORY_TAG_DELETE' | 'MEMORY_TAG_VIEW' | 'MEMORY_TAG_SHARE'
    | 'NOTIFICATION_SEND' | 'NOTIFICATION_READ' | 'NOTIFICATION_DELETE'
    | 'NOTIFICATION_SETTINGS_UPDATE' | 'MEDIA_UPLOAD' | 'MEDIA_DELETE'
    | 'MEDIA_VIEW' | 'MEDIA_DOWNLOAD' | 'ADMIN_LOGIN' | 'ADMIN_USER_VIEW'
    | 'ADMIN_USER_UPDATE' | 'ADMIN_USER_DELETE' | 'ADMIN_USER_STATUS_CHANGE'
    | 'ADMIN_ANALYTICS_VIEW' | 'ADMIN_SYSTEM_CONFIG_UPDATE' | 'ADMIN_BULK_OPERATION'
    | 'SECURITY_LOGIN_ATTEMPT_FAILED' | 'SECURITY_MULTIPLE_LOGIN_ATTEMPTS'
    | 'SECURITY_SUSPICIOUS_ACTIVITY' | 'SECURITY_IP_BLOCKED'
    | 'SECURITY_DATA_BREACH_ATTEMPT' | 'SECURITY_UNAUTHORIZED_ACCESS'
    | 'SYSTEM_STARTUP' | 'SYSTEM_SHUTDOWN' | 'SYSTEM_ERROR' | 'SYSTEM_BACKUP'
    | 'SYSTEM_MAINTENANCE' | 'SYSTEM_UPDATE' | 'DATA_EXPORT_REQUEST'
    | 'DATA_DELETE_REQUEST' | 'GDPR_COMPLIANCE_ACTION' | 'HIPAA_ACCESS_LOG'
    | 'MEDICAL_DATA_ACCESS' | 'MEDICAL_DATA_ENCRYPT' | 'MEDICAL_DATA_DECRYPT'
    | 'API_CALL' | 'API_ERROR' | 'WEBHOOK_TRIGGER' | 'EXTERNAL_SERVICE_CALL';

export type AuditResourceType = 'USER' | 'QRCODE' | 'SCAN' | 'MEMORY_TAG' | 'NOTIFICATION' | 'MEDIA' | 'ADMIN' | 'SYSTEM' | 'API' | 'WEBHOOK';
export type AuditSeverity = 'LOW' | 'MEDIUM' | 'HIGH' | 'CRITICAL';
export type AuditStatus = 'SUCCESS' | 'FAILURE' | 'PARTIAL';

export interface IAuditLog extends mongoose.Document {
    _id: mongoose.Types.ObjectId;
    action: AuditActionType;
    resource_type: AuditResourceType;
    user_id?: mongoose.Types.ObjectId;
    description: string;
    severity: AuditSeverity;
    status: AuditStatus;
    metadata?: any;
    compliance_flags?: {
        hipaa_relevant?: boolean;
    };
    tags?: string[];
    timestamp: Date;
    updated_at: Date;
}

// ============ LOGGING INTERFACE ============
export type LogLevel = 'INFO' | 'WARN' | 'ERROR' | 'DEBUG' | 'CRITICAL';

export interface ILogging extends mongoose.Document {
    _id: mongoose.Types.ObjectId;
    module: string;
    level: LogLevel;
    action: string;
    admin_id?: mongoose.Types.ObjectId;
    user_id?: mongoose.Types.ObjectId;
    metadata?: any;
    ip_address?: string;
    user_agent?: string;
    error_details?: any;
    timestamp: Date;
    updated_at: Date;
}

// ============ DEVICE TOKEN INTERFACE ============
export interface IDeviceToken extends mongoose.Document {
    _id: mongoose.Types.ObjectId;
    userId: mongoose.Types.ObjectId;
    deviceToken: string;
    deviceType: 'ios' | 'android' | 'web';
    isActive: boolean;
    lastUsed: Date;
    createdAt: Date;
    updatedAt: Date;
}

// ============ FAMILY INTERFACE ============
export interface IFamily extends mongoose.Document {
    _id: mongoose.Types.ObjectId;
    familyName: string;
    familyOwner: mongoose.Types.ObjectId;
    members?: mongoose.Types.ObjectId[];
    createdAt: Date;
    updatedAt: Date;
}

// ============ FAMILY MEMBER INTERFACE ============
export interface IFamilyMember extends mongoose.Document {
    _id: mongoose.Types.ObjectId;
    userId: mongoose.Types.ObjectId;
    name: string;
    email: string;
    familyRole: 'OWNER' | 'MEMBER' | 'SPOUSE' | 'CHILD' | 'WIFE' | 'RELATIVES';
    phoneNumber?: string;
    profilePicture?: string;
    createdAt: Date;
    updatedAt: Date;
}

// ============ GUEST USER INTERFACE ============
export interface IGuestUser extends mongoose.Document {
    _id: mongoose.Types.ObjectId;
    guest_token: string;
    name: string;
    device_type?: string;
    ip_address?: string;
    user_agent?: string;
    createdAt: Date;
    updatedAt: Date;
}

// ============ OTP INTERFACE ============
export interface IOTP extends mongoose.Document {
    _id: mongoose.Types.ObjectId;
    otp: string;
    isUsed: boolean;
    type: 'signup' | 'forgot-password' | 'resend-otp';
    userId: mongoose.Types.ObjectId;
    expiresAt: Date;
    createdAt: Date;
    updatedAt: Date;
}

// ============ ADMIN DOCUMENT INTERFACE ============
export interface IAdminDocument extends mongoose.Document {
    _id: mongoose.Types.ObjectId;
    media_id: mongoose.Types.ObjectId;
    downloads: number;
    documentType: 'company-profile' | 'terms-and-conditions' | 'privacy-policy' | 'financial-report' | 'marketing-presentation';
    userId: mongoose.Types.ObjectId;
    createdAt: Date;
    updatedAt: Date;
}

// ============ QR CODE UPLOAD INTERFACE ============
export interface IQRCodeUpload extends mongoose.Document {
    _id: mongoose.Types.ObjectId;
    media_id: mongoose.Types.ObjectId;
    qrcode_id: mongoose.Types.ObjectId;
    created_at: Date;
    updated_at: Date;
}

// All interfaces are already exported above

