from __future__ import annotations

import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch

from daily_report import config


class ConfigTest(unittest.TestCase):
    def test_reads_dotenv_and_allows_environment_override(self) -> None:
        with tempfile.TemporaryDirectory(prefix="daily-report-config-") as temp:
            root = Path(temp)
            (root / ".env").write_text(
                "\n".join(
                    [
                        "PORT=9000",
                        "BASE_URL=http://example.test",
                        "FEISHU_WEBHOOK_URL=https://open.feishu.cn/webhook",
                        "FEISHU_APP_ID=cli_xxx",
                        "FEISHU_APP_SECRET=secret",
                    ]
                ),
                encoding="utf-8",
            )
            fake_file = root / "daily_report" / "config.py"
            fake_file.parent.mkdir()
            fake_file.write_text("", encoding="utf-8")

            with patch.object(config, "__file__", str(fake_file)):
                result = config.read_config({"PORT": "9001"})

        self.assertEqual(result.port, 9001)
        self.assertEqual(result.base_url, "http://example.test")
        self.assertEqual(result.feishu_webhook_url, "https://open.feishu.cn/webhook")
        self.assertEqual(result.feishu_app_id, "cli_xxx")
        self.assertEqual(result.feishu_app_secret, "secret")


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