from __future__ import annotations

from datetime import datetime
from typing import Any

from pydantic import BaseModel, Field


class LinearLabel(BaseModel):
    id: str = ""
    name: str = ""


class LinearLabels(BaseModel):
    nodes: list[LinearLabel] = Field(default_factory=list)


class LinearIssue(BaseModel):
    id: str
    title: str
    description: str | None = None
    url: str = ""
    labels: LinearLabels = Field(default_factory=LinearLabels)

    @property
    def label_names(self) -> list[str]:
        return [lbl.name for lbl in self.labels.nodes]


class LinearWebhookPayload(BaseModel):
    action: str
    """'create', 'update', 'remove'"""

    type: str
    """'Issue', 'Comment', etc."""

    data: LinearIssue | dict[str, Any]
    organization_id: str = Field(default="", alias="organizationId")
    webhook_id: str = Field(default="", alias="webhookId")

    model_config = {"populate_by_name": True}

    @property
    def issue(self) -> LinearIssue | None:
        if self.type == "Issue" and isinstance(self.data, LinearIssue):
            return self.data
        return None
