"""Planka webhook payload data models."""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any


@dataclass
class PlankaUser:
    """Planka user from webhook payload."""

    id: str
    username: str
    name: str = ""
    email: str = ""

    @classmethod
    def from_payload(cls, data: dict[str, Any]) -> PlankaUser:
        return cls(
            id=str(data.get("id", "")),
            username=data.get("username", ""),
            name=data.get("name", ""),
            email=data.get("email", ""),
        )


@dataclass
class PlankaBoard:
    """Planka board from webhook payload."""

    id: str
    name: str
    position: int = 0

    @classmethod
    def from_payload(cls, data: dict[str, Any]) -> PlankaBoard:
        return cls(
            id=str(data.get("id", "")),
            name=data.get("name", ""),
            position=data.get("position", 0),
        )


@dataclass
class PlankaList:
    """Planka list from webhook payload."""

    id: str
    name: str
    position: int = 0
    board_id: str = ""

    @classmethod
    def from_payload(cls, data: dict[str, Any]) -> PlankaList:
        return cls(
            id=str(data.get("id", "")),
            name=data.get("name", ""),
            position=data.get("position", 0),
            board_id=str(data.get("boardId", "")),
        )


@dataclass
class PlankaCard:
    """Planka card from webhook payload."""

    id: str
    name: str
    description: str = ""
    position: int = 0
    board_id: str = ""
    list_id: str = ""
    due_date: str = ""
    is_completed: bool = False

    @classmethod
    def from_payload(cls, data: dict[str, Any]) -> PlankaCard:
        return cls(
            id=str(data.get("id", "")),
            name=data.get("name", ""),
            description=data.get("description", "") or "",
            position=data.get("position", 0),
            board_id=str(data.get("boardId", "")),
            list_id=str(data.get("listId", "")),
            due_date=data.get("dueDate", "") or "",
            is_completed=data.get("isCompleted", False),
        )


@dataclass
class PlankaComment:
    """Planka comment (action) from webhook payload."""

    id: str
    text: str
    card_id: str = ""
    user_id: str = ""

    @classmethod
    def from_payload(cls, data: dict[str, Any]) -> PlankaComment:
        return cls(
            id=str(data.get("id", "")),
            text=data.get("text", data.get("data", {}).get("text", "")),
            card_id=str(data.get("cardId", "")),
            user_id=str(data.get("userId", "")),
        )


@dataclass
class PlankaTask:
    """Planka task (checklist item) from webhook payload."""

    id: str
    name: str
    is_completed: bool = False
    card_id: str = ""

    @classmethod
    def from_payload(cls, data: dict[str, Any]) -> PlankaTask:
        return cls(
            id=str(data.get("id", "")),
            name=data.get("name", ""),
            is_completed=data.get("isCompleted", False),
            card_id=str(data.get("cardId", "")),
        )


@dataclass
class PlankaAttachment:
    """Planka attachment from webhook payload."""

    id: str
    name: str
    card_id: str = ""
    url: str = ""

    @classmethod
    def from_payload(cls, data: dict[str, Any]) -> PlankaAttachment:
        return cls(
            id=str(data.get("id", "")),
            name=data.get("name", data.get("filename", "")),
            card_id=str(data.get("cardId", "")),
            url=data.get("url", ""),
        )


@dataclass
class PlankaLabel:
    """Planka label from webhook payload."""

    id: str
    name: str
    color: str = ""
    board_id: str = ""

    @classmethod
    def from_payload(cls, data: dict[str, Any]) -> PlankaLabel:
        return cls(
            id=str(data.get("id", "")),
            name=data.get("name", ""),
            color=data.get("color", ""),
            board_id=str(data.get("boardId", "")),
        )
