# Copyright (c) 2025 Patrick Motsch
# All rights reserved.
"""
CommCoach Scheduler Service.
Handles daily reminders and scheduled email summaries.
"""

import logging
from typing import Dict, Any, List

logger = logging.getLogger(__name__)


def registerScheduledJobs(eventManagement):
    """Register CommCoach scheduled jobs with the event management system."""
    try:
        eventManagement.registerCron(
            jobId="commcoach_daily_reminder",
            func=_runDailyReminders,
            cronKwargs={"hour": 8, "minute": 0},
        )
        logger.info("CommCoach scheduler: daily reminder job registered at 08:00")
    except Exception as e:
        logger.error(f"CommCoach scheduler: failed to register jobs: {e}")


async def _runDailyReminders():
    """Send daily coaching reminders to users who have opted in."""
    try:
        from modules.shared.configuration import APP_CONFIG
        from modules.connectors.connectorDbPostgre import DatabaseConnector
        from .datamodelCommcoach import CoachingUserProfile, CoachingContextStatus
        from modules.interfaces.interfaceMessaging import getInterface as getMessagingInterface

        dbHost = APP_CONFIG.get("DB_HOST", "_no_config_default_data")
        db = DatabaseConnector(
            dbHost=dbHost,
            dbDatabase="poweron_commcoach",
            dbUser=APP_CONFIG.get("DB_USER"),
            dbPassword=APP_CONFIG.get("DB_PASSWORD_SECRET"),
            dbPort=int(APP_CONFIG.get("DB_PORT", 5432)),
            userId="system",
        )

        profiles = db.getRecordset(CoachingUserProfile, recordFilter={"dailyReminderEnabled": True})
        if not profiles:
            return

        messaging = getMessagingInterface()

        from modules.interfaces.interfaceDbApp import getRootInterface
        rootInterface = getRootInterface()

        sentCount = 0
        for profile in profiles:
            try:
                userId = profile.get("userId")
                user = rootInterface.getUser(userId)
                if not user or not user.email:
                    continue

                # Check if user has active contexts
                from .datamodelCommcoach import CoachingContext
                contexts = db.getRecordset(CoachingContext, recordFilter={
                    "userId": userId,
                    "status": CoachingContextStatus.ACTIVE.value,
                })
                if not contexts:
                    continue

                contextTitles = [c.get("title", "Unbenannt") for c in contexts[:3]]
                contextList = ", ".join(contextTitles)

                subject = "Dein taegliches Coaching wartet"
                message = f"""
                <h2>Zeit fuer dein Coaching</h2>
                <p>Du hast aktive Coaching-Themen: <strong>{contextList}</strong></p>
                <p>Nimm dir 10 Minuten fuer eine kurze Session. Konsistenz ist der Schluessel zu Fortschritt.</p>
                <p>Dein aktueller Streak: <strong>{profile.get('streakDays', 0)} Tage</strong></p>
                """

                messaging.send("email", user.email, subject, message)
                sentCount += 1
            except Exception as e:
                logger.warning(f"Failed to send reminder to user {profile.get('userId')}: {e}")

        if sentCount > 0:
            logger.info(f"CommCoach scheduler: sent {sentCount} daily reminders")

    except Exception as e:
        logger.error(f"CommCoach daily reminders failed: {e}")
