# coding: utf-8
from sqlalchemy import BigInteger, Boolean, Column, Integer, ARRAY, VARCHAR, text, VARCHAR
from sqlalchemy.ext.declarative import declarative_base
from pathlib import Path
import os

DATABASE = os.environ['DATABASE_NAME']

Base = declarative_base()
metadata = Base.metadata

base_path = Path(__file__).parent.parent

# Get a table name from a configuration file
try:
    events_table_name = os.environ['events_table_name']
except KeyError as e:
    events_table_name = None
    print(repr(e))
try:
    events_detailed_table_name = os.environ['events_detailed_table_name']
except KeyError as e:
    print(repr(e))
    events_detailed_table_name = None
sessions_table_name = os.environ['sessions_table']


class Session(Base):
    __tablename__ = sessions_table_name

    sessionid = Column(BigInteger, primary_key=True)
    user_agent = Column(VARCHAR(5000))
    user_browser = Column(VARCHAR(5000))
    user_browser_version = Column(VARCHAR(5000))
    user_country = Column(VARCHAR(5000))
    user_device = Column(VARCHAR(5000))
    user_device_heap_size = Column(BigInteger)
    user_device_memory_size = Column(BigInteger)
    user_device_type = Column(VARCHAR(5000))
    user_os = Column(VARCHAR(5000))
    user_os_version = Column(VARCHAR(5000))
    user_uuid = Column(VARCHAR(5000))
    connection_effective_bandwidth = Column(BigInteger)  # Downlink
    connection_type = Column(VARCHAR(5000))  # "bluetooth", "cellular", "ethernet", "none", "wifi", "wimax", "other", "unknown"
    metadata_key = Column(VARCHAR(5000))
    metadata_value = Column(VARCHAR(5000))
    referrer = Column(VARCHAR(5000))
    user_anonymous_id = Column(VARCHAR(5000))
    user_id = Column(VARCHAR(5000))

    # TIME
    session_start_timestamp = Column(BigInteger)
    session_end_timestamp = Column(BigInteger)
    session_duration = Column(BigInteger)

    # SPEED INDEX RELATED
    first_contentful_paint = Column(BigInteger)
    speed_index = Column(BigInteger)
    visually_complete = Column(BigInteger)
    timing_time_to_interactive = Column(BigInteger)

    # PERFORMANCE
    avg_cpu = Column(Integer)
    avg_fps = Column(BigInteger)
    max_cpu = Column(Integer)
    max_fps = Column(BigInteger)
    max_total_js_heap_size = Column(BigInteger)
    max_used_js_heap_size = Column(BigInteger)

    # ISSUES AND EVENTS
    js_exceptions_count = Column(BigInteger)
    long_tasks_total_duration = Column(BigInteger)
    long_tasks_max_duration = Column(BigInteger)
    long_tasks_count = Column(BigInteger)
    inputs_count = Column(BigInteger)
    clicks_count = Column(BigInteger)
    issues_count = Column(BigInteger)
    issues = ARRAY(VARCHAR(5000))
    urls_count = Column(BigInteger)
    urls = ARRAY(VARCHAR(5000))


class Event(Base):
    __tablename__ = events_table_name

    sessionid = Column(BigInteger, primary_key=True)
    connectioninformation_downlink = Column(BigInteger)
    connectioninformation_type = Column(VARCHAR(5000))
    consolelog_level = Column(VARCHAR(5000))
    consolelog_value = Column(VARCHAR(5000))
    customevent_messageid = Column(BigInteger)
    customevent_name = Column(VARCHAR(5000))
    customevent_payload = Column(VARCHAR(5000))
    customevent_timestamp = Column(BigInteger)
    errorevent_message = Column(VARCHAR(5000))
    errorevent_messageid = Column(BigInteger)
    errorevent_name = Column(VARCHAR(5000))
    errorevent_payload = Column(VARCHAR(5000))
    errorevent_source = Column(VARCHAR(5000))
    errorevent_timestamp = Column(BigInteger)
    jsexception_message = Column(VARCHAR(5000))
    jsexception_name = Column(VARCHAR(5000))
    jsexception_payload = Column(VARCHAR(5000))
    metadata_key = Column(VARCHAR(5000))
    metadata_value = Column(VARCHAR(5000))
    mouseclick_id = Column(BigInteger)
    mouseclick_hesitationtime = Column(BigInteger)
    mouseclick_label = Column(VARCHAR(5000))
    pageevent_firstcontentfulpaint = Column(BigInteger)
    pageevent_firstpaint = Column(BigInteger)
    pageevent_messageid = Column(BigInteger)
    pageevent_referrer = Column(VARCHAR(5000))
    pageevent_speedindex = Column(BigInteger)
    pageevent_timestamp = Column(BigInteger)
    pageevent_url = Column(VARCHAR(5000))
    pagerendertiming_timetointeractive = Column(BigInteger)
    pagerendertiming_visuallycomplete = Column(BigInteger)
    rawcustomevent_name = Column(VARCHAR(5000))
    rawcustomevent_payload = Column(VARCHAR(5000))
    setviewportsize_height = Column(BigInteger)
    setviewportsize_width = Column(BigInteger)
    timestamp_timestamp = Column(BigInteger)
    user_anonymous_id = Column(VARCHAR(5000))
    user_id = Column(VARCHAR(5000))
    issueevent_messageid = Column(BigInteger)
    issueevent_timestamp = Column(BigInteger)
    issueevent_type = Column(VARCHAR(5000))
    issueevent_contextstring = Column(VARCHAR(5000))
    issueevent_context = Column(VARCHAR(5000))
    issueevent_payload = Column(VARCHAR(5000))
    customissue_name = Column(VARCHAR(5000))
    customissue_payload = Column(VARCHAR(5000))
    received_at = Column(BigInteger)
    batch_order_number = Column(BigInteger)


class DetailedEvent(Base):
    __tablename__ = events_detailed_table_name

    # id = Column(Integer, primary_key=True, server_default=text("\"identity\"(119029, 0, '0,1'::text)"))
    sessionid = Column(BigInteger, primary_key=True)
    clickevent_hesitationtime = Column(BigInteger)
    clickevent_label = Column(VARCHAR(5000))
    clickevent_messageid = Column(BigInteger)
    clickevent_timestamp = Column(BigInteger)
    connectioninformation_downlink = Column(BigInteger)
    connectioninformation_type = Column(VARCHAR(5000))
    consolelog_level = Column(VARCHAR(5000))
    consolelog_value = Column(VARCHAR(5000))
    cpuissue_duration = Column(BigInteger)
    cpuissue_rate = Column(BigInteger)
    cpuissue_timestamp = Column(BigInteger)
    createdocument = Column(Boolean)
    createelementnode_id = Column(BigInteger)
    createelementnode_parentid = Column(BigInteger)
    cssdeleterule_index = Column(BigInteger)
    cssdeleterule_stylesheetid = Column(BigInteger)
    cssinsertrule_index = Column(BigInteger)
    cssinsertrule_rule = Column(VARCHAR(5000))
    cssinsertrule_stylesheetid = Column(BigInteger)
    customevent_messageid = Column(BigInteger)
    customevent_name = Column(VARCHAR(5000))
    customevent_payload = Column(VARCHAR(5000))
    customevent_timestamp = Column(BigInteger)
    domdrop_timestamp = Column(BigInteger)
    errorevent_message = Column(VARCHAR(5000))
    errorevent_messageid = Column(BigInteger)
    errorevent_name = Column(VARCHAR(5000))
    errorevent_payload = Column(VARCHAR(5000))
    errorevent_source = Column(VARCHAR(5000))
    errorevent_timestamp = Column(BigInteger)
    fetch_duration = Column(BigInteger)
    fetch_method = Column(VARCHAR(5000))
    fetch_request = Column(VARCHAR(5000))
    fetch_response = Column(VARCHAR(5000))
    fetch_status = Column(BigInteger)
    fetch_timestamp = Column(BigInteger)
    fetch_url = Column(VARCHAR(5000))
    graphql_operationkind = Column(VARCHAR(5000))
    graphql_operationname = Column(VARCHAR(5000))
    graphql_response = Column(VARCHAR(5000))
    graphql_variables = Column(VARCHAR(5000))
    graphqlevent_messageid = Column(BigInteger)
    graphqlevent_name = Column(VARCHAR(5000))
    graphqlevent_timestamp = Column(BigInteger)
    inputevent_label = Column(VARCHAR(5000))
    inputevent_messageid = Column(BigInteger)
    inputevent_timestamp = Column(BigInteger)
    inputevent_value = Column(VARCHAR(5000))
    inputevent_valuemasked = Column(Boolean)
    jsexception_message = Column(VARCHAR(5000))
    jsexception_name = Column(VARCHAR(5000))
    jsexception_payload = Column(VARCHAR(5000))
    memoryissue_duration = Column(BigInteger)
    memoryissue_rate = Column(BigInteger)
    memoryissue_timestamp = Column(BigInteger)
    metadata_key = Column(VARCHAR(5000))
    metadata_value = Column(VARCHAR(5000))
    mobx_payload = Column(VARCHAR(5000))
    mobx_type = Column(VARCHAR(5000))
    mouseclick_id = Column(BigInteger)
    mouseclick_hesitationtime = Column(BigInteger)
    mouseclick_label = Column(VARCHAR(5000))
    mousemove_x = Column(BigInteger)
    mousemove_y = Column(BigInteger)
    movenode_id = Column(BigInteger)
    movenode_index = Column(BigInteger)
    movenode_parentid = Column(BigInteger)
    ngrx_action = Column(VARCHAR(5000))
    ngrx_duration = Column(BigInteger)
    ngrx_state = Column(VARCHAR(5000))
    otable_key = Column(VARCHAR(5000))
    otable_value = Column(VARCHAR(5000))
    pageevent_domcontentloadedeventend = Column(BigInteger)
    pageevent_domcontentloadedeventstart = Column(BigInteger)
    pageevent_firstcontentfulpaint = Column(BigInteger)
    pageevent_firstpaint = Column(BigInteger)
    pageevent_loaded = Column(Boolean)
    pageevent_loadeventend = Column(BigInteger)
    pageevent_loadeventstart = Column(BigInteger)
    pageevent_messageid = Column(BigInteger)
    pageevent_referrer = Column(VARCHAR(5000))
    pageevent_requeststart = Column(BigInteger)
    pageevent_responseend = Column(BigInteger)
    pageevent_responsestart = Column(BigInteger)
    pageevent_speedindex = Column(BigInteger)
    pageevent_timestamp = Column(BigInteger)
    pageevent_url = Column(VARCHAR(5000))
    pageloadtiming_domcontentloadedeventend = Column(BigInteger)
    pageloadtiming_domcontentloadedeventstart = Column(BigInteger)
    pageloadtiming_firstcontentfulpaint = Column(BigInteger)
    pageloadtiming_firstpaint = Column(BigInteger)
    pageloadtiming_loadeventend = Column(BigInteger)
    pageloadtiming_loadeventstart = Column(BigInteger)
    pageloadtiming_requeststart = Column(BigInteger)
    pageloadtiming_responseend = Column(BigInteger)
    pageloadtiming_responsestart = Column(BigInteger)
    pagerendertiming_speedindex = Column(BigInteger)
    pagerendertiming_timetointeractive = Column(BigInteger)
    pagerendertiming_visuallycomplete = Column(BigInteger)
    performancetrack_frames = Column(BigInteger)
    performancetrack_ticks = Column(BigInteger)
    performancetrack_totaljsheapsize = Column(BigInteger)
    performancetrack_usedjsheapsize = Column(BigInteger)
    performancetrackaggr_avgcpu = Column(BigInteger)
    performancetrackaggr_avgfps = Column(BigInteger)
    performancetrackaggr_avgtotaljsheapsize = Column(BigInteger)
    performancetrackaggr_avgusedjsheapsize = Column(BigInteger)
    performancetrackaggr_maxcpu = Column(BigInteger)
    performancetrackaggr_maxfps = Column(BigInteger)
    performancetrackaggr_maxtotaljsheapsize = Column(BigInteger)
    performancetrackaggr_maxusedjsheapsize = Column(BigInteger)
    performancetrackaggr_mincpu = Column(BigInteger)
    performancetrackaggr_minfps = Column(BigInteger)
    performancetrackaggr_mintotaljsheapsize = Column(BigInteger)
    performancetrackaggr_minusedjsheapsize = Column(BigInteger)
    performancetrackaggr_timestampend = Column(BigInteger)
    performancetrackaggr_timestampstart = Column(BigInteger)
    profiler_args = Column(VARCHAR(5000))
    profiler_duration = Column(BigInteger)
    profiler_name = Column(VARCHAR(5000))
    profiler_result = Column(VARCHAR(5000))
    rawcustomevent_name = Column(VARCHAR(5000))
    rawcustomevent_payload = Column(VARCHAR(5000))
    rawerrorevent_message = Column(VARCHAR(5000))
    rawerrorevent_name = Column(VARCHAR(5000))
    rawerrorevent_payload = Column(VARCHAR(5000))
    rawerrorevent_source = Column(VARCHAR(5000))
    rawerrorevent_timestamp = Column(BigInteger)
    redux_action = Column(VARCHAR(5000))
    redux_duration = Column(BigInteger)
    redux_state = Column(VARCHAR(5000))
    removenode_id = Column(BigInteger)
    removenodeattribute_id = Column(BigInteger)
    removenodeattribute_name = Column(VARCHAR(5000))
    resourceevent_decodedbodysize = Column(BigInteger)
    resourceevent_duration = Column(BigInteger)
    resourceevent_encodedbodysize = Column(BigInteger)
    resourceevent_headersize = Column(BigInteger)
    resourceevent_messageid = Column(BigInteger)
    resourceevent_method = Column(VARCHAR(5000))
    resourceevent_status = Column(BigInteger)
    resourceevent_success = Column(Boolean)
    resourceevent_timestamp = Column(BigInteger)
    resourceevent_ttfb = Column(BigInteger)
    resourceevent_type = Column(VARCHAR(5000))
    resourceevent_url = Column(VARCHAR(5000))
    resourcetiming_decodedbodysize = Column(BigInteger)
    resourcetiming_duration = Column(BigInteger)
    resourcetiming_encodedbodysize = Column(BigInteger)
    resourcetiming_headersize = Column(BigInteger)
    resourcetiming_initiator = Column(VARCHAR(5000))
    resourcetiming_timestamp = Column(BigInteger)
    resourcetiming_ttfb = Column(BigInteger)
    resourcetiming_url = Column(VARCHAR(5000))
    sessiondisconnect = Column(Boolean)
    sessiondisconnect_timestamp = Column(BigInteger)
    sessionend = Column(Boolean)
    sessionend_timestamp = Column(BigInteger)
    sessionstart_projectid = Column(BigInteger)
    sessionstart_revid = Column(VARCHAR(5000))
    sessionstart_timestamp = Column(BigInteger)
    sessionstart_trackerversion = Column(VARCHAR(5000))
    sessionstart_useragent = Column(VARCHAR(5000))
    sessionstart_userbrowser = Column(VARCHAR(5000))
    sessionstart_userbrowserversion = Column(VARCHAR(5000))
    sessionstart_usercountry = Column(VARCHAR(5000))
    sessionstart_userdevice = Column(VARCHAR(5000))
    sessionstart_userdeviceheapsize = Column(BigInteger)
    sessionstart_userdevicememorysize = Column(BigInteger)
    sessionstart_userdevicetype = Column(VARCHAR(5000))
    sessionstart_useros = Column(VARCHAR(5000))
    sessionstart_userosversion = Column(VARCHAR(5000))
    sessionstart_useruuid = Column(VARCHAR(5000))
    setcssdata_data = Column(BigInteger)
    setcssdata_id = Column(BigInteger)
    setinputchecked_checked = Column(BigInteger)
    setinputchecked_id = Column(BigInteger)
    setinputtarget_id = Column(BigInteger)
    setinputtarget_label = Column(BigInteger)
    setinputvalue_id = Column(BigInteger)
    setinputvalue_mask = Column(BigInteger)
    setinputvalue_value = Column(BigInteger)
    setnodeattribute_id = Column(BigInteger)
    setnodeattribute_name = Column(BigInteger)
    setnodeattribute_value = Column(BigInteger)
    setnodedata_data = Column(BigInteger)
    setnodedata_id = Column(BigInteger)
    setnodescroll_id = Column(BigInteger)
    setnodescroll_x = Column(BigInteger)
    setnodescroll_y = Column(BigInteger)
    setpagelocation_navigationstart = Column(BigInteger)
    setpagelocation_referrer = Column(VARCHAR(5000))
    setpagelocation_url = Column(VARCHAR(5000))
    setpagevisibility_hidden = Column(Boolean)
    setviewportscroll_x = Column(BigInteger)
    setviewportscroll_y = Column(BigInteger)
    setviewportsize_height = Column(BigInteger)
    setviewportsize_width = Column(BigInteger)
    stateaction_type = Column(VARCHAR(5000))
    stateactionevent_messageid = Column(BigInteger)
    stateactionevent_timestamp = Column(BigInteger)
    stateactionevent_type = Column(VARCHAR(5000))
    timestamp_timestamp = Column(BigInteger)
    useranonymousid_id = Column(VARCHAR(5000))
    userid_id = Column(VARCHAR(5000))
    vuex_mutation = Column(VARCHAR(5000))
    vuex_state = Column(VARCHAR(5000))
    longtask_timestamp = Column(BigInteger)
    longtask_duration = Column(BigInteger)
    longtask_context = Column(BigInteger)
    longtask_containertype = Column(BigInteger)
    longtask_containersrc = Column(VARCHAR(5000))
    longtask_containerid = Column(VARCHAR(5000))
    longtask_containername = Column(VARCHAR(5000))
    setnodeurlbasedattribute_id = Column(BigInteger)
    setnodeurlbasedattribute_name = Column(VARCHAR(5000))
    setnodeurlbasedattribute_value = Column(VARCHAR(5000))
    setnodeurlbasedattribute_baseurl = Column(VARCHAR(5000))
    setstyledata_id = Column(BigInteger)
    setstyledata_data = Column(VARCHAR(5000))
    setstyledata_baseurl = Column(VARCHAR(5000))
    issueevent_messageid = Column(BigInteger)
    issueevent_timestamp = Column(BigInteger)
    issueevent_type = Column(VARCHAR(5000))
    issueevent_contextstring = Column(VARCHAR(5000))
    issueevent_context = Column(VARCHAR(5000))
    issueevent_payload = Column(VARCHAR(5000))
    technicalinfo_type = Column(VARCHAR(5000))
    technicalinfo_value = Column(VARCHAR(5000))
    customissue_name = Column(VARCHAR(5000))
    customissue_payload = Column(VARCHAR(5000))
    pageclose = Column(Boolean)
    received_at = Column(BigInteger)
    batch_order_number = Column(BigInteger)

