import json
from pathlib import Path
from typing import Any

import pytest

from datatypes.magicplan.api.response import MagicPlanPlan
from datatypes.magicplan.domain.mapper import map_plan
from datatypes.magicplan.domain.models import Plan

FIXTURE_DIR = Path(__file__).parents[4] / "backend" / "magic_plan"
PLAN_ID = "a7285ed1-878d-47eb-8aa6-85ef9e187516"
PLAN_ID_2 = "9f9889ff-793e-4e9a-a6f0-e22f5b0f5365"


@pytest.fixture(scope="module")
def raw_data() -> dict[str, Any]:
    payload = json.loads(
        (FIXTURE_DIR / "magicplan_api_plan_response_example.json").read_text()
    )
    return payload["data"]


@pytest.fixture(scope="module")
def mp(raw_data: dict[str, Any]) -> MagicPlanPlan:
    return MagicPlanPlan.model_validate(raw_data)


@pytest.fixture(scope="module")
def plan(mp: MagicPlanPlan) -> Plan:
    return map_plan(mp)


def test_plan_uid(plan: Plan):
    assert plan.uid == PLAN_ID


def test_floor_count(plan: Plan):
    assert len(plan.floors) == 2


def test_first_room_name(plan: Plan):
    assert plan.floors[0].rooms[0].name == "Kitchen"


def test_room_dimensions_are_floats(plan: Plan):
    room = plan.floors[0].rooms[0]
    assert isinstance(room.width_m, float)
    assert isinstance(room.length_m, float)
    assert isinstance(room.area_m2, float)


def test_room_area_rounded_to_2dp(plan: Plan):
    room = plan.floors[0].rooms[0]
    assert room.area_m2 == 7.95


def test_room_dimensions_parsed_from_string(plan: Plan):
    room = plan.floors[0].rooms[0]
    assert room.width_m == pytest.approx(2.67)
    assert room.length_m == pytest.approx(2.98)


def test_kitchen_has_windows(plan: Plan):
    room = plan.floors[0].rooms[0]
    assert len(room.windows) >= 1


def test_window_fields_are_floats(plan: Plan):
    window = plan.floors[0].rooms[0].windows[0]
    assert isinstance(window.width_m, float)
    assert isinstance(window.height_m, float)
    assert isinstance(window.area_m2, float)


def test_window_opening_type_prefix_stripped(plan: Plan):
    window = plan.floors[0].rooms[0].windows[0]
    assert not window.opening_type.startswith("window")
    assert window.opening_type == "casement"


def test_window_area_is_width_times_height(plan: Plan):
    window = plan.floors[0].rooms[0].windows[0]
    assert window.area_m2 == pytest.approx(window.width_m * window.height_m, rel=1e-2)


def test_window_dimensions_rounded_to_2dp(plan: Plan):
    window = plan.floors[0].rooms[0].windows[0]
    assert window.width_m == 1.40
    assert window.height_m == 1.20
    assert window.area_m2 == 1.68


def test_door_width_rounded_to_2dp(plan: Plan):
    door = plan.floors[0].rooms[0].doors[0]
    assert door.width_mm == 0.79


def test_kitchen_has_doors(plan: Plan):
    room = plan.floors[0].rooms[0]
    assert len(room.doors) >= 1


def test_door_width_is_float(plan: Plan):
    door = plan.floors[0].rooms[0].doors[0]
    assert isinstance(door.width_mm, float)


# --- Fixture 2: magicplan_api_plan_response_example_2.json ---


@pytest.fixture(scope="module")
def raw_data_2() -> dict[str, Any]:
    payload = json.loads(
        (FIXTURE_DIR / "magicplan_api_plan_response_example_2.json").read_text()
    )
    return payload["data"]


@pytest.fixture(scope="module")
def plan2(raw_data_2: dict[str, Any]) -> Plan:
    return map_plan(MagicPlanPlan.model_validate(raw_data_2))


def test_plan2_uid(plan2: Plan):
    assert plan2.uid == PLAN_ID_2


def test_plan2_floor_count(plan2: Plan):
    assert len(plan2.floors) == 3


def test_plan2_first_room_name(plan2: Plan):
    assert plan2.floors[0].rooms[0].name == "Toilet"


def test_plan2_room_area_rounded_to_2dp(plan2: Plan):
    room = plan2.floors[0].rooms[0]
    assert room.area_m2 == 0.96


def test_plan2_room_dimensions_parsed_from_string(plan2: Plan):
    room = plan2.floors[0].rooms[0]
    assert room.width_m == pytest.approx(1.12)
    assert room.length_m == pytest.approx(0.86)


def test_plan2_room_with_no_windows(plan2: Plan):
    hall = plan2.floors[0].rooms[1]
    assert hall.name == "Hall"
    assert hall.windows == []


def test_plan2_window_dimensions_rounded_to_2dp(plan2: Plan):
    window = plan2.floors[0].rooms[0].windows[0]
    assert window.width_m == 0.39
    assert window.height_m == 0.67
    assert window.area_m2 == 0.26


def test_plan2_window_opening_type_casement(plan2: Plan):
    window = plan2.floors[0].rooms[0].windows[0]
    assert window.opening_type == "casement"


def test_plan2_window_opening_type_hung(plan2: Plan):
    bathroom1 = plan2.floors[1].rooms[1]
    assert bathroom1.name == "Bathroom 1"
    assert bathroom1.windows[0].opening_type == "hung"


def test_plan2_door_width_rounded_to_2dp(plan2: Plan):
    door = plan2.floors[0].rooms[0].doors[0]
    assert door.width_mm == 0.71


# --- Address and postcode fields ---


def test_plan_postcode(plan: Plan):
    assert plan.postcode == "BR2 8BZ"


def test_plan_address(plan: Plan):
    assert plan.address == "2 Laburnum Way, Bromley, GB"


def test_plan2_postcode(plan2: Plan):
    assert plan2.postcode == "BR1 3LP"


def test_plan2_address(plan2: Plan):
    assert plan2.address == "11 Station Road, Bromley, GB"


# --- Fixture 3: street_number set, city absent ---


@pytest.fixture(scope="module")
def plan3() -> Plan:
    payload = json.loads(
        (FIXTURE_DIR / "magicplan_api_plan_response_example_3.json").read_text()
    )
    return map_plan(MagicPlanPlan.model_validate(payload["data"]))


def test_plan3_address_uses_street_number_and_omits_city(plan3: Plan):
    assert plan3.address == "2 Laburnum Way, GB"


def test_plan3_postcode(plan3: Plan):
    assert plan3.postcode == "BR2 8BZ"


# --- Fixture 4: street_number set, street absent ---


@pytest.fixture(scope="module")
def plan4() -> Plan:
    payload = json.loads(
        (FIXTURE_DIR / "magicplan_api_plan_response_example_4.json").read_text()
    )
    return map_plan(MagicPlanPlan.model_validate(payload["data"]))


def test_plan4_address_uses_street_number_when_street_absent(plan4: Plan):
    assert plan4.address == "2, Bromley, GB"
