from application.model import Historical
from memory import S3Shaped
from application import db
from application import app
import json


class Storage(object):

    def get(self, key, version=None):
        query = db.session.query(Historical).filter(
                Historical.version == version,
                Historical.key == key).first()
        if query:
            app.logger.debug(query.value)
            return S3Shaped(query.key, query.value, query.version)
        else:
            app.logger.debug('version not found')
            empty_result = Historical()
            empty_result.key = None
            empty_result.value = None
            empty_result.version = None
            return S3Shaped(empty_result.key, empty_result.value, empty_result.version)

    def post(self, key, data):
        app.logger.debug('database storing ' + json.dumps(data))
        try:
            version_row = Historical()
            version_row.key = key
            version_row.value = json.dumps(data)
            self.get_version_number(key)
            version_row.version = self.get_version_number(key)
            db.session.add(version_row)
            db.session.commit()
        except Exception as e:
            app.logger.error(e.message)

    def list_versions(self, key):
        results_list = []
        all_key_versions = db.session.query(Historical).filter(Historical.key == key)
        for historical_instance in all_key_versions:
            results_list.append(
                S3Shaped(historical_instance.key, historical_instance.value, historical_instance.version))
        return results_list
    
    def count(self):
        return Historical.query.count()


    def get_version_number(self, key):
        query = db.session.query(Historical).filter(Historical.key == key)
        next_version_number = query.count() + 1
        return next_version_number


    def health(self):
        try:
            self.count()
            return True, "DB"
        except:
            return False, "DB"


@@ -34,16 +34,44 @@
 torical%0A
+from sqlalchemy import desc%0A
 from mem
@@ -221,16 +221,41 @@
 =None):%0A
+%0A        if version:%0A    
         
@@ -389,24 +389,203 @@
 ey).first()%0A
+        else:%0A            query = db.session.query(Historical).filter(%0A                Historical.key == key).order_by(desc(%0A                        Historical.version)).first()%0A%0A
         if q

return latest version in absence of version