import time
from datetime import datetime

from sqlalchemy import Column, Integer, String, DateTime, Boolean

from core import db


class Model(db.Base):
    __abstract__ = True

    id = Column(Integer, primary_key=True)
    created_on = Column(DateTime, default=datetime.now())
    deleted = Column(Boolean, nullable=False, default=False)

    def __repr__(self):
        return f"<{self.__class__.__name__} {self.id}>"


class MusicFolder(Model):
    __tablename__ = 'MusicFolders'

    STATUS_UNKNOWN = 0
    STATUS_FOUND = 1
    STATUS_UNAVAILABLE = 2
    STATUS_DELETED = 3

    path = Column(String, nullable=False)
    last_scan = Column(DateTime)
    status = Column(Integer, default=0)


class Tag(Model):
    __tablename__ = 'Tags'

    label = Column(String, nullable=False)
    color = Column(String, nullable=False, default="#6666ff")


class Track(Model):
    __tablename__ = 'Tracks'

    STATUS_UNKNOWN = 0
    STATUS_FOUND = 1
    STATUS_UNAVAILABLE = 2
    STATUS_UNREADABLE = 3

    title = Column(String)
    format = Column(String)
    artist = Column(String)
    album = Column(String)
    track_num = Column(Integer)
    duration = Column(Integer)
    note = Column(String)
    status = Column(Integer, nullable=False, default=0)
    path = Column(String, nullable=False)
    hash = Column(String, nullable=False)

    def literal_duration(self):
        return time.strftime("%M:%S", time.gmtime(self.duration))


class TrackTag(Model):
    __tablename__ = 'TracksTags'

    track_id = Column(Integer)
    tag_id = Column(Integer)


class SessionFolder(Model):
    __tablename__ = 'SessionFolders'

    name = Column(String, nullable=False)


class Session(Model):
    __tablename__ = 'Sessions'

    name = Column(String, nullable=False, default="Ma session")
    date = Column(DateTime, default=datetime.now())
    notes = Column(String)
    folder_id = Column(Integer)


class SessionTrack(Model):
    __tablename__ = 'SessionsTracks'

    track_id = Column(Integer)
    session_id = Column(Integer)
    order = Column(Integer)


if __name__ == "__main__":
    from core import constants

    session = db.Session()
    for track in session.query(Track).all():
        print(track)
