from __future__ import annotations

import asyncio
import logging
from datetime import datetime, timedelta

from apscheduler.events import EVENT_JOB_ERROR, EVENT_JOB_EXECUTED, EVENT_JOB_MISSED
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
from apscheduler.triggers.date import DateTrigger

from server.ai import generate_daily_motivation, generate_roast_of_the_day
from server.config_loader import load_meds_config, slot_to_dict, today_str
from server.db import get_db, utc_now_iso
from server.messages import pick
from server.push import send_slot_reminder
from server.slots import build_today, get_log_for_day, mark_missed_for_overdue

logger = logging.getLogger(__name__)
scheduler = AsyncIOScheduler()


async def _remind_slot(slot_id: str) -> None:
    config = load_meds_config()
    tz = config.timezone
    day = today_str(tz)
    logger.info("Reminder triggered slot=%s day=%s", slot_id, day)
    db = await get_db()
    try:
        logs = await get_log_for_day(db, day)
        if slot_id in logs and logs[slot_id]["status"] == "taken":
            logger.info("Reminder skipped slot=%s day=%s reason=already_taken", slot_id, day)
            return
        slot = next((s for s in config.slots if s.id == slot_id), None)
        if not slot:
            logger.warning("Reminder skipped slot=%s day=%s reason=unknown_slot", slot_id, day)
            return
        title = f"{slot.label} — Med-Time!"
        body = pick("reminder", label=slot.label, med=slot.meds[0].name if slot.meds else "Medis")
        sent = await send_slot_reminder(db, slot_id, title, body)
        logger.info("Reminder finished slot=%s day=%s sent=%d", slot_id, day, sent)
    finally:
        await db.close()


async def _generate_daily_motivation() -> None:
    config = load_meds_config()
    db = await get_db()
    try:
        await generate_daily_motivation(db, config.timezone)
    except Exception:
        logger.exception("Daily motivation generation failed")
    finally:
        await db.close()


async def _generate_daily_roast() -> None:
    config = load_meds_config()
    db = await get_db()
    try:
        await generate_roast_of_the_day(db, config.timezone)
    except Exception:
        logger.exception("Daily roast generation failed")
    finally:
        await db.close()


async def _evening_check() -> None:
    logger.info("Evening check triggered")
    db = await get_db()
    try:
        marked = await mark_missed_for_overdue(db)
        if not marked:
            logger.info("Evening check done marked=0")
            return
        logger.info("Evening check marked missed slots=%s", ",".join(marked))
        for slot_id in marked:
            body = pick("missed")
            sent = await send_slot_reminder(db, slot_id, "Verpasst?", body)
            logger.info("Evening reminder slot=%s sent=%d", slot_id, sent)
    finally:
        await db.close()


async def schedule_snooze(slot_id: str, minutes: int) -> str:
    config = load_meds_config()
    tz = config.timezone
    now = datetime.now(tz)
    until = now + timedelta(minutes=minutes)
    day = today_str(tz)

    db = await get_db()
    try:
        await db.execute(
            """
            INSERT INTO snooze (slot_id, day, snooze_until) VALUES (?, ?, ?)
            ON CONFLICT(slot_id, day) DO UPDATE SET snooze_until=excluded.snooze_until
            """,
            (slot_id, day, until.isoformat()),
        )
        await db.commit()
    finally:
        await db.close()

    run_at = until.astimezone(tz).replace(tzinfo=None)
    scheduler.add_job(
        _remind_slot,
        trigger=DateTrigger(run_date=run_at),
        args=[slot_id],
        id=f"snooze-{slot_id}-{until.timestamp()}",
        replace_existing=False,
    )
    return until.isoformat()


def _scheduler_listener(event) -> None:
    if event.code == EVENT_JOB_MISSED:
        logger.warning(
            "Scheduler job missed id=%s scheduled=%s",
            event.job_id,
            event.scheduled_run_time,
        )
    elif event.code == EVENT_JOB_ERROR:
        logger.exception("Scheduler job error id=%s", event.job_id)
    elif event.code == EVENT_JOB_EXECUTED:
        logger.info("Scheduler job executed id=%s", event.job_id)


def start_scheduler() -> None:
    config = load_meds_config()
    tz = config.timezone

    if not scheduler.running:
        scheduler.add_listener(_scheduler_listener, EVENT_JOB_EXECUTED | EVENT_JOB_MISSED | EVENT_JOB_ERROR)

    for slot in config.slots:
        h, m = slot.time.hour, slot.time.minute
        scheduler.add_job(
            _remind_slot,
            trigger=CronTrigger(hour=h, minute=m, timezone=tz),
            args=[slot.id],
            id=f"remind-{slot.id}",
            replace_existing=True,
        )
        logger.info("Registered remind job slot=%s at %02d:%02d %s", slot.id, h, m, tz)

    scheduler.add_job(
        _evening_check,
        trigger=CronTrigger(hour=21, minute=0, timezone=tz),
        id="evening-check",
        replace_existing=True,
    )

    scheduler.add_job(
        _generate_daily_motivation,
        trigger=CronTrigger(hour=3, minute=0, timezone=tz),
        id="daily-motivation",
        replace_existing=True,
    )

    scheduler.add_job(
        _generate_daily_roast,
        trigger=CronTrigger(hour=3, minute=5, timezone=tz),
        id="daily-roast",
        replace_existing=True,
    )

    if not scheduler.running:
        scheduler.start()
        logger.info("Scheduler started for timezone %s", tz)
        for job in scheduler.get_jobs():
            logger.info("Scheduler job id=%s next=%s", job.id, job.next_run_time)
