from datetime import datetime

from app.extensions import db


class AbstractConfiguration(db.Model):
    __abstract__ = True

    id = db.Column(db.Integer, primary_key=True)
    description = db.Column(db.String(255), nullable=False)
    added = db.Column(db.DateTime(), default=datetime.utcnow, nullable=False)
    updated = db.Column(db.DateTime(), default=datetime.utcnow, nullable=False)
    destroyed = db.Column(db.DateTime(), nullable=True)

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

    @classmethod
    def csv_header(cls):
        return [
            "id", "description", "added", "updated", "destroyed"
        ]

    def csv_row(self):
        return [
            getattr(self, x) for x in self.csv_header()
        ]


class AbstractResource(db.Model):
    __abstract__ = True

    id = db.Column(db.Integer, primary_key=True)
    added = db.Column(db.DateTime(), default=datetime.utcnow, nullable=False)
    updated = db.Column(db.DateTime(), default=datetime.utcnow, nullable=False)
    deprecated = db.Column(db.DateTime(), nullable=True)
    deprecation_reason = db.Column(db.String(), nullable=True)
    destroyed = db.Column(db.DateTime(), nullable=True)

    def deprecate(self, *, reason: str):
        self.deprecated = datetime.utcnow()
        self.deprecation_reason = reason
        self.updated = datetime.utcnow()

    def destroy(self):
        if self.deprecated is None:
            self.deprecated = datetime.utcnow()
        self.destroyed = datetime.utcnow()
        self.updated = datetime.utcnow()

    @classmethod
    def csv_header(cls):
        return [
            "id", "added", "updated", "deprecated", "destroyed"
        ]

    def csv_row(self):
        return [
            getattr(self, x) for x in self.csv_header()
        ]
