# imports
import time
from dotenv import load_dotenv
load_dotenv()

from accounts import *
from economy import *
from jobs import *

# pre vars
code = None

# login user
does_user_have_account = input("Do you have an account? (y/n): ")

if does_user_have_account == "y":
    email = input("Please enter your email: ")
    password = input("Please enter your password: ")

    code = find_account(code = None, email=email, password=password)
elif does_user_have_account == "n":
    email = input("Please enter your email: ")
    password = input("Please enter your password: ")
    name = input("Please enter your name: ")

    code = create_account(name, email, password)
    create_econ_account(code)
else:
    print("That is not a valid answer")

# start function
def start(option):
    # start jobs
    if option == "jobs":
        user_job = get_user_job(code)
        if user_job is None:
            get_all_jobs()
            job_id = int(input("Please enter the ID of the job you would like: "))
            did_job_assign = assign_job(code, job_id)
            if did_job_assign is True:
                print("Congratulations! Welcome to your new job. Work to get some money")
                time.sleep(1)
                return
            else:
                print("Whoops. Sorry. You did not get accepted into this job.")
        else:
            job_details = get_job_details(get_user_job(code))
            update_job = input(f"You already have a job.\n\nTitle: {job_details.title}\nEducation Requirement: {job_details.education}\nPay Per Hour: {job_details.pph}"
                               f"Do you want to change your job? (y/n): ")

            if update_job == "y":
                get_all_jobs()
                job_id = int(input("Please enter the ID of the job you would like: "))
                did_job_assign = assign_job(code, job_id)
                if did_job_assign is True:
                    print("Congratulations! Welcome to your new job. Work to get some money")
                    time.sleep(1)
                    return
                else:
                    print("Whoops. Sorry. You did not get accepted into this job.")
            else:
                return
    # end jobs
    # begin add_job
    elif option == "add_job":
        account_status = get_account_status(code)

        if account_status == "admin":
            title = input("Name for the job: ")
            education_level = int(input("What is the education requirement? (1 - 5): "))
            pay = int(input("What is the pph (pay-per-hour)? "))

            new_job = create_new_job(title, education_level, pay)

            if new_job:
                print("The new job has been created successfully!")
                time.sleep(1)
                return
            else:
                print("The new job has not been created. Something went wrong.")
                time.sleep(1)
                return
    # end add_job
    # begin work
    elif option == "work":
        hours_worked = work(code)
        job_id = get_user_job(code)
        job = get_job_details(job_id)
        if hours_worked is None:
            print("Something went wrong.. ")
            time.sleep(1)
            return

        print(f"""
You worked for a total of: {hours_worked} hours.
You made ${hours_worked * job.pph}!
Great job today!
""")
        time.sleep(2)
        return
    # end work
    # begin balance
    elif option == "balance":
        balance = get_balance(code)
        cash = get_cash(code)
        print(f"The balance in your bank is: ${balance}. The balance of cash you have is: ${cash}")
        time.sleep(3)
        return
    # end balance
    # TODO: Add school and inventory
    # begin deposit
    elif option == "deposit":
        amount = int(input("How much would you like to deposit? "))
        did_deposit = deposit(code, amount)

        if did_deposit:
            print("The deposit is complete!")
            time.sleep(2)
            return

        return
    # end deposit
    # begin withdraw
    elif option == "withdraw":
        amount = int(input("How much would you like to withdraw? "))
        did_withdraw = withdraw(code, amount)

        if did_withdraw:
            print("The withdraw is complete!")
            time.sleep(2)
            return

        return
    # end withdraw
    # begin setup
    elif option == "setup":
        account_manager(code)
        time.sleep(3)
        return
    # end setup

# main function
while code is not None:
    print(f"""
Welcome {str(get_user_name(code)).capitalize()}.
Your bank balance is {get_balance(code)}
In cash, you have {get_cash(code)}
⚠️This is not a real bank account. This is all fiction.⚠️

What would you like to do?
Options:
    work - work for some money
    setup - enter the user configuration menu
    balance - view your balance
    jobs - view and apply for jobs
    school - go to school
    inventory - view your inventory
    deposit - deposit some of your cash into your bank account
    withdraw - take some money out of your bank account
    leave - exit your virtual economy
""")

    options = input("")

    if options == "leave":
        break

    start(options)