from dataclasses import dataclass

import httpx

from .base import AbuseResponse, json_data
from .models import CheckBlockIP


@dataclass(frozen=True, slots=True, kw_only=True)
class CheckBlockResponse(AbuseResponse):
    network_address: str
    netmask: str
    min_address: str
    max_address: str
    num_possible_hosts: int
    address_space_description: str
    reported_addresses: tuple[CheckBlockIP, ...]

    @classmethod
    def from_response(cls, response: httpx.Response) -> "CheckBlockResponse":
        data = json_data(response)
        return cls(
            **cls.metadata(response),
            network_address=data["networkAddress"],
            netmask=data["netmask"],
            min_address=data["minAddress"],
            max_address=data["maxAddress"],
            num_possible_hosts=data["numPossibleHosts"],
            address_space_description=data["addressSpaceDesc"],
            reported_addresses=tuple(
                CheckBlockIP.from_dict(item) for item in data.get("reportedAddress", [])
            ),
        )
