"""app_levels — the named access levels and what each one may do.

Levels are data, not an enum: the owner can add or edit them without a code
change. The seeded set (admin / lead / reviewer) is created by migration
0003; `rian` is the owner and is not a row here at all — the kit synthesizes
the super admin.
"""

from sqlalchemy import JSON, String
from sqlalchemy.orm import Mapped, mapped_column

from app.models.base import Base


class Level(Base):
    __tablename__ = "app_levels"

    name: Mapped[str] = mapped_column(String(40), primary_key=True)

    # What a holder of this level may do. Strings from the kit's standard
    # vocabulary plus Scout's own `scout.*` permissions.
    permissions: Mapped[list] = mapped_column(JSON, default=list)

    # Which levels a holder may hand out. This is what stops an admin from
    # minting another admin — `admin` does not list itself.
    assignable: Mapped[list] = mapped_column(JSON, default=list)

    def as_dict(self) -> dict:
        return {
            "name": self.name,
            "permissions": list(self.permissions or []),
            "assignable": list(self.assignable or []),
        }
