import sys
import time

_t_import = time.perf_counter()
import sm3_entry as entry
from resources.lib.utils.log_utils import get_logger

try:
    from resources.lib.database import migration_v3
    migration_v3.perform_migration_v2_to_v3()
except Exception as e:
    import logging
    logging.error(f"[V3 Migration] Nelze spustit pred startem: {e}")

log = get_logger()
log.info(
    "SM2-perf: import sm3_entry/router %.1f ms"
    % ((time.perf_counter() - _t_import) * 1000.0)
)

if __name__ == '__main__':
    log.info(f"Router starting with args: {sys.argv}")
    # In Kodi, sys.argv is typically ['plugin_url', 'handle', 'query_string']
    # The query_string is at index 2, starting with '?'
    if len(sys.argv) > 2:
        entry.router(sys.argv[2][1:])
    else:
        # This can happen when the addon is called without parameters,
        # e.g. from a home screen widget or library item.
        # We pass an empty string to the router to handle the default action.
        log.info("Router started with no query string, running default action.")
        entry.router('')
