import json
import logging
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen

from app.core.config import get_telegram_config

logger = logging.getLogger(__name__)

TELEGRAM_API_URL = "https://api.telegram.org"


async def send_telegram_message(chat_id: str, text: str) -> dict:
    """Send a message via Telegram Bot API. Returns {success, error, status_code}."""
    bot_token = (get_telegram_config().get("bot_token") or "").strip()
    chat_id = (chat_id or "").strip()

    if not bot_token:
        return {
            "success": False,
            "error": "Telegram bot token not configured",
            "status_code": 400,
        }
    if not chat_id:
        return {
            "success": False,
            "error": "Telegram chat_id not configured",
            "status_code": 400,
        }

    url = f"{TELEGRAM_API_URL}/bot{bot_token}/sendMessage"
    payload = json.dumps(
        {
            "chat_id": chat_id,
            "text": text,
            "parse_mode": "HTML",
            "disable_web_page_preview": True,
        }
    ).encode("utf-8")

    try:
        req = Request(url, data=payload, headers={"Content-Type": "application/json"})
        with urlopen(req, timeout=30) as resp:
            data = json.loads(resp.read().decode("utf-8"))
            if data.get("ok"):
                return {"success": True, "error": None, "status_code": 200}
            return {
                "success": False,
                "error": data.get("description", "Unknown Telegram error"),
                "status_code": 400,
            }
    except HTTPError as e:
        description = f"Telegram API error: HTTP {e.code}"
        try:
            body = e.read().decode("utf-8")
            data = json.loads(body)
            description = data.get("description", description)
        except Exception:
            pass
        logger.error("Telegram send failed: %s", description)
        status_code = 400 if e.code in (400, 401, 403, 404) else 502
        return {"success": False, "error": description, "status_code": status_code}
    except URLError as e:
        logger.error("Telegram send failed: %s", e)
        return {"success": False, "error": str(e), "status_code": 502}
    except Exception as e:
        logger.error("Telegram send failed: %s", e)
        return {"success": False, "error": str(e), "status_code": 500}


def format_invoice_message(invoice) -> str:
    """Format an invoice as a Telegram message."""
    status_value = str(invoice.status.value if hasattr(invoice.status, "value") else invoice.status)
    status_emoji = {
        "draft": "[DRAFT]",
        "issued": "[ISSUED]",
        "sent": "[SENT]",
        "partially_paid": "[PARTIAL]",
        "paid": "[PAID]",
        "overdue": "[OVERDUE]",
        "void": "[VOID]",
    }
    emoji = status_emoji.get(status_value, "[INFO]")
    return (
        f"Invoice {invoice.invoice_number}\n"
        f"{emoji}\n"
        f"Customer: {getattr(invoice.customer, 'name', 'N/A')}\n"
        f"Amount: {invoice.currency} {float(invoice.total_amount):,.2f}\n"
        f"Status: {status_value.upper()}\n"
        f"Due Date: {invoice.due_date or 'Upon receipt'}"
    )


def format_reminder_message(reminder_type: str, entity_name: str, days_remaining: int, details: str = "") -> str:
    """Format a reminder notification as Telegram message."""
    if reminder_type == "subscription_expiry":
        return (
            "Subscription Expiry Reminder\n"
            f"Subscription: {entity_name}\n"
            f"Expires in: {days_remaining} days\n"
            f"{details}"
        )
    if reminder_type == "invoice_overdue":
        return (
            "Invoice Overdue\n"
            f"Invoice: {entity_name}\n"
            f"Days overdue: {days_remaining}\n"
            f"{details}"
        )
    return (
        "Reminder\n"
        f"Item: {entity_name}\n"
        f"{details}"
    )
