import socket
from fastapi import FastAPI
from app.salary_analytics.integrations.salary_detect import SalaryDetect
from app.utils.logger import logger

salary_detect = SalaryDetect()


def register_events(app: FastAPI):
    @app.on_event("startup")
    async def startup_event():
        """Initialize the pipeline on startup."""
        try:
            logger.info("Initializing pipeline...")
            
            # Start autonomous salary detection loop
            salary_detect.start()
            logger.info("Started autonomous salary detection loop.")
            
            # Print network information
            hostname = socket.gethostname()
            ip_address = socket.gethostbyname(hostname)
            logger.info(f"Server running on hostname: {hostname}")
            logger.info(f"Server IP address: {ip_address}")
            logger.info(f"Server is accessible at:")
            logger.info(f"- http://localhost:8000")
            logger.info(f"- http://127.0.0.1:8000")
            logger.info(f"- http://{ip_address}:8000")
            logger.info("Pipeline initialized successfully")
        except Exception as e:
            logger.error(f"Error during startup: {str(e)}")
            raise

    @app.on_event("shutdown")
    async def shutdown_event():
        logger.info("Shutting down Salary Analytics API...")
