"""
Core utilities for the news system.
"""

import os
import uuid
from datetime import datetime, timezone
from zoneinfo import ZoneInfo

from loguru import logger


def get_local_date_string(settings_manager=None) -> str:
    """
    Get the current date as a string in the user's configured timezone.

    This is used for replacing the YYYY-MM-DD placeholder in news subscriptions.
    The timezone can be configured via:
    1. The 'app.timezone' setting in the database
    2. The TZ environment variable
    3. Falls back to UTC if neither is set

    Args:
        settings_manager: Optional settings manager to read timezone setting from.
                         If not provided, will check TZ env var or default to UTC.

    Returns:
        str: Current date in ISO format (YYYY-MM-DD) in the configured timezone
    """
    tz_name = None

    # Try to get timezone from settings
    if settings_manager is not None:
        try:
            tz_name = settings_manager.get_setting("app.timezone")
        except Exception as e:
            logger.debug(f"Could not read timezone from settings: {e}")

    # Fall back to TZ environment variable
    if not tz_name:
        tz_name = os.environ.get("TZ")

    # Default to UTC if nothing is configured
    if not tz_name:
        tz_name = "UTC"

    try:
        tz = ZoneInfo(tz_name)
        local_date = datetime.now(tz).date()
        logger.debug(f"Using timezone {tz_name}, local date is {local_date}")
        return local_date.isoformat()
    except Exception:
        logger.warning(f"Invalid timezone '{tz_name}', falling back to UTC")
        return datetime.now(timezone.utc).date().isoformat()


def generate_card_id() -> str:
    """
    Generate a unique ID for a news card using UUID.

    Returns:
        str: A unique UUID string
    """
    return str(uuid.uuid4())


def generate_subscription_id() -> str:
    """
    Generate a unique ID for a subscription.

    Returns:
        str: A unique UUID string
    """
    return str(uuid.uuid4())


def utc_now() -> datetime:
    """
    Get current UTC time with timezone awareness.

    Returns:
        datetime: Current UTC time
    """
    return datetime.now(timezone.utc)


def hours_ago(dt: datetime) -> float:
    """
    Calculate how many hours ago a datetime was.

    Args:
        dt: The datetime to compare

    Returns:
        float: Number of hours ago (negative if in future)
    """
    if dt.tzinfo is None:
        dt = dt.replace(tzinfo=timezone.utc)

    delta = utc_now() - dt
    return delta.total_seconds() / 3600
