from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Text, Enum as SQLEnum, JSON
from sqlalchemy.orm import relationship
from database import Base
from datetime import datetime
import enum


class ActivityType(str, enum.Enum):
    TASK_CREATED = "task_created"
    TASK_UPDATED = "task_updated"
    TASK_ASSIGNED = "task_assigned"
    TASK_STATUS_CHANGED = "task_status_changed"
    SUBMISSION_CREATED = "submission_created"
    SUBMISSION_REVIEWED = "submission_reviewed"
    COMMENT_ADDED = "comment_added"
    ASSET_CREATED = "asset_created"
    ASSET_UPDATED = "asset_updated"
    SHOT_CREATED = "shot_created"
    SHOT_UPDATED = "shot_updated"
    PROJECT_CREATED = "project_created"
    PROJECT_UPDATED = "project_updated"
    USER_JOINED_PROJECT = "user_joined_project"
    SHOT_DELETED = "shot_deleted"
    ASSET_DELETED = "asset_deleted"
    SHOT_RECOVERED = "shot_recovered"
    ASSET_RECOVERED = "asset_recovered"


class Activity(Base):
    __tablename__ = "activities"

    id = Column(Integer, primary_key=True, index=True)
    type = Column(SQLEnum(ActivityType), nullable=False, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, index=True)
    
    # Optional references to related entities
    project_id = Column(Integer, ForeignKey("projects.id"), nullable=True, index=True)
    task_id = Column(Integer, ForeignKey("tasks.id"), nullable=True)
    asset_id = Column(Integer, ForeignKey("assets.id"), nullable=True)
    shot_id = Column(Integer, ForeignKey("shots.id"), nullable=True)
    submission_id = Column(Integer, ForeignKey("submissions.id"), nullable=True)
    
    # Activity details
    description = Column(Text, nullable=False)
    activity_metadata = Column(JSON, nullable=True)  # Additional context data
    
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False, index=True)

    # Relationships
    user = relationship("User", foreign_keys=[user_id])
    project = relationship("Project", foreign_keys=[project_id])
    task = relationship("Task", foreign_keys=[task_id])
    asset = relationship("Asset", foreign_keys=[asset_id])
    shot = relationship("Shot", foreign_keys=[shot_id])
    submission = relationship("Submission", foreign_keys=[submission_id])
