from pydantic import Field, model_validator
from typing import Optional, List, Self

from .flowsint_base import FlowsintType
from .registry import flowsint_type


@flowsint_type
class Affiliation(FlowsintType):
    """Represents an organizational affiliation or employment relationship."""

    organization: str = Field(
        ..., description="Organization or group name", title="Organization", json_schema_extra={"primary": True}
    )
    role: Optional[str] = Field(
        None, description="Role or position within organization", title="Role"
    )
    start_date: Optional[str] = Field(
        None, description="Start date of affiliation", title="Start Date"
    )
    end_date: Optional[str] = Field(
        None, description="End date of affiliation", title="End Date"
    )
    is_current: Optional[bool] = Field(
        None, description="Whether affiliation is currently active", title="Is Current"
    )
    department: Optional[str] = Field(
        None, description="Department or division", title="Department"
    )
    location: Optional[str] = Field(
        None, description="Geographic location of affiliation", title="Location"
    )
    industry: Optional[str] = Field(
        None, description="Industry or sector", title="Industry"
    )
    description: Optional[str] = Field(
        None, description="Description of affiliation", title="Description"
    )
    source: Optional[str] = Field(
        None, description="Source of affiliation information", title="Source"
    )
    confidence: Optional[float] = Field(
        None, description="Confidence score for affiliation", title="Confidence"
    )
    associated_individuals: Optional[List[str]] = Field(
        None,
        description="Individuals with similar affiliations",
        title="Associated Individuals",
    )
    organization_type: Optional[str] = Field(
        None,
        description="Type of organization (company, NGO, government, etc.)",
        title="Organization Type",
    )
    hierarchy_level: Optional[str] = Field(
        None,
        description="Hierarchical level within organization",
        title="Hierarchy Level",
    )

    @model_validator(mode='after')
    def compute_label(self) -> Self:
        if self.role:
            self.nodeLabel = f"{self.role} at {self.organization}"
        else:
            self.nodeLabel = self.organization
        return self

    @classmethod
    def from_string(cls, line: str):
        """Parse an affiliation from a raw string."""
        return cls(organization=line.strip())

    @classmethod
    def detect(cls, line: str) -> bool:
        """Affiliation cannot be reliably detected from a single line of text."""
        return False
