"""Jira integration models — link Aegis entities to Jira issues."""

# Import enum
import enum

# Import uuid
import uuid

# Import Column, DateTime, ForeignKey, Index, String, func from sqlalchemy
from sqlalchemy import Column, DateTime, ForeignKey, Index, String, func

# Import Enum as SQLEnum from sqlalchemy
from sqlalchemy import Enum as SQLEnum

# Import JSONB, UUID from sqlalchemy.dialects.postgresql
from sqlalchemy.dialects.postgresql import JSONB, UUID

# Import relationship from sqlalchemy.orm
from sqlalchemy.orm import relationship

# Import Base from app.database
from app.database import Base


# Define class JiraLinkEntityType
class JiraLinkEntityType(str, enum.Enum):
    """Aegis entity types that can be linked to a Jira issue."""

    # Assign test = "test"
    test = "test"
    # Assign technique = "technique"
    technique = "technique"
    # Assign campaign = "campaign"
    campaign = "campaign"
    # Assign evidence = "evidence"
    evidence = "evidence"


# Define class JiraSyncDirection
class JiraSyncDirection(str, enum.Enum):
    """Direction of synchronisation between Aegis and Jira."""

    # Assign aegis_to_jira = "aegis_to_jira"
    aegis_to_jira = "aegis_to_jira"
    # Assign jira_to_aegis = "jira_to_aegis"
    jira_to_aegis = "jira_to_aegis"
    # Assign bidirectional = "bidirectional"
    bidirectional = "bidirectional"


# Define class JiraLink
class JiraLink(Base):
    """Associates an Aegis entity with a Jira issue for bidirectional sync."""

    # Assign __tablename__ = "jira_links"
    __tablename__ = "jira_links"

    # Assign id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    # Assign entity_type = Column(SQLEnum(JiraLinkEntityType), nullable=False)
    entity_type = Column(SQLEnum(JiraLinkEntityType), nullable=False)
    # Assign entity_id = Column(UUID(as_uuid=True), nullable=False)
    entity_id = Column(UUID(as_uuid=True), nullable=False)
    # Assign jira_issue_key = Column(String(50), nullable=False)
    jira_issue_key = Column(String(50), nullable=False)
    # Assign jira_issue_id = Column(String(50))
    jira_issue_id = Column(String(50))
    # Assign jira_project_key = Column(String(20))
    jira_project_key = Column(String(20))
    # Assign jira_status = Column(String(100))
    jira_status = Column(String(100))
    # Assign jira_priority = Column(String(50))
    jira_priority = Column(String(50))
    # Assign jira_assignee = Column(String(255))
    jira_assignee = Column(String(255))
    # Assign jira_story_points = Column(String(10))
    jira_story_points = Column(String(10))
    # Assign sync_direction = Column(
    sync_direction = Column(
        SQLEnum(JiraSyncDirection), default=JiraSyncDirection.bidirectional
    )
    # Assign last_synced_at = Column(DateTime)
    last_synced_at = Column(DateTime)
    # Assign sync_metadata = Column(JSONB, default={})
    sync_metadata = Column(JSONB, default={})
    # Assign created_by = Column(UUID(as_uuid=True), ForeignKey("users.id"))
    created_by = Column(UUID(as_uuid=True), ForeignKey("users.id"))
    # Assign created_at = Column(DateTime(timezone=True), server_default=func.now())
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    # Assign updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate...
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())

    # Assign creator = relationship("User", foreign_keys=[created_by])
    creator = relationship("User", foreign_keys=[created_by])

    # Assign __table_args__ = (
    __table_args__ = (
        Index("ix_jira_links_entity_id", "entity_id"),
        Index("ix_jira_links_issue_key", "jira_issue_key"),
        Index("ix_jira_links_entity_type_entity_id", "entity_type", "entity_id"),
    )
