"""Printer driver: a named link (SMB path or HTTP URL) to driver files."""

from shopdb.api import db


class PrinterDriver(db.Model):
    __tablename__ = 'printerdrivers'

    driverid = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(150), nullable=False)
    # SMB path (\\\\server\\share\\...) or HTTP URL to the driver package
    location = db.Column(db.String(500), nullable=False)
    description = db.Column(db.Text)
    # Exact driver name as the INF declares it: Add-PrinterDriver matches on
    # this string, not on `name`, which is ours to choose
    drivername = db.Column(db.String(255))
    # Optional: attach a driver to a whole VENDOR rather than one model. A
    # universal driver (HP UPD, Xerox GPD) serves every printer of that make, and
    # binding it to one model would mean a near-duplicate row per model.
    # Model wins over vendor when both match - see _printer_driver.
    vendorid = db.Column(db.Integer, nullable=True, index=True)

    # Optional: attach a driver to a specific printer model
    modelnumberid = db.Column(
        db.Integer,
        db.ForeignKey('models.modelnumberid'),
        nullable=True
    )
    isactive = db.Column(db.Boolean, default=True, nullable=False)

    model = db.relationship('Model')

    def to_dict(self):
        return {
            'driverid': self.driverid,
            'name': self.name,
            'location': self.location,
            'description': self.description,
            'drivername': self.drivername,
            'vendorid': self.vendorid,
            'modelnumberid': self.modelnumberid,
            'modelname': self.model.modelnumber if self.model else None,
            'isactive': bool(self.isactive),
        }
