"""Tesla Fleet API – čisté parsery (bez sítě/DB)."""

from __future__ import annotations

import unittest

from services.tesla_client import parse_charge_state


class ParseChargeStateTests(unittest.TestCase):
    def test_full_response(self) -> None:
        data = {
            "response": {
                "vin": "5YJYGDEE0MF000000",
                "charge_state": {
                    "battery_level": 47,
                    "charge_limit_soc": 80,
                    "charging_state": "Stopped",
                },
            }
        }
        out = parse_charge_state(data)
        self.assertEqual(out["battery_level"], 47)
        self.assertEqual(out["charge_limit_soc"], 80)
        self.assertEqual(out["vin"], "5YJYGDEE0MF000000")

    def test_missing_level_returns_none(self) -> None:
        self.assertIsNone(parse_charge_state({"response": {"charge_state": {}}}))
        self.assertIsNone(parse_charge_state({}))

    def test_odometer_miles_to_km(self) -> None:
        data = {
            "response": {
                "charge_state": {"battery_level": 60},
                "vehicle_state": {"odometer": 12345.6},  # míle!
            }
        }
        out = parse_charge_state(data)
        self.assertAlmostEqual(out["odometer_km"], 19868.3, places=1)

    def test_missing_odometer_is_none(self) -> None:
        data = {"response": {"charge_state": {"battery_level": 60}}}
        self.assertIsNone(parse_charge_state(data)["odometer_km"])

    def test_zero_limit_normalized_to_none(self) -> None:
        data = {"response": {"charge_state": {"battery_level": 10, "charge_limit_soc": 0}}}
        self.assertIsNone(parse_charge_state(data)["charge_limit_soc"])


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