
from dataclasses import dataclass

@dataclass
class ClientListFilters:
    q: str = ""
    status: str = ""
    client_type: str = ""
    client_group_id: int | None = None
    partner_id: int | None = None
    include_archived: bool = False
    page: int = 1
    per_page: int = 25
    sort_by: str = "client_name"
    sort_order: str = "asc"

    @classmethod
    def from_params(cls, **kwargs):
        partner_id = kwargs.get("partner_id")
        if partner_id in ("", None):
            partner_id = None
        elif not isinstance(partner_id, int):
            partner_id = int(partner_id)
        client_group_id = kwargs.get("client_group_id")
        if client_group_id in ("", None): client_group_id = None
        elif not isinstance(client_group_id, int): client_group_id = int(client_group_id)
        include_archived = kwargs.get("include_archived", False)
        if isinstance(include_archived, str):
            include_archived = include_archived.lower() in ("1", "true", "yes", "on")
        return cls(
            q=kwargs.get("q", "") or "",
            status=kwargs.get("status", "") or "",
            client_type=kwargs.get("client_type", "") or "",
            client_group_id=client_group_id,
            partner_id=partner_id,
            include_archived=include_archived,
            page=max(int(kwargs.get("page", 1) or 1), 1),
            per_page=min(max(int(kwargs.get("per_page", 25) or 25), 1), 100),
            sort_by=kwargs.get("sort_by", "client_name") or "client_name",
            sort_order=kwargs.get("sort_order", "asc") or "asc",
        )
