"""Business Unit model."""

from shopdb.extensions import db
from .base import BaseModel


class BusinessUnit(BaseModel):
    """Business unit / department model."""
    __tablename__ = 'businessunits'

    businessunitid = db.Column(db.Integer, primary_key=True)
    businessunit = db.Column(db.String(100), unique=True, nullable=False)
    code = db.Column(db.String(20), unique=True, comment='Short code')
    description = db.Column(db.Text)

    # Optional parent for hierarchy
    parentid = db.Column(
        db.Integer,
        db.ForeignKey('businessunits.businessunitid'),
        nullable=True
    )

    # Self-referential relationship for hierarchy
    parent = db.relationship(
        'BusinessUnit',
        remote_side=[businessunitid],
        backref='children'
    )

    def __repr__(self):
        return f"<BusinessUnit {self.businessunit}>"
