from datetime import datetime, timezone

from ..extensions import db


def _utcnow():
    return datetime.now(timezone.utc)


class FeaturedProduct(db.Model):
    """Productos destacados manualmente en la home (sección TechConsole).

    El admin elige hasta 12 productos y su orden mediante `position`.
    Si la tabla está vacía, la home cae a un fallback alfabético.
    """
    __tablename__ = "featured_products"

    id = db.Column(db.Integer, primary_key=True)
    product_id = db.Column(
        db.Integer,
        db.ForeignKey("products.id", ondelete="CASCADE"),
        nullable=False,
        index=True,
    )
    position = db.Column(db.Integer, nullable=False, unique=True)
    created_at = db.Column(db.DateTime, nullable=False, default=_utcnow)

    product = db.relationship("Product")

    def __repr__(self):
        return f"<FeaturedProduct pos={self.position} product_id={self.product_id}>"
