#!/usr/bin/python3

from sqlalchemy import Column, Integer, Float, String, Text, Date, ForeignKey, BOOLEAN
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from config import CONFIG

Base = declarative_base()


class User(Base):
    __tablename__   = 'user'
    id              = Column(Integer, primary_key=True, autoincrement=True)
    username        = Column(String(64))
    password        = Column(String(64))
    email           = Column(Text)
    settings        = Column(Text) #neki json...

class Card(Base):
    __tablename__   = 'card'
    id              = Column(Integer, primary_key=True, autoincrement=True)
    title           = Column(String(1024))
    owner_id        = Column(Integer, ForeignKey("user.id"), nullable=False)
    interest_rate   = Column(Float)
    item_location   = Column(String(1024))
    last_review     = Column(Text)
    share_id        = Column(Text) #explain: 0 means not shared, otherwise increment from db and generate new cards for all relevant(tbd) users
    card_type       = Column(String(1024)) 

class Deck(Base):
    __tablename__   = 'deck'
    id = Column(Integer, primary_key=True, autoincrement=True)
    owner_id = Column(Integer, ForeignKey("user.id"), nullable=False)
    cards_by_id = Column(Text)
    number_of_cards = Column(Integer) 
    current_card = Column(Integer)
    completed = Column(BOOLEAN)

class Rating(Base):
    __tablename__ = 'rating'
    id = Column(Integer, primary_key=True, autoincrement=True)
    user_id = Column(Integer, ForeignKey("user.id"), nullable=False)
    card_id = Column(Integer, ForeignKey("card.id"), nullable=False)
    rating_value = Column(Text)                                               #Yes, Maybe, No
    rating_time = Column(Date)

engine = create_engine(CONFIG['DB_CONNECTION'])


def create_all():
    Base.metadata.create_all(Engine)

Engine = create_engine(CONFIG['DB_CONNECTION'])
Session = sessionmaker(Engine)

def get_session():
    return Session

def get_engine():
    return Engine


"""
NAVODILA TODO

Upravljanje klicev na bazo
  Base = declarative_base()
  class ImeTabele(Base):
      __tablename__ = 'imetabele'
      stolpec       = Column(Tip, druge lastnosti stolpca)

    def create_all():
      Base.metadata.create_all(Engine)

Ustvarimo engine in session objekt ter funkciji, ki ju vračata
 Engine = create_engine(CONFIG['DB_CONNECTION'])
 Session = sessionmaker(Engine)
      
 def get_session():
     return Session
 def get_engine():
     return Engine

V skripti, ki želi dostopati pa do baze pa klic postavimo v naslednji okvir:
   Session = get_session()
   with Session() as session:
      <vsebina klica>
   get_engine().dispose()
"""