from datetime import datetime
from typing import List

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")
    eotks = db.relationship("Eotk", back_populates="group")
    onions = db.relationship("Onion", back_populates="group")
    alarms = db.relationship("Alarm", back_populates="group")

    @classmethod
    def csv_header(cls) -> List[str]:
        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)
    # obfuscate = db.Column(db.Boolean(), nullable=False)
    container = db.Column(db.String(255), nullable=False)
    branch = db.Column(db.String(255), nullable=False)
    role = db.Column(db.String(255), nullable=True)
    filename = db.Column(db.String(255), nullable=False)

    providers_supported = {
        "github": "GitHub",
        "gitlab": "GitLab",
        "s3": "AWS S3",
    }

    formats_supported = {
        "bc2": "Bypass Censorship v2",
        "bc3": "Bypass Censorship v3",
        "bca": "Bypass Censorship Analytics",
        "bridgelines": "Tor Bridge Lines"
    }

    def destroy(self) -> None:
        self.destroyed = datetime.utcnow()
        self.updated = datetime.utcnow()

    def url(self) -> str:
        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}"
        return "Unknown provider"

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