from app.extensions import db
from datetime import datetime

class Project(db.Model):
    __tablename__ = 'projekte'

    id = db.Column(db.Integer, primary_key=True)
    company_id = db.Column(db.Integer, db.ForeignKey('companies.id'), nullable=False)
    contract_id = db.Column(db.Integer, db.ForeignKey('contracts.id'), nullable=True)
    sm_nr = db.Column(db.String(100), nullable=False, default='')
    bezeichnung = db.Column(db.String(300))
    baustelle = db.Column(db.String(300))
    vertrag = db.Column(db.String(200))
    abruf_nr = db.Column(db.String(100))
    lv_name = db.Column(db.String(200))
    datum_start = db.Column(db.Date)
    datum_ende = db.Column(db.Date)
    ansprechpartner_vorname = db.Column(db.String(100))
    ansprechpartner_nachname = db.Column(db.String(100))
    ansprechpartner_tel = db.Column(db.String(50))
    ansprechpartner_email = db.Column(db.String(200))
    bauabschnitt = db.Column(db.String(200))
    datum = db.Column(db.Date)
    ev_details_id = db.Column(db.String(50))
    status = db.Column(db.String(20), default='aktiv')
    erstellt_von = db.Column(db.Integer, db.ForeignKey('users.id'))
    erstellt_am = db.Column(db.DateTime, default=datetime.utcnow)
    geaendert_am = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    positionen = db.relationship('Position', backref='project', lazy='dynamic',
                                  cascade='all, delete-orphan', order_by='Position.sortierung')
    aufmass_liste = db.relationship('Aufmass', backref='aufmass_project',
                                     cascade='all, delete-orphan',
                                     order_by='Aufmass.sortierung')

    def __repr__(self):
        return f'<Project {self.sm_nr}>'
