# /opt/docker/dev/service_finder/backend/app/models/system.py
from datetime import datetime
from typing import Optional, Any
from sqlalchemy import String, Integer, Boolean, DateTime, text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.sql import func
from app.db.base_class import Base

class SystemParameter(Base):
    """ Dinamikus konfigurációs motor (Global -> Org -> User). """
    __tablename__ = "system_parameters"
    __table_args__ = (
        UniqueConstraint('key', 'scope_level', 'scope_id', name='uix_param_scope'),
        {"extend_existing": True}
    )

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    key: Mapped[str] = mapped_column(String, index=True)
    category: Mapped[str] = mapped_column(String, server_default="general", index=True)
    value: Mapped[dict] = mapped_column(JSONB, nullable=False)
    
    scope_level: Mapped[str] = mapped_column(String(30), server_default=text("'global'"), index=True)
    scope_id: Mapped[Optional[str]] = mapped_column(String(50)) 
    
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)
    description: Mapped[Optional[str]] = mapped_column(String)
    last_modified_by: Mapped[Optional[str]] = mapped_column(String) 
    updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), onupdate=func.now(), server_default=func.now())