from __future__ import annotations

from dataclasses import dataclass

from scraper2_hj3415.app.dart.adapters.out.dto.dart_list_item_dto import (
    DartListItemDto,
)


def _to_int(value: object, *, default: int) -> int:
    try:
        return int(value)  # pyright: ignore[reportArgumentType]
    except (TypeError, ValueError):
        return default


@dataclass(slots=True, frozen=True)
class DartListPageDto:
    status: str
    message: str
    page_no: int
    page_count: int
    total_count: int
    total_page: int
    items: list[DartListItemDto]

    @classmethod
    def from_dict(cls, data: dict[str, object]) -> "DartListPageDto":
        raw_items = data.get("list", [])
        items = (
            [DartListItemDto.from_dict(item) for item in raw_items]
            if isinstance(raw_items, list)
            else []
        )

        return cls(
            status=str(data.get("status", "")),
            message=str(data.get("message", "")),
            page_no=_to_int(data.get("page_no"), default=1),
            page_count=_to_int(data.get("page_count"), default=100),
            total_count=_to_int(data.get("total_count"), default=0),
            total_page=_to_int(data.get("total_page"), default=1),
            items=items,
        )
