"""Gitea webhook payload data models."""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any


@dataclass
class GiteaUser:
    """Gitea user from webhook payload."""

    id: int
    login: str
    full_name: str = ""
    email: str = ""
    avatar_url: str = ""

    @classmethod
    def from_payload(cls, data: dict[str, Any]) -> GiteaUser:
        return cls(
            id=data.get("id", 0),
            login=data.get("login", data.get("username", "")),
            full_name=data.get("full_name", ""),
            email=data.get("email", ""),
            avatar_url=data.get("avatar_url", ""),
        )


@dataclass
class GiteaRepository:
    """Gitea repository from webhook payload."""

    id: int
    name: str
    full_name: str
    html_url: str
    description: str = ""
    private: bool = False
    owner: GiteaUser | None = None

    @classmethod
    def from_payload(cls, data: dict[str, Any]) -> GiteaRepository:
        owner = None
        if data.get("owner"):
            owner = GiteaUser.from_payload(data["owner"])
        return cls(
            id=data.get("id", 0),
            name=data.get("name", ""),
            full_name=data.get("full_name", ""),
            html_url=data.get("html_url", ""),
            description=data.get("description", ""),
            private=data.get("private", False),
            owner=owner,
        )


@dataclass
class GiteaCommit:
    """Gitea commit from push payload."""

    id: str
    message: str
    url: str
    author_name: str = ""
    author_email: str = ""
    timestamp: str = ""

    @classmethod
    def from_payload(cls, data: dict[str, Any]) -> GiteaCommit:
        author = data.get("author", {})
        return cls(
            id=data.get("id", ""),
            message=data.get("message", "").strip(),
            url=data.get("url", ""),
            author_name=author.get("name", ""),
            author_email=author.get("email", ""),
            timestamp=data.get("timestamp", ""),
        )


@dataclass
class GiteaIssue:
    """Gitea issue from webhook payload."""

    id: int
    number: int
    title: str
    html_url: str
    state: str = ""
    body: str = ""
    labels: list[str] = field(default_factory=list)

    @classmethod
    def from_payload(cls, data: dict[str, Any]) -> GiteaIssue:
        labels = [lbl.get("name", "") for lbl in data.get("labels", []) if lbl.get("name")]
        return cls(
            id=data.get("id", 0),
            number=data.get("number", 0),
            title=data.get("title", ""),
            html_url=data.get("html_url", ""),
            state=data.get("state", ""),
            body=data.get("body", ""),
            labels=labels,
        )


@dataclass
class GiteaPullRequest:
    """Gitea pull request from webhook payload."""

    id: int
    number: int
    title: str
    html_url: str
    state: str = ""
    body: str = ""
    merged: bool = False
    base_branch: str = ""
    head_branch: str = ""
    labels: list[str] = field(default_factory=list)

    @classmethod
    def from_payload(cls, data: dict[str, Any]) -> GiteaPullRequest:
        labels = [lbl.get("name", "") for lbl in data.get("labels", []) if lbl.get("name")]
        base = data.get("base", {})
        head = data.get("head", {})
        return cls(
            id=data.get("id", 0),
            number=data.get("number", 0),
            title=data.get("title", ""),
            html_url=data.get("html_url", ""),
            state=data.get("state", ""),
            body=data.get("body", ""),
            merged=data.get("merged", False),
            base_branch=base.get("label", base.get("ref", "")),
            head_branch=head.get("label", head.get("ref", "")),
            labels=labels,
        )


@dataclass
class GiteaRelease:
    """Gitea release from webhook payload."""

    id: int
    tag_name: str
    name: str
    html_url: str
    body: str = ""
    draft: bool = False
    prerelease: bool = False

    @classmethod
    def from_payload(cls, data: dict[str, Any]) -> GiteaRelease:
        return cls(
            id=data.get("id", 0),
            tag_name=data.get("tag_name", ""),
            name=data.get("name", ""),
            html_url=data.get("html_url", ""),
            body=data.get("body", ""),
            draft=data.get("draft", False),
            prerelease=data.get("prerelease", False),
        )


@dataclass
class GiteaComment:
    """Gitea comment from webhook payload."""

    id: int
    body: str
    html_url: str
    user: GiteaUser | None = None

    @classmethod
    def from_payload(cls, data: dict[str, Any]) -> GiteaComment:
        user = None
        if data.get("user"):
            user = GiteaUser.from_payload(data["user"])
        return cls(
            id=data.get("id", 0),
            body=data.get("body", ""),
            html_url=data.get("html_url", ""),
            user=user,
        )
