import unittest
from pathlib import Path


ROOT = Path(__file__).resolve().parents[2]


def source(path: str) -> str:
    return (ROOT / path).read_text(encoding="utf-8")


class RuntimeConfigIntegrationTests(unittest.TestCase):
    def test_ai_service_reads_runtime_config(self) -> None:
        ai_service = source("backend/app/services/ai_service.py")

        self.assertIn("get_ai_config", ai_service)
        self.assertNotIn('AI_API_KEY = os.getenv("AI_API_KEY"', ai_service)
        self.assertIn("https://api.minimax.io/v1/chat/completions", ai_service)
        self.assertIn("https://api.minimaxi.com/v1/chat/completions", ai_service)
        self.assertIn("MiniMax-M2.7", ai_service)
        self.assertIn('removeprefix("Bearer ")', ai_service)
        self.assertIn('startswith("sk-cp-")', ai_service)
        self.assertIn("never reveal hidden reasoning", ai_service)
        self.assertIn('re.sub(r"<think>.*?</think>"', ai_service)
        self.assertIn("Do not introduce yourself unless the user asks who you are.", ai_service)
        self.assertIn("Prepare customer creation data", ai_service)
        self.assertIn('"action": "create_customer"', ai_service)
        self.assertIn('return the same JSON with "confirmed": true', ai_service)
        self.assertIn("def validate_customer_data", ai_service)
        self.assertIn("import asyncio", ai_service)
        self.assertIn("for attempt in range(2):", ai_service)
        self.assertIn("except httpx.HTTPError as e:", ai_service)

    def test_telegram_config_includes_chat_id(self) -> None:
        config_core = source("backend/app/core/config.py")
        config_api = source("backend/app/api/config.py")
        telegram_api = source("backend/app/api/telegram.py")
        telegram_service = source("backend/app/services/telegram_service.py")

        self.assertIn('"chat_id": get_config_value("telegram_chat_id")', config_core)
        self.assertIn("chat_id: str | None = None", config_api)
        self.assertIn("Telegram chat_id not configured", telegram_api)
        self.assertIn("_raise_telegram_error", telegram_api)
        self.assertIn("except HTTPError as e:", telegram_service)
        self.assertIn('data.get("description", description)', telegram_service)

    def test_backend_exposes_smtp_test_and_ai_config_routes(self) -> None:
        config_api = source("backend/app/api/config.py")

        self.assertIn('"/smtp/test"', config_api)
        self.assertIn("AIConfigResponse", config_api)
        self.assertIn('"/ai"', config_api)

    def test_reminders_fall_back_to_contact_email(self) -> None:
        reminder_service = source("backend/app/services/reminder_service.py")

        self.assertIn("def _customer_email_recipient", reminder_service)
        self.assertIn('getattr(contact, "is_primary", False)', reminder_service)
        self.assertIn("if reminder.send_email and recipient:", reminder_service)
        self.assertIn('"customer_email": recipient or ""', reminder_service)


if __name__ == "__main__":
    unittest.main()
