import os
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Column, String, Integer, Numeric, create_engine
from flask_migrate import Migrate

database_name = 'fiz'
database_path = os.environ.get('DATABASE_URL', "postgres://{}/{}".format("localhost:5432", database_name))

db = SQLAlchemy()


def setup_db(app, database_path=database_path):
    '''
    binds a flask app and a SQLAlchemy service
    '''
    app.config['SQLALCHEMY_DATABASE_URI'] = database_path
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    db.app = app
    db.init_app(app)
    migrate = Migrate(app, db)

class Item(db.Model):
    __tablename__ = 'items'

    id = db.Column(db.Integer, primary_key=True)
    description = db.Column(db.String(120))
    amount = db.Column(db.Numeric(scale=2), nullable=False)
    date = db.Column(db.Date, nullable=False)
    expense = db.Column(db.Boolean, nullable=False)
    category_id = db.Column(db.Integer, db.ForeignKey(
        'categories.id'), nullable=False)

    def insert(self):
        db.session.add(self)
        db.session.commit()

    def update(self):
        db.session.commit()

    def delete(self):
        db.session.delete(self)
        db.session.commit()

    def format(self):
        return {
            'id': self.id,
            'description': self.description,
            'amount': str(self.amount),
            'date': self.date,
            'expense': self.expense,
            'category_id': self.category_id
        }


class Category(db.Model):
    __tablename__ = 'categories'

    id = db.Column(db.Integer, primary_key=True)
    type = db.Column(db.String(50))
    items = db.relationship('Item', backref='category', lazy='dynamic')

    def insert(self):
        db.session.add(self)
        db.session.commit()

    def update(self):
        db.session.commit()

    def delete(self):
        db.session.delete(self)
        db.session.commit()

    def format(self):
        return {
            'id': self.id,
            'type': self.type,
            'items': self.items
        }
