"""Admin-configurable Premium settings (Premium Management v2).

These three small tables move Premium *business configuration* out of source code
and templates into the Admin Panel — payment details, plans and features. They
hold configuration only; the premium activation/approval workflow is unchanged
and still lives in :mod:`app.premium.services`. Access is always through
:mod:`app.premium.config` (which caches per request and seeds sensible defaults),
so nothing hardcodes payment info, prices or feature copy.
"""

from __future__ import annotations

from app.extensions import db
from app.models.base import BaseModel, BigIntegerType, utcnow
from app.models.mixins import TimestampMixin


class PremiumPaymentSettings(BaseModel, TimestampMixin):
    """The single active payment configuration (one row).

    Which methods are offered is controlled by the ``enable_*`` flags; the UPI /
    bank / cash details are what the request page renders. Managed exclusively via
    :func:`app.premium.config.get_payment_settings` / ``update_payment_settings``.
    """

    __tablename__ = "premium_payment_settings"

    active_upi_id = db.Column(db.String(120), nullable=True)
    bank_name = db.Column(db.String(120), nullable=True)
    account_holder = db.Column(db.String(120), nullable=True)
    account_number = db.Column(db.String(40), nullable=True)
    ifsc = db.Column(db.String(20), nullable=True)
    cash_instructions = db.Column(db.Text, nullable=True)
    # Public URL of an admin-uploaded UPI QR image. NULL falls back to the QR
    # generated on the fly from ``active_upi_id`` (unchanged legacy behaviour).
    qr_image_path = db.Column(db.String(255), nullable=True)
    enable_upi = db.Column(db.Boolean, nullable=False, default=True, server_default="1")
    enable_bank = db.Column(db.Boolean, nullable=False, default=True, server_default="1")
    enable_cash = db.Column(db.Boolean, nullable=False, default=True, server_default="1")
    support_email = db.Column(db.String(120), nullable=True)
    support_phone = db.Column(db.String(40), nullable=True)

    def __repr__(self) -> str:
        return f"<PremiumPaymentSettings id={self.id} upi={self.active_upi_id!r}>"


class PremiumPlan(BaseModel, TimestampMixin):
    """A purchasable Premium plan (display-only pricing; no payments processed).

    ``name`` is both the label and the identifier stored on a request/subscription
    (matched case-insensitively, so legacy 'monthly'/'yearly' keep working).
    ``duration_days`` drives the subscription end date on approval.
    """

    __tablename__ = "premium_plans"
    __table_args__ = (db.Index("ix_premium_plans_sort_order", "sort_order"),)

    name = db.Column(db.String(50), nullable=False, unique=True)
    duration_days = db.Column(db.Integer, nullable=False, default=30)
    price = db.Column(db.Numeric(10, 2), nullable=False, default=0)
    description = db.Column(db.String(300), nullable=True)
    enabled = db.Column(db.Boolean, nullable=False, default=True, server_default="1")
    sort_order = db.Column(db.Integer, nullable=False, default=0, server_default="0")

    def __repr__(self) -> str:
        return f"<PremiumPlan {self.name!r} {self.duration_days}d enabled={self.enabled}>"


class PremiumFeature(BaseModel, TimestampMixin):
    """One Premium benefit rendered on the Premium landing page."""

    __tablename__ = "premium_features"
    __table_args__ = (db.Index("ix_premium_features_display_order", "display_order"),)

    title = db.Column(db.String(80), nullable=False)
    description = db.Column(db.String(300), nullable=True)
    icon = db.Column(db.String(60), nullable=True)  # a Bootstrap-Icons class
    enabled = db.Column(db.Boolean, nullable=False, default=True, server_default="1")
    display_order = db.Column(db.Integer, nullable=False, default=0, server_default="0")

    def __repr__(self) -> str:
        return f"<PremiumFeature {self.title!r} enabled={self.enabled}>"
