# main.py
from flask import Flask, render_template, request, redirect, url_for, Response, jsonify
from calc import calculate_savings_depletion
from db import init_db, get_finance_data, update_finance, get_budget_data, insert_cset, get_comp_set_data, get_comp_set_options
from collections import defaultdict
from datetime import datetime
import csv
import io
from disp import FP_VAR

app = Flask(__name__)

# core vars that we will save for comparisons
VARS= ( 'D_Salary', 'D_Num_fortnights_pay', 'School_Fees', 'Car_loan_via_pay', 'Car_loan', 'Car_balloon', 'Living_Expenses', 'Savings', 'Interest_Rate', 'Inflation', 'Mich_present', 'Overseas_trip', 'Mark_reno', 'D_leave_owed_in_days', 'D_TLS_shares', 'M_TLS_shares', 'D_CBA_shares', 'TLS_price', 'CBA_price', 'Overseas_trip_date', 'Mark_reno_date', 'Sell_shares', 'CBA', 'TLS' )

# Initialize the database
init_db()

@app.route('/')
def index():
    finance_data = get_finance_data()
    get_comp_set_options(finance_data)
    depletion_date, savings_per_fortnight, final_savings = calculate_savings_depletion(finance_data)
    BUDGET=get_budget_data(finance_data)

    if depletion_date:
        depletion_date=depletion_date.date(); # just show date

    # if we are comparing...(compare_to will be 0 / None to start with, and then COMP will be None
    COMP=get_comp_set_data(finance_data['compare_to'])

    DISP=[]
    # Row 1
    r=[]
    r.append( FP_VAR( 'D Salary', 'D_Salary' ) )
    r.append( FP_VAR( 'Savings', 'Savings' ) )
    r.append( FP_VAR( 'Car Loan via Pay', 'Car_loan_via_pay', 'readonly' ) )
    r.append( FP_VAR( 'Living Expenses', 'Living_Expenses' ) )
    r.append( FP_VAR( 'Overseas Trip', 'Overseas_trip', 'date', 'col-auto', 'Overseas_trip_date' ) )
    DISP.append(r)

    # Row 2
    r=[]
    r.append( FP_VAR( 'D # Fortnights pay', 'D_Num_fortnights_pay' ) )
    r.append( FP_VAR( 'Interest Rate', 'Interest_Rate' ) )
    r.append( FP_VAR( 'Car Loan (monthly)', 'Car_loan', 'readonly' ) )
    r.append( FP_VAR( 'Inflation', 'Inflation' ) )
    r.append( FP_VAR( 'Reno Costs', 'Mark_reno', 'date', 'col-auto', 'Mark_reno_date' ) )
    DISP.append(r)

    # Row 2
    r=[]
    r.append( FP_VAR( 'D # days leave', 'D_leave_owed_in_days' ) )
    r.append( FP_VAR( 'M TLS amount', 'M_TLS_shares' ) )
    r.append( FP_VAR( 'Car Balloon', 'Car_balloon', 'readonly' ) )
    ss_opt = [ { 'val': 0, 'label': 'Never' } ]
    for el in range(1,7):
        ss_opt.append( { 'val': el, 'label': f'{el} years' } )

    r.append( FP_VAR( 'Sell Shares for:', 'Sell_shares', 'select', 'col-auto', '', ss_opt ) )
    r.append( FP_VAR( 'Car Buyout', 'Car_buyout', 'date', 'col-auto', 'Car_buyout_date' ) )
    DISP.append(r)

    # Row 3
    r=[]
    r.append( FP_VAR( 'D CBA amount', 'D_CBA_shares' ) )
    r.append( FP_VAR( 'D TLS amount', 'D_TLS_shares' ) )
    r.append( FP_VAR( 'Mich Present', 'Mich_present', 'readonly' ) )
    if_opt = [ { 'val': 0, 'label': 'lease' } , {'val': 1, 'label': 'buyout' } ]
    r.append( FP_VAR( 'Ioniq6 future', 'Ioniq6_future', 'select', 'col-auto', '', if_opt ) )
    DISP.append(r)

    # Row 4
    r=[]
    r.append( FP_VAR( 'CBA price', 'CBA_price' ) )
    r.append( FP_VAR( 'TLS price', 'TLS_price' ) )
    r.append( FP_VAR( 'School Fees', 'School_Fees', 'readonly' ) )
    r.append( FP_VAR( 'FINAL # of CBA', 'CBA', 'readonly' ) )
    r.append( FP_VAR( 'FINAL # of TLS', 'TLS', 'readonly' ) )
    DISP.append(r)

    now=datetime.today().strftime('%Y-%m-%d')
    return render_template('index.html', now=now, finance=finance_data, depletion_date=depletion_date, savings=savings_per_fortnight, BUDGET=BUDGET, COMP=COMP, DISP=DISP)

@app.route('/save', methods=['POST'])
def save():
    insert_cset( request.get_json() )
    return "200"

@app.route('/update', methods=['POST'])
def update():

    finance_data = (
        request.form['D_Salary'],
        request.form['D_Num_fortnights_pay'],
        request.form['School_Fees'],
        request.form['Car_loan_via_pay'],
        request.form['Car_loan'],
        request.form['Car_balloon'],
        request.form['Car_buyout'],
        request.form['Living_Expenses'],
        request.form['Savings'],
        request.form['Interest_Rate'],
        request.form['Inflation'],
        request.form['Mich_present'],
        request.form['Overseas_trip'],
        request.form['Mark_reno'],
        request.form['D_leave_owed_in_days'],
        request.form['D_TLS_shares'],
        request.form['M_TLS_shares'],
        request.form['D_CBA_shares'],
        request.form['TLS_price'],
        request.form['CBA_price'],
        request.form['Overseas_trip_date'],
        request.form['Mark_reno_date'],
        request.form['Car_buyout_date'],
        request.form['Sell_shares'],
        request.form['compare_to'],
        request.form['Ioniq6_future']
    )

    update_finance(finance_data)

    return redirect(url_for('index'))

# Main program
if __name__ == '__main__':
    app.run(debug=True)


##########
#
# How to cross-check, so we get paid: 4762.29 + 1962.56 per fortnight or: $174846.1 AFTER TAX
# take $20k for Cam, and $20k for Mich for schools last year, $10k for Cam pres, take $72k for living, take $8k in furniture.
# We went from 250 to 300k (more or less), so about right
# to note: transfers to Cam/Mich - $850
# 
##########
