from datetime import datetime

from app import db
from app.models import AbstractConfiguration


class Group(AbstractConfiguration):
    group_name = db.Column(db.String(80), unique=True, nullable=False)
    eotk = db.Column(db.Boolean())

    origins = db.relationship("Origin", back_populates="group")
    bridgeconfs = db.relationship("BridgeConf", back_populates="group")
    alarms = db.relationship("Alarm", back_populates="group")

    @classmethod
    def csv_header(cls):
        return super().csv_header() + [
            "group_name", "eotk"
        ]


class MirrorList(AbstractConfiguration):
    provider = db.Column(db.String(255), nullable=False)
    format = db.Column(db.String(20), nullable=False)
    container = db.Column(db.String(255), nullable=False)
    branch = db.Column(db.String(255), nullable=False)
    filename = db.Column(db.String(255), nullable=False)

    def destroy(self):
        self.destroyed = datetime.utcnow()
        self.updated = datetime.utcnow()
        db.session.commit()

    def url(self):
        if self.provider == "gitlab":
            return f"https://gitlab.com/{self.container}/-/raw/{self.branch}/{self.filename}"
        if self.provider == "github":
            return f"https://raw.githubusercontent.com/{self.container}/{self.branch}/{self.filename}"
        if self.provider == "s3":
            return f"s3://{self.container}/{self.filename}"

    @classmethod
    def csv_header(cls):
        return super().csv_header() + [
            "provider", "format", "container", "branch", "filename"
        ]
