"""Model (vendor catalog model number) model."""

from shopdb.extensions import db
from .base import BaseModel


class Model(BaseModel):
    """Vendor catalog model information (machines, PCs, printers, network)."""
    __tablename__ = 'models'

    modelnumberid = db.Column(db.Integer, primary_key=True)
    modelnumber = db.Column(db.String(100), nullable=False)

    # Link to model type (what kind of thing this catalog model is for)
    modeltypeid = db.Column(
        db.Integer,
        db.ForeignKey('modeltypes.modeltypeid'),
        nullable=True
    )

    # Link to vendor/manufacturer
    vendorid = db.Column(
        db.Integer,
        db.ForeignKey('vendors.vendorid'),
        nullable=True
    )

    description = db.Column(db.Text)
    imageurl = db.Column(db.String(500), comment='URL to product image')
    documentationurl = db.Column(db.String(500), comment='URL to documentation')
    notes = db.Column(db.Text)

    # Relationships
    modeltype = db.relationship('ModelType', backref='models')
    vendor = db.relationship('Vendor', backref='models')

    # Unique constraint on modelnumber + vendor
    __table_args__ = (
        db.UniqueConstraint('modelnumber', 'vendorid', name='uq_model_vendor'),
    )

    def __repr__(self):
        return f"<Model {self.modelnumber}>"
