text: # -*- coding: utf-8 -*-
  """Loan Qualifier Application.
  
  This is a command line application to match applicants with qualifying loans.
  
  Example:
      $ python app.py
  """
  import sys
  import fire
  import questionary
  from pathlib import Path
  import csv
  
  from questionary.prompts.common import Choice
  from questionary.prompts.confirm import confirm
  
  from qualifier.utils.fileio import load_csv
  
  from qualifier.utils.calculators import (
      calculate_monthly_debt_ratio,
      calculate_loan_to_value_ratio,
  )
  
  from qualifier.filters.max_loan_size import filter_max_loan_size
  from qualifier.filters.credit_score import filter_credit_score
  from qualifier.filters.debt_to_income import filter_debt_to_income
  from qualifier.filters.loan_to_value import filter_loan_to_value
  
  
  def load_bank_data():
      """Ask for the file path to the latest banking data and load the CSV file.
  
      Returns:
          The bank data from the data rate sheet CSV file.
      """
  
      csvpath = questionary.text("Enter a file path to a rate-sheet (.csv):").ask()
      csvpath = Path(csvpath)
      if not csvpath.exists():
          sys.exit(f"Oops! Can't find this path: {csvpath}")
  
      return load_csv(csvpath)
  
  
  def get_applicant_info():
      """Prompt dialog to get the applicant's financial information.
  
      Returns:
          Returns the applicant's financial information.
      """
  
      credit_score = questionary.text("What's your credit score?").ask()
      debt = questionary.text("What's your current amount of monthly debt?").ask()
      income = questionary.text("What's your total monthly income?").ask()
      loan_amount = questionary.text("What's your desired loan amount?").ask()
      home_value = questionary.text("What's your home value?").ask()
  
      credit_score = int(credit_score)
      debt = float(debt)
      income = float(income)
      loan_amount = float(loan_amount)
      home_value = float(home_value)
  
      return credit_score, debt, income, loan_amount, home_value
  
  
  def find_qualifying_loans(bank_data, credit_score, debt, income, loan, home_value):
      """Determine which loans the user qualifies for.
  
      Loan qualification criteria is based on:
          - Credit Score
          - Loan Size
          - Debit to Income ratio (calculated)
          - Loan to Value ratio (calculated)
  
      Args:
          bank_data (list): A list of bank data.
          credit_score (int): The applicant's current credit score.
          debt (float): The applicant's total monthly debt payments.
          income (float): The applicant's total monthly income.
          loan (float): The total loan amount applied for.
          home_value (float): The estimated home value.
  
      Returns:
          A list of the banks willing to underwrite the loan.
  
      """
  
      # Calculate the monthly debt ratio
      monthly_debt_ratio = calculate_monthly_debt_ratio(debt, income)
      print(f"The monthly debt to income ratio is {monthly_debt_ratio:.02f}")
  
      # Calculate loan to value ratio
      loan_to_value_ratio = calculate_loan_to_value_ratio(loan, home_value)
      print(f"The loan to value ratio is {loan_to_value_ratio:.02f}.")
  
      # Run qualification filters
      bank_data_filtered = filter_max_loan_size(loan, bank_data)
      bank_data_filtered = filter_credit_score(credit_score, bank_data_filtered)
      bank_data_filtered = filter_debt_to_income(monthly_debt_ratio, bank_data_filtered)
      bank_data_filtered = filter_loan_to_value(loan_to_value_ratio, bank_data_filtered)
  
      print(f"Found {len(bank_data_filtered)} qualifying loans")
  
      return bank_data_filtered
  
  def save_csv(qualifying_loans):
      """Allows the user to input a csv path to export his qualifying loans and then proceeds to write them into a csv file with a predetermined header.
  
      Args:
          qualifying_loans (list of lists): The qualifying bank loans.
      """
  #This line of code asks the user where he would like to save his file using the questionary.text function, it also reminds them to add desired file name and extension
      csvpath = questionary.text("Enter a file path to save your qualifying loans as a (.csv):, be sure to add the desired file name followed by the .csv extension, if you would like to save to the default press enter.").ask()
  
  #This line of code allows for the user to press enter on the previous prompt to select a default saving path
      if csvpath == "":
          csvpath = Path("../loan_qualifier_app/data/qualifying_loans.csv") 
  
  #Sets the header for the output file
      header = ["Lender","Max Loan Amount","Max LTV","Max DTI","Min Credit Score","Interest Rate"]    
      
  #Creates the csv file using the "csvpath" inputed or defaulted, proceeds to  write the header and each applicable loan as a row
      with open(csvpath, 'w', newline='') as csvfile:
          csvwriter = csv.writer(csvfile)
          csvwriter.writerow(header)
          for row in qualifying_loans:
              csvwriter.writerow(row)
  
  #Prints back the filepath of the save file to remind the user of where it has been saved
      print(f"Your file has been saved to {csvpath}")
  
  
  def save_qualifying_loans(qualifying_loans):
      """Saves the qualifying loans to a CSV file.
  
      Args:
          qualifying_loans (list of lists): The qualifying bank loans.
      """
      # @TODO: Complete the usability dialog for savings the CSV Files.
      # YOUR CODE HERE!
  
  #This two lines of code verify if there are available loans to save, if there are none it returns a message telling the user to improve his credit conditions and exits the program
      if len(qualifying_loans) == 0:
          sys.exit("Please try to improve your credit score, debt-to-income ratio and loan-to-value ratio and try again!.")
  
  #If there are available loans to save, it prompts the user to select whether he wants to save them or not using the questionary "Select" function, if the choice is "Yes" 
  # it calls the save_csv function, if choice is "No" it prints a Goodbye message and exits the system.
      else:
          confirm = questionary.confirm("Would you like to save the qualifying loans found in a .csv file?").ask()
          if confirm == True:
              save_csv(qualifying_loans)     
          else:
              sys.exit(f"Goodbye, good luck in your loan search!")
  
  def run():
      """The main function for running the script."""
  
      # Load the latest Bank data
      bank_data = load_bank_data()
  
      # Get the applicant's information
      credit_score, debt, income, loan_amount, home_value = get_applicant_info()
  
      # Find qualifying loans
      qualifying_loans = find_qualifying_loans(
          bank_data, credit_score, debt, income, loan_amount, home_value
      )
  
      # Save qualifying loans
      save_qualifying_loans(qualifying_loans)
  
  
  if __name__ == "__main__":
      fire.Fire(run)
  