"""Test cases for the Renault client API keys."""

import os
from datetime import datetime
from datetime import timezone
from typing import Any
from typing import cast

import aiohttp
import pytest
from aiointercept import aiointercept
from syrupy.assertion import SnapshotAssertion
from yarl import URL

from tests import fixtures
from tests.const import TEST_ACCOUNT_ID
from tests.const import TEST_COUNTRY
from tests.const import TEST_LOCALE_DETAILS
from tests.const import TEST_VIN
from tests.fixtures import DEFAULT_QUERY_STRING
from tests.fixtures import KCA_ADAPTER_PATH_V1
from tests.fixtures import KCM_ADAPTER_PATH
from tests.test_credential_store import get_logged_in_credential_store
from tests.test_renault_session import get_logged_in_session

from renault_api.exceptions import EndpointNotAvailableError
from renault_api.kamereon.helpers import DAYS_OF_WEEK
from renault_api.kamereon.models import ChargeSchedule
from renault_api.kamereon.models import HvacSchedule
from renault_api.renault_vehicle import RenaultVehicle


@pytest.fixture
def vehicle(websession: aiohttp.ClientSession) -> RenaultVehicle:
    """Fixture for testing RenaultVehicle."""
    return RenaultVehicle(
        account_id=TEST_ACCOUNT_ID,
        vin=TEST_VIN,
        session=get_logged_in_session(websession),
    )


def test_init(websession: aiohttp.ClientSession) -> None:
    """Test RenaultVehicle initialisation."""
    assert RenaultVehicle(
        account_id=TEST_ACCOUNT_ID,
        vin=TEST_VIN,
        session=get_logged_in_session(websession),
    )

    assert RenaultVehicle(
        account_id=TEST_ACCOUNT_ID,
        vin=TEST_VIN,
        websession=websession,
        country=TEST_COUNTRY,
        locale_details=TEST_LOCALE_DETAILS,
        credential_store=get_logged_in_credential_store(),
    )


@pytest.mark.asyncio
async def test_get_details(
    vehicle: RenaultVehicle, mocked_responses: aiointercept
) -> None:
    """Test get_details."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_40.1.json")
    assert await vehicle.get_details()

    # Ensure second call still works (ie. use cached value)
    assert await vehicle.get_details()


@pytest.mark.asyncio
async def test_get_car_adapter(
    vehicle: RenaultVehicle, mocked_responses: aiointercept
) -> None:
    """Test get_details."""
    fixtures.inject_get_car_adapter(mocked_responses, "zoe_40.1.json")
    assert await vehicle.get_car_adapter()

    # Ensure second call still works (ie. use cached value)
    assert await vehicle.get_car_adapter()


@pytest.mark.asyncio
async def test_get_contracts(
    vehicle: RenaultVehicle, mocked_responses: aiointercept
) -> None:
    """Test get_contracts."""
    fixtures.inject_get_vehicle_contracts(mocked_responses, "fr_FR.1.json")

    assert await vehicle.get_contracts()

    # Ensure second call still works (ie. use cached value)
    assert await vehicle.get_contracts()


@pytest.mark.asyncio
async def test_get_battery_status(
    vehicle: RenaultVehicle, mocked_responses: aiointercept
) -> None:
    """Test get_battery_status."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_40.1.json")
    fixtures.inject_get_battery_status(mocked_responses)
    assert await vehicle.get_battery_status()


@pytest.mark.asyncio
async def test_get_battery_soc(
    vehicle: RenaultVehicle, mocked_responses: aiointercept, snapshot: SnapshotAssertion
) -> None:
    """Test get_battery_soc."""
    fixtures.inject_get_vehicle_details(mocked_responses, "renault_5.1.json")
    fixtures.inject_get_battery_soc_levels(mocked_responses)
    assert await vehicle.get_battery_soc() == snapshot


@pytest.mark.asyncio
async def test_get_tyre_pressure(
    vehicle: RenaultVehicle, mocked_responses: aiointercept
) -> None:
    """Test get_tyre_pressure."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_50.1.json")
    fixtures.inject_get_tyre_pressure(mocked_responses)
    assert await vehicle.get_tyre_pressure()


@pytest.mark.asyncio
async def test_get_location(
    vehicle: RenaultVehicle, mocked_responses: aiointercept
) -> None:
    """Test get_location."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_50.1.json")
    fixtures.inject_get_location(mocked_responses)
    assert await vehicle.get_location()


@pytest.mark.asyncio
async def test_get_hvac_status(
    vehicle: RenaultVehicle, mocked_responses: aiointercept
) -> None:
    """Test get_hvac_status."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_50.1.json")
    fixtures.inject_get_hvac_status(mocked_responses, "zoe")
    assert await vehicle.get_hvac_status()


@pytest.mark.asyncio
async def test_get_hvac_settings(
    vehicle: RenaultVehicle, mocked_responses: aiointercept
) -> None:
    """Test get_hvac_settings."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_50.1.json")
    fixtures.inject_get_hvac_settings(mocked_responses)
    data = await vehicle.get_hvac_settings()

    assert data.mode == "scheduled"
    assert data.schedules
    schedules: list[HvacSchedule] = data.schedules
    assert schedules
    assert schedules[1].id == 2
    assert schedules[1].activated is True
    assert schedules[1].wednesday
    assert schedules[1].wednesday.readyAtTime == "T15:15Z"
    assert schedules[1].friday
    assert schedules[1].friday.readyAtTime == "T15:15Z"

    for i in (0, 2, 3, 4):
        assert schedules[i].id == i + 1
        assert schedules[i].activated is False
        for day in DAYS_OF_WEEK:
            assert schedules[i].__dict__[day] is None


@pytest.mark.asyncio
async def test_get_charge_mode(
    vehicle: RenaultVehicle, mocked_responses: aiointercept
) -> None:
    """Test get_charge_mode."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_40.1.json")
    fixtures.inject_get_charge_mode(mocked_responses)
    assert await vehicle.get_charge_mode()


@pytest.mark.parametrize(
    "mode",
    ["always", "delayed", "multi", "single"],
)
@pytest.mark.asyncio
async def test_get_charging_settings(
    vehicle: RenaultVehicle,
    mocked_responses: aiointercept,
    mode: str,
) -> None:
    """Test get_charging_settings using Megane e-Tech."""
    fixtures.inject_get_vehicle_details(mocked_responses, "megane_e-tech.2.json")
    fixtures.inject_get_charging_settings(mocked_responses, mode)
    assert await vehicle.get_charging_settings()


@pytest.mark.asyncio
async def test_get_cockpit(
    vehicle: RenaultVehicle, mocked_responses: aiointercept
) -> None:
    """Test get_cockpit."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_40.1.json")
    fixtures.inject_get_cockpit(mocked_responses, "zoe")
    assert await vehicle.get_cockpit()


@pytest.mark.asyncio
async def test_get_lock_status(
    vehicle: RenaultVehicle, mocked_responses: aiointercept
) -> None:
    """Test get_lock_status."""
    fixtures.inject_get_vehicle_details(mocked_responses, "duster.1.json")
    fixtures.inject_get_lock_status(mocked_responses)
    assert await vehicle.get_lock_status()


@pytest.mark.asyncio
async def test_get_lock_status_no_endpoint(
    vehicle: RenaultVehicle, mocked_responses: aiointercept
) -> None:
    """Test get_lock_status."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_50.1.json")
    fixtures.inject_get_lock_status(mocked_responses)

    with pytest.raises(
        EndpointNotAvailableError,
        match=f"{EndpointNotAvailableError('lock-status', 'X102VE')}",
    ):
        await vehicle.get_lock_status()


@pytest.mark.asyncio
async def test_get_charge_schedule(
    vehicle: RenaultVehicle, mocked_responses: aiointercept, snapshot: SnapshotAssertion
) -> None:
    """Test get_charging_settings."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_40.1.json")
    fixtures.inject_get_charge_schedule(mocked_responses, "single")
    assert await vehicle.get_charge_schedule() == snapshot


@pytest.mark.parametrize(
    "ev_schedule_type",
    [
        "single.active",
        "single.inactive",
    ],
)
@pytest.mark.asyncio
async def test_get_charge_schedule_kcm(
    vehicle: RenaultVehicle,
    mocked_responses: aiointercept,
    snapshot: SnapshotAssertion,
    ev_schedule_type: str,
) -> None:
    """Test get_charging_settings."""
    fixtures.inject_get_vehicle_details(mocked_responses, "renault_5.1.json")
    fixtures.inject_get_ev_settings(mocked_responses, ev_schedule_type)
    assert await vehicle.get_charge_schedule() == snapshot


@pytest.mark.asyncio
async def test_get_notification_settings(
    vehicle: RenaultVehicle, mocked_responses: aiointercept
) -> None:
    """Test get_notification_settings."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_50.1.json")
    fixtures.inject_get_notification_settings(mocked_responses)
    assert await vehicle.get_notification_settings()


@pytest.mark.asyncio
async def test_get_charge_history_month(
    vehicle: RenaultVehicle, mocked_responses: aiointercept
) -> None:
    """Test get_charge_history."""
    fixtures.inject_get_charge_history(mocked_responses, "202010", "202011", "month")
    assert await vehicle.get_charge_history(
        start=datetime(2020, 10, 1),
        end=datetime(2020, 11, 15),
        period="month",
    )


@pytest.mark.asyncio
async def test_get_charge_history_day(
    vehicle: RenaultVehicle, mocked_responses: aiointercept
) -> None:
    """Test get_charge_history."""
    fixtures.inject_get_charge_history(mocked_responses, "20201001", "20201115", "day")
    assert await vehicle.get_charge_history(
        start=datetime(2020, 10, 1),
        end=datetime(2020, 11, 15),
        period="day",
    )


@pytest.mark.asyncio
async def test_get_charges(
    vehicle: RenaultVehicle, mocked_responses: aiointercept
) -> None:
    """Test get_charges."""
    fixtures.inject_get_charges(mocked_responses, "20201001", "20201115")
    assert await vehicle.get_charges(
        start=datetime(2020, 10, 1),
        end=datetime(2020, 11, 15),
    )


@pytest.mark.asyncio
async def test_get_hvac_history(
    vehicle: RenaultVehicle, mocked_responses: aiointercept
) -> None:
    """Test get_hvac_history."""
    fixtures.inject_get_hvac_history(mocked_responses, "202010", "202011", "month")
    assert await vehicle.get_hvac_history(
        start=datetime(2020, 10, 1),
        end=datetime(2020, 11, 15),
        period="month",
    )


@pytest.mark.asyncio
async def test_get_hvac_sessions(
    vehicle: RenaultVehicle, mocked_responses: aiointercept
) -> None:
    """Test get_hvac_sessions."""
    fixtures.inject_get_hvac_sessions(mocked_responses, "20201001", "20201115")
    assert await vehicle.get_hvac_sessions(
        start=datetime(2020, 10, 1),
        end=datetime(2020, 11, 15),
    )


@pytest.mark.asyncio
async def test_set_ac_start(
    vehicle: RenaultVehicle, mocked_responses: aiointercept, snapshot: SnapshotAssertion
) -> None:
    """Test set_ac_start."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_40.1.json")
    url = fixtures.inject_set_hvac_start(mocked_responses, "start")
    assert await vehicle.set_ac_start(
        21, datetime(2020, 11, 24, 6, 30, tzinfo=timezone.utc)
    )

    request = mocked_responses.requests[("POST", URL(url))][0]
    assert request.kwargs["json"] == snapshot


@pytest.mark.asyncio
async def test_set_ac_stop(
    vehicle: RenaultVehicle, mocked_responses: aiointercept, snapshot: SnapshotAssertion
) -> None:
    """Test set_ac_stop."""
    url = fixtures.inject_set_hvac_start(mocked_responses, "cancel")
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_50.1.json")
    assert await vehicle.set_ac_stop()

    request = mocked_responses.requests[("POST", URL(url))][0]
    assert request.kwargs["json"] == snapshot


@pytest.mark.asyncio
async def test_set_battery_soc(
    vehicle: RenaultVehicle, mocked_responses: aiointercept, snapshot: SnapshotAssertion
) -> None:
    """Test set_battery_soc."""
    fixtures.inject_get_vehicle_details(mocked_responses, "renault_5.1.json")
    fixtures.inject_set_battery_soc_levels(mocked_responses)
    assert await vehicle.set_battery_soc(min=10, target=50) == snapshot


@pytest.mark.asyncio
async def test_set_charge_mode(
    vehicle: RenaultVehicle, mocked_responses: aiointercept, snapshot: SnapshotAssertion
) -> None:
    """Test set_charge_mode."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_40.1.json")
    url = fixtures.inject_set_charge_mode(mocked_responses, "schedule_mode")
    assert await vehicle.set_charge_mode("schedule_mode")

    request = mocked_responses.requests[("POST", URL(url))][0]
    assert request.kwargs["json"] == snapshot


@pytest.mark.asyncio
async def test_set_charge_schedules(
    vehicle: RenaultVehicle, mocked_responses: aiointercept, snapshot: SnapshotAssertion
) -> None:
    """Test set_charge_schedules."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_40.1.json")
    url = fixtures.inject_set_charge_schedule(mocked_responses, "schedules")

    schedules: list[ChargeSchedule] = []
    assert await vehicle.set_charge_schedules(schedules)

    request = mocked_responses.requests[("POST", URL(url))][0]
    assert request.kwargs["json"] == snapshot


@pytest.mark.asyncio
async def test_set_charge_schedules_empty(
    vehicle: RenaultVehicle, mocked_responses: aiointercept, snapshot: SnapshotAssertion
) -> None:
    """Test set_charge_schedules when mode is always and current schedules []."""
    fixtures.inject_get_vehicle_details(mocked_responses, "megane_e-tech.2.json")
    fixtures.inject_get_charging_settings(mocked_responses, "always")
    url = fixtures.inject_set_kcm_charge_schedule(mocked_responses, "empty")
    charge_schedules = await vehicle.get_charging_settings()
    schedules: list[dict[str, Any]] = [
        {
            "id": 1,
            "activated": True,
            "monday": {"startTime": "T01:00Z", "duration": 300},
            "wednesday": {"startTime": "T01:00Z", "duration": 300},
            "friday": {"startTime": "T01:00Z", "duration": 300},
            "sunday": {"startTime": "T01:01Z", "duration": 300},
        }
    ]
    for schedule in schedules:
        charge_schedules.update(schedule)
    sched = cast(list[ChargeSchedule], charge_schedules.schedules)
    assert await vehicle.set_charge_schedules(sched)

    request = mocked_responses.requests[("POST", URL(url))][0]
    assert request.kwargs["json"] == snapshot


@pytest.mark.asyncio
async def test_set_charge_schedules_update(
    vehicle: RenaultVehicle, mocked_responses: aiointercept, snapshot: SnapshotAssertion
) -> None:
    """Test set_charge_schedules when mode is always and current schedules []."""
    fixtures.inject_get_vehicle_details(mocked_responses, "megane_e-tech.2.json")
    fixtures.inject_get_charging_settings(mocked_responses, "multi")
    url = fixtures.inject_set_kcm_charge_schedule(mocked_responses, "scheduled")
    charge_schedules = await vehicle.get_charging_settings()
    schedules: list[dict[str, Any]] = [
        {
            "id": 1,
            "activated": True,
            "monday": {"startTime": "T02:00Z", "duration": 200},
            "tuesday": {"startTime": "T02:01Z", "duration": 200},
            "sunday": {"startTime": "T02:01Z", "duration": 200},
        },
        {
            "id": 3,
            "activated": True,
            "monday": {"startTime": "T01:00Z", "duration": 300},
            "tuesday": {"startTime": "T01:01Z", "duration": 300},
            "wednesday": {"startTime": "T01:00Z", "duration": 300},
            "thursday": {"startTime": "T01:00Z", "duration": 300},
            "friday": {"startTime": "T01:00Z", "duration": 300},
            "saturday": {"startTime": "T01:01Z", "duration": 300},
            "sunday": {"startTime": "T01:01Z", "duration": 300},
        },
    ]
    for schedule in schedules:
        charge_schedules.update(schedule)

    sched = cast(list[ChargeSchedule], charge_schedules.schedules)
    assert await vehicle.set_charge_schedules(sched)

    request = mocked_responses.requests[("POST", URL(url))][0]
    assert request.kwargs["json"] == snapshot


@pytest.mark.parametrize(
    ("vehicle_details", "arguments", "action_url", "action_result"),
    [
        (
            "megane_e-tech.2.json",
            {},
            f"{KCM_ADAPTER_PATH}/charge/start?{DEFAULT_QUERY_STRING}",
            "vehicle_kcm_action/charging-start.now.json",
        ),
        (
            "megane_e-tech.2.json",
            {"when": datetime(2026, 3, 6, 23, 45, tzinfo=timezone.utc)},
            f"{KCM_ADAPTER_PATH}/charge/start?{DEFAULT_QUERY_STRING}",
            "vehicle_kcm_action/charging-start.delayed.json",
        ),
        (
            "zoe_40.1.json",
            {},
            f"{KCA_ADAPTER_PATH_V1}/actions/charging-start?{DEFAULT_QUERY_STRING}",
            "vehicle_action/charging-start.start.json",
        ),
    ],
    ids=[
        "megane_e_tech_2-now",
        "megane_e_tech_2-delayed",
        "zoe_40_1-now",
    ],
)
@pytest.mark.asyncio
async def test_set_charge_start(
    vehicle: RenaultVehicle,
    mocked_responses: aiointercept,
    vehicle_details: str,
    arguments: dict[str, Any],
    action_url: str,
    action_result: str,
    snapshot: SnapshotAssertion,
) -> None:
    """Test set_charge_start."""
    fixtures.inject_get_vehicle_details(mocked_responses, vehicle_details)
    url = fixtures.inject_action(mocked_responses, action_url, action_result)

    assert await vehicle.set_charge_start(**arguments)
    request = mocked_responses.requests[("POST", URL(url))][0]
    assert request.kwargs["json"] == snapshot


@pytest.mark.asyncio
async def test_set_charge_start_r5(
    vehicle: RenaultVehicle, mocked_responses: aiointercept, snapshot: SnapshotAssertion
) -> None:
    """Test set_charge_start for Renault 5 E-TECH via ev/settings endpoint."""
    fixtures.inject_get_vehicle_details(mocked_responses, "renault_5.1.json")
    # GET current settings is required for kcm-settings mode
    fixtures.inject_get_ev_settings(mocked_responses, "single.active")
    # POST to ev/settings
    url = fixtures.inject_set_kcm_ev_settings_charge(mocked_responses, "start")

    assert await vehicle.set_charge_start()
    request = mocked_responses.requests[("POST", URL(url))][0]
    assert request.kwargs["json"] == snapshot


@pytest.mark.asyncio
async def test_set_hvac_schedules(
    vehicle: RenaultVehicle, mocked_responses: aiointercept, snapshot: SnapshotAssertion
) -> None:
    """Test set_hvac_schedules."""
    schedules: list[HvacSchedule] = []
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_40.1.json")
    url = fixtures.inject_set_hvac_schedules(mocked_responses)

    assert await vehicle.set_hvac_schedules(schedules)
    request = mocked_responses.requests[("POST", URL(url))][0]

    assert request.kwargs["json"] == snapshot


@pytest.mark.asyncio
async def test_start_horn(
    vehicle: RenaultVehicle, mocked_responses: aiointercept, snapshot: SnapshotAssertion
) -> None:
    """Test start_horn."""
    url = fixtures.inject_set_hornlight(mocked_responses, "horn")
    fixtures.inject_get_vehicle_details(mocked_responses, "renault_5.1.json")
    assert await vehicle.start_horn()

    request = mocked_responses.requests[("POST", URL(url))][0]
    assert request.kwargs["json"] == snapshot


@pytest.mark.asyncio
async def test_start_lights(
    vehicle: RenaultVehicle, mocked_responses: aiointercept, snapshot: SnapshotAssertion
) -> None:
    """Test start_lights."""
    url = fixtures.inject_set_hornlight(mocked_responses, "lights")
    fixtures.inject_get_vehicle_details(mocked_responses, "renault_5.1.json")
    assert await vehicle.start_lights()

    request = mocked_responses.requests[("POST", URL(url))][0]
    assert request.kwargs["json"] == snapshot


@pytest.mark.asyncio
async def test_refresh_location(
    vehicle: RenaultVehicle, mocked_responses: aiointercept, snapshot: SnapshotAssertion
) -> None:
    """Test refresh_location."""
    url = fixtures.inject_set_refresh_location(mocked_responses)
    fixtures.inject_get_vehicle_details(mocked_responses, "renault_5.1.json")
    assert await vehicle.refresh_location()

    request = mocked_responses.requests[("POST", URL(url))][0]
    assert request.kwargs["json"] == snapshot


@pytest.mark.asyncio
async def test_http_get(
    vehicle: RenaultVehicle, mocked_responses: aiointercept, snapshot: SnapshotAssertion
) -> None:
    """Test http_get."""
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_40.1.json")
    endpoint = await vehicle.get_full_endpoint("charge-schedule")
    url = fixtures.inject_get_charge_schedule(mocked_responses, "single")

    assert await vehicle.http_get(endpoint) == snapshot
    request = mocked_responses.requests[("GET", URL(url))][0]

    assert request.kwargs["json"] is None


@pytest.mark.asyncio
async def test_http_post(
    vehicle: RenaultVehicle, mocked_responses: aiointercept, snapshot: SnapshotAssertion
) -> None:
    """Test http_post."""
    endpoint = (
        "/commerce/v1/accounts/{account_id}"
        "/kamereon/kca/car-adapter/v1/cars/{vin}/actions/charging-start"
    )
    json = {
        "data": {
            "attributes": {
                "action": "start",
            },
            "type": "ChargingStart",
        },
    }
    url = fixtures.inject_action(
        mocked_responses,
        f"{KCA_ADAPTER_PATH_V1}/actions/charging-start?{DEFAULT_QUERY_STRING}",
        "vehicle_action/charging-start.start.json",
    )

    assert await vehicle.http_post(endpoint, json) == snapshot
    request = mocked_responses.requests[("POST", URL(url))][0]

    assert request.kwargs["json"] == snapshot


@pytest.mark.parametrize(
    "filename", fixtures.get_json_files(f"{fixtures.KAMEREON_FIXTURE_PATH}/vehicles")
)
@pytest.mark.asyncio
async def test_get_endpoints(
    vehicle: RenaultVehicle,
    mocked_responses: aiointercept,
    filename: str,
    snapshot: SnapshotAssertion,
) -> None:
    """Test get_endpoints."""
    filename = os.path.basename(filename)
    fixtures.inject_get_vehicle_details(mocked_responses, filename)
    details = await vehicle.get_details()
    endpoints = details.get_endpoints()

    assert endpoints == snapshot


@pytest.mark.asyncio
async def test_get_full_endpoint_unknown(
    vehicle: RenaultVehicle, mocked_responses: aiointercept
) -> None:
    """Test http_get."""
    # Unkown endpoint
    fixtures.inject_get_vehicle_details(mocked_responses, "zoe_40.1.json")
    with pytest.raises(EndpointNotAvailableError):
        await vehicle.get_full_endpoint("random")
