"""Diagnostics support for Renault."""

from typing import Any

from homeassistant.components.diagnostics import async_redact_data
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntry

from . import RenaultConfigEntry
from .const import RenaultConfigurationKeys
from .renault_vehicle import RenaultVehicleProxy

TO_REDACT = {
    RenaultConfigurationKeys.KAMEREON_ACCOUNT_ID,
    RenaultConfigurationKeys.LOGIN_TOKEN,
    RenaultConfigurationKeys.PASSWORD,
    RenaultConfigurationKeys.USERNAME,
    "radioCode",
    "registrationNumber",
    "vin",
    "gpsLatitude",
    "gpsLongitude",
}


async def async_get_config_entry_diagnostics(
    hass: HomeAssistant, entry: RenaultConfigEntry
) -> dict[str, Any]:
    """Return diagnostics for a config entry."""
    return {
        "entry": {
            "title": entry.title,
            "data": async_redact_data(entry.data, TO_REDACT),
        },
        "vehicles": [
            _get_vehicle_diagnostics(vehicle)
            for vehicle in entry.runtime_data.vehicles.values()
        ],
    }


async def async_get_device_diagnostics(
    hass: HomeAssistant, entry: RenaultConfigEntry, device: DeviceEntry
) -> dict[str, Any]:
    """Return diagnostics for a device."""
    vin = next(iter(device.identifiers))[1]
    vehicle = entry.runtime_data.vehicles[vin]

    return _get_vehicle_diagnostics(vehicle)


def _get_vehicle_diagnostics(vehicle: RenaultVehicleProxy) -> dict[str, Any]:
    """Return diagnostics for a device."""
    return {
        "details": async_redact_data(vehicle.details.raw_data, TO_REDACT),
        "data": {
            key: (
                async_redact_data(coordinator.data.raw_data, TO_REDACT)
                # Renault coordinators override async_config_entry_first_refresh
                # to not raise ConfigEntryNotReady, so coordinator data can be None
                if coordinator.data
                else None
            )
            for key, coordinator in vehicle.coordinators.items()
        },
    }
