from datetime import datetime
from typing import List

from app import db
from app.models import AbstractConfiguration, AbstractResource


class BridgeConf(AbstractConfiguration):
    group_id = db.Column(db.Integer, db.ForeignKey("group.id"), nullable=False)
    provider = db.Column(db.String(20), nullable=False)
    method = db.Column(db.String(20), nullable=False)
    description = db.Column(db.String(255))
    number = db.Column(db.Integer())

    group = db.relationship("Group", back_populates="bridgeconfs")
    bridges = db.relationship("Bridge", back_populates="conf")

    def destroy(self) -> None:
        self.destroyed = datetime.utcnow()
        self.updated = datetime.utcnow()
        for bridge in self.bridges:
            if bridge.destroyed is None:
                bridge.destroyed = datetime.utcnow()
                bridge.updated = datetime.utcnow()

    @classmethod
    def csv_header(cls) -> List[str]:
        return super().csv_header() + [
            "group_id", "provider", "method", "description", "number"
        ]


class Bridge(AbstractResource):
    conf_id = db.Column(db.Integer, db.ForeignKey("bridge_conf.id"), nullable=False)
    terraform_updated = db.Column(db.DateTime(), nullable=True)
    nickname = db.Column(db.String(255), nullable=True)
    fingerprint = db.Column(db.String(255), nullable=True)
    hashed_fingerprint = db.Column(db.String(255), nullable=True)
    bridgeline = db.Column(db.String(255), nullable=True)

    conf = db.relationship("BridgeConf", back_populates="bridges")
    alarms = db.relationship("Alarm", back_populates="bridge")

    @classmethod
    def csv_header(cls) -> List[str]:
        return super().csv_header() + [
            "conf_id", "terraform_updated", "nickname", "fingerprint", "hashed_fingerprint", "bridgeline"
        ]
