from __future__ import annotations

from datetime import datetime
from typing import Optional

from sqlalchemy import Integer, String, Text, DateTime, func, JSON, text
from sqlalchemy.orm import Mapped, mapped_column, relationship

from shared.db.base import Base


class PageConfig(Base):
    __tablename__ = "page_configs"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)

    container_type: Mapped[str] = mapped_column(
        String(32), nullable=False, server_default=text("'page'"),
    )
    container_id: Mapped[int] = mapped_column(Integer, nullable=False)

    features: Mapped[dict] = mapped_column(
        JSON, nullable=False, server_default="{}"
    )

    # Per-page SumUp credentials (NULL until configured)
    sumup_merchant_code: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
    sumup_api_key: Mapped[Optional[str]] = mapped_column(Text(), nullable=True)
    sumup_checkout_prefix: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)

    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), nullable=False, server_default=func.now()
    )
    updated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), nullable=False, server_default=func.now()
    )
    deleted_at: Mapped[Optional[datetime]] = mapped_column(
        DateTime(timezone=True), nullable=True
    )
