import os
import json
from flask import Flask, render_template, request, flash

# app (convention name) = instance of Flask class
# __name__, the first argument of the Flask class, is the name of the aplications module or package
# since we're just using a single module, we can use __name__ which is a built in Python variable
# Flask needs this so it knows where to look for templates and static files
app = Flask(__name__)
app.secret_key = 'some_secret'

# app.route decorator (decorator starts with @)
# decorator is a way to wrap functions
@app.route('/')
def index():
    return render_template("index.html")


@app.route('/about')
def about():
    data = []
    with open("data/company.json", "r") as json_data:
        data = json.load(json_data)
    return render_template("about.html", page_title="About", company=data)


@app.route('/about/<member_name>')
def about_member(member_name):
    member = {}
   
    with open("data/company.json", "r") as json_data:
        data = json.load(json_data)
        for obj in data:
            if obj["url"] == member_name:
                member = obj
   
    return render_template("member.html", member=member)


@app.route('/contact', methods=["GET", "POST"])
def contact():
    if request.method == "POST":
        flash("Thanks {}, we have received your message".format(
            request.form["name"]
        ))
    return render_template("contact.html", page_title="Contact")


@app.route('/careers')
def careers():
    return render_template("careers.html", page_title="Careers")

#reference built in variable __name__
# DON'T SUBMIT A PROJECT WITH DEBUG = TRUE
# if __name__ == '__main__':
#     app.run(host=os.environ.get('IP'),
#             port=int(os.environ.get('PORT')),
#             debug=True)