from datetime import datetime
from typing import Optional
import uuid
from sqlalchemy import JSON, Column, DateTime, ForeignKey, String
from sqlmodel import Field, SQLModel
from utils.datetime_utils import get_current_utc_datetime


class AuditLogModel(SQLModel, table=True):
    __tablename__ = "audit_logs"

    id: uuid.UUID = Field(primary_key=True, default_factory=uuid.uuid4)
    user_id: Optional[uuid.UUID] = Field(
        sa_column=Column(ForeignKey("users.id"), nullable=True), default=None
    )
    action: str
    resource_type: str
    resource_id: Optional[uuid.UUID] = Field(default=None)
    client_id: Optional[uuid.UUID] = Field(default=None)
    details: Optional[dict] = Field(sa_column=Column(JSON), default=None)
    ip_address: Optional[str] = Field(
        sa_column=Column(String, nullable=True), default=None
    )
    created_at: datetime = Field(
        sa_column=Column(
            DateTime(timezone=True),
            nullable=False,
            default=get_current_utc_datetime,
            index=True,
        ),
    )
