from sqlalchemy import Integer, String, ForeignKey, DateTime, Text, JSON
from sqlalchemy.orm import Mapped, relationship, mapped_column
from sqlalchemy.sql import func
from typing import TYPE_CHECKING, Optional

from app.database.database import Base

if TYPE_CHECKING:
    from app.models.staff.staff_model import Staff
    from app.models.staff.store_model import Chain


class ActivityLog(Base):
    __tablename__ = "activity_logs"
    
    log_id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
    chain_id: Mapped[int] = mapped_column(ForeignKey("chains.chain_id"), nullable=False, index=True)
    staff_id: Mapped[int] = mapped_column(ForeignKey("staff.id"), nullable=False)
    action_type: Mapped[str] = mapped_column(String(100), nullable=False, index=True)  # e.g., "CREATE_LISTING", "UPDATE_LISTING", "DELETE_LISTING", "APPROVE_LISTING"
    entity_type: Mapped[str] = mapped_column(String(50), nullable=False, index=True)  # e.g., "LISTING", "PRODUCT", "CATEGORY"
    entity_id: Mapped[Optional[int]] = mapped_column(Integer, nullable=True, index=True)  # ID of the affected entity
    description: Mapped[str] = mapped_column(Text, nullable=False)  # Human-readable description
    action_metadata: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True, name="metadata")  # Additional data like old values, new values, etc. (stored as 'metadata' in DB)
    created_at: Mapped[DateTime] = mapped_column(DateTime(timezone=True), server_default=func.now(), index=True)
    
    # Relationships
    staff: Mapped["Staff"] = relationship("Staff")
    chain: Mapped["Chain"] = relationship("Chain")

