import {
    CancelCheckoutProperties,
    CartProperties,
    CheckoutProperties,
    DeliveryProperties,
    ECommerceTypes,
    ItemProperties,
    ItemReportProperties,
    ItemRequestProperties,
} from "../modules/ECommerce/typings";

import {
    LevelProperties,
    LoyaltyTypes,
    MilestoneProperties,
    PromoProperties,
    RewardProperties,
    SurveyProperties,
} from "../modules/Loyalty/typings";

import {ELearningTypes, ExamProperties, ModuleProperties, QuestionProperties,} from "../modules/ELearning/typings";

import {
    BreakProperties,
    CallCenterEventType,
    ContactProperties,
    OpsScorecardProperties,
} from "../modules/CallCenter/typings";

import {AmbulanceProperties, EmergencyMgmtEventType, IncidentProperties,} from "../modules/EmergencyMgmt/typings";

import {
    AppProperties,
    ContentBlock,
    IdentifyProperties,
    MediaProperties,
    ModuleSelectionProperties,
    NavigationTypes,
    PageProperties,
    RateProperties,
    SearchProperties,
    TrackProperties,
} from "../modules/Navigation/typings";

import {PaymentMethodProperties, PaymentsEventTypes} from "../modules/Payments/typings";

import {
    AppointmentProperties,
    EncounterProperties,
    PatientMgmtCatalogType,
    PatientMgmtEventType,
    PatientProperties,
} from "../modules/PatientMgmt/typings";
import {NudgeResponseProperties} from "./repositories/nudges/typings";
import {CatalogItemModel} from "../modules/typings";

export enum GlobalTypes {
    NavigationTypes,
    ECommerceTypes,
}

export interface LogIngestorOptions {
    flushInterval: number;
    flushMaxRetries: number;
    ingestPath: string;
    dimensionPath: string;
    cacheEventsInLocalstorage: boolean;
    cacheEventsKey: string;
    cacheCatalogKey: string;
}

export interface CfLogOptions {
    cdnUrl: string;
    baseUrl: string;
    nudgeFetchPath: string;
    allowAnonymousUsers: boolean;
    catalogPath: string;
    activateNudgeMechanism: boolean;
    selfManagedNudges: boolean;
    defaultBlock: ContentBlock;
    debug: boolean;
    skipDynamicLoading?: boolean;
    isUsingAsInternalWrapper: boolean;
    pauseSDK: boolean;
}

export interface BatteryStatus {
    charging: boolean;
    chargingTime: number;
    dischargingTime: number;
    level: number;
}

export interface EventContext {
    browser?: {
        user_agent: string;
        languages: string[];
        online: boolean;
        battery?: BatteryStatus;
    };
}

export type AnyKnownType =
    | NavigationTypes
    | ECommerceTypes
    | ELearningTypes
    | PaymentsEventTypes
    | LoyaltyTypes
    | CallCenterEventType
    | EmergencyMgmtEventType
    | PatientMgmtEventType
    | PatientMgmtCatalogType;

type WithOlineStatus<T> = T & { __ol?: boolean };

export type EventTeaser =
    | {
        name: NavigationTypes;
        property?: string;
        ctx?: WithOlineStatus<
            ModuleSelectionProperties
            | PageProperties
            | AppProperties
            | IdentifyProperties
            | MediaProperties
            | RateProperties
            | SearchProperties
            | TrackProperties
            | NudgeResponseProperties
        >;
    }
    | {
        name: ECommerceTypes;
        property?: string;
        ctx?: WithOlineStatus<
            CartProperties
            | DeliveryProperties
            | ItemReportProperties
            | ItemRequestProperties
            | CheckoutProperties
            | CancelCheckoutProperties
            | ItemProperties
        >;
    }
    | {
        name: ELearningTypes;
        property?: string;
        ctx?: WithOlineStatus<ExamProperties | ModuleProperties | QuestionProperties>;
    }
    | {
        name: PaymentsEventTypes;
        property?: string;
        ctx?: WithOlineStatus<PaymentMethodProperties>;
    }
    | {
        name: LoyaltyTypes;
        property?: string;
        ctx?: WithOlineStatus<
            LevelProperties
            | MilestoneProperties
            | PromoProperties
            | RewardProperties
            | SurveyProperties
        >;
    }
    | {
        name: CallCenterEventType;
        property?: string;
        ctx?: WithOlineStatus<BreakProperties | ContactProperties | OpsScorecardProperties>;
    }
    | {
        name: EmergencyMgmtEventType;
        property?: string;
        ctx?: WithOlineStatus<IncidentProperties | AmbulanceProperties>;
    }
    | {
        name: PatientMgmtEventType;
        property?: string;
        ctx?: WithOlineStatus<PatientProperties | EncounterProperties | AppointmentProperties>;
    }
    | {
        name: string;
        property?: string;
        ctx?: WithOlineStatus<Record<string, string | number>>;
    };

export type EventCommon = {
    time: string;
};

export type Event = EventTeaser & EventCommon;

export type InternalInfoObject = {
    s_id: string;
    sdk: string;
    app_id: string;
    app_version: string;
    device_id: string;
    device_os: string;
};

export type EventMainObject = {
    id: string;
    tz: string;
    internal: InternalInfoObject;
    data?: Event[];
};

export interface QueueItem {
    uuid: string;
    event: Event;
    time: number;
    retries: number;
    lastRetry?: number;
}

export interface QueueCatalogItem {
    uuid: string;
    catalog: CatalogItemModel;
    retries: number;
    lastRetry?: number;
}

export interface TypedRows {
    header: string[];
    data_type: string[];
    rows: any[][];
}

export interface ExceptionObject {
    title: string;
    event_type: string;
    exception_type: string;
    exception_source: string;
    stack_trace: string;
    ts: string;
}

export interface MainExceptionBody {
    id: string;
    tz: string;
    internal: InternalInfoObject;
    data: ExceptionObject[];
}


export enum GlobalSDKEventTypes {
    // CORE
    Identify = 'identify',
    Page = 'page',
    App = 'app',
    Search = 'search',
    Media = 'media',
    ActionResponse = 'action_response',
    Rate = 'rate',
    ModuleSelection = 'module_selection',

    // ECommerce
    Item = 'item',
    Delivery = 'delivery',
    Checkout = 'checkout',
    Cart = 'cart',
    CancelCheckout = 'cancel_checkout',
    ItemReport = 'item_report',
    ItemRequest = 'item_request',

    // ELearning
    Module = 'module',
    Exam = 'exam',
    Question = 'question',

    // Loyalty
    Level = 'level',
    Milestone = 'milestone',
    Promo = 'promo',
    Survey = 'survey',
    Reward = 'reward',

    // Payments
    Payment = 'payment',

    // Patient Mgmt
    Patient = 'patient',
    Encounter = 'encounter',
    Appointment = 'appointment',
    Diagnosis = 'diagnosis',
}
