from app.extensions import db
from datetime import datetime

class Position(db.Model):
    __tablename__ = 'positionen'

    id = db.Column(db.Integer, primary_key=True)
    project_id = db.Column(db.Integer, db.ForeignKey('projekte.id'), nullable=False)
    aufmass_id = db.Column(db.Integer, db.ForeignKey('aufmass.id'), nullable=True)
    lv_position_id = db.Column(db.Integer, db.ForeignKey('lv_positionen.id'), nullable=True)
    pos_nr = db.Column(db.String(50), nullable=False)
    sortierung = db.Column(db.Integer, default=0)
    rsa = db.Column(db.String(20))
    abschnitt = db.Column(db.String(100))
    kurztext = db.Column(db.String(300))
    langtext = db.Column(db.Text)
    einheit = db.Column(db.String(10), default='ST')
    einzelpreis = db.Column(db.Float, default=0.0)
    menge = db.Column(db.Float, default=0.0)
    gesamtpreis = db.Column(db.Float, default=0.0)
    faktor = db.Column(db.Float, default=1.0)
    laenge = db.Column(db.Float, default=0.0)
    breite = db.Column(db.Float, default=0.0)
    tiefe = db.Column(db.Float, default=0.0)
    formel_typ = db.Column(db.String(10), default='standard')
    formel = db.Column(db.String(300))
    bemerkung = db.Column(db.Text)
    menge_hinten = db.Column(db.Float, default=0.0)
    erstellt_am = db.Column(db.DateTime, default=datetime.utcnow)

    def berechne_menge(self, recalc_hinten=True, skip_menge_recalc=False):
        if not skip_menge_recalc:
            if self.formel_typ == 'frei':
                if self.formel:
                    from app.services.formel_rechner import berechne_formel
                    try:
                        self.menge = berechne_formel(self.formel)
                    except Exception:
                        self.menge = 0
                else:
                    self.menge = 0
            elif self.einheit == 'ST':
                self.menge = self.faktor * 1
            elif self.einheit == 'M':
                self.menge = self.laenge
            elif self.einheit == 'M2':
                self.menge = self.laenge * self.breite
            elif self.einheit == 'M3':
                self.menge = self.laenge * self.breite * self.tiefe
            else:
                self.menge = self.laenge
        if recalc_hinten:
            self.menge_hinten = self.faktor * self.menge
        self.gesamtpreis = self.menge_hinten * self.einzelpreis
        return self.menge

    def to_dict(self):
        return {
            'id': self.id,
            'pos_nr': self.pos_nr,
            'sortierung': self.sortierung,
            'rsa': self.rsa,
            'kurztext': self.kurztext,
            'langtext': self.langtext,
            'einheit': self.einheit,
            'einzelpreis': self.einzelpreis,
            'menge': self.menge,
            'gesamtpreis': self.gesamtpreis,
            'faktor': self.faktor,
            'laenge': self.laenge,
            'breite': self.breite,
            'tiefe': self.tiefe,
            'bemerkung': self.bemerkung,
            'menge_hinten': self.menge_hinten,
        }

    def __repr__(self):
        return f'<Position {self.pos_nr} @ {self.project_id}>'
