import csv
import itertools #to iterate between the rows
import pandas as pd #to import csv files into tables if need be
import requests #to import requests to use APIS
df = pd.read_csv('all_drinks.csv') #table of CSV file
print('Enter 1 to search by alcohol') #will link to search by alcohol def
print('Enter 2 to search by name') #will link to search by name def
print ('Enter 3 to search for food establishments')  #will connect to API to search for food establishments
def searchbyalcohol():
    data = []
    with open('all_drinks.csv') as csvfile:
        reader = csv.reader(csvfile)
        for row in reader:
            data.append(row) #creates a list which shows all of the rows with the correct alcohol in and assigns it to the data list.
        ingredient1 = input("Choose your main ingredient: ")
        for row1 in data:
            if ingredient1 in row1:
                print((row1[1]) + '\n' + (row1[6]) + '\n') #prints the name and picture of each row the ingredient is in
        ingredient2 = input('Choose your second ingredient: ')
        for row2 in data:
            if ingredient1 in row2 and ingredient2 in row2:
                print((row2[1]) + '\n' + (row2[6]) + '\n')#prints the name and picture of each row with both the first and second ingrideint in
                break
        else:
            print('Please have another go!')
def searchbyname():
    data = []
    with open('all_drinks.csv') as csvfile:
        reader = csv.reader(csvfile)
        for row4 in reader:
            data.append(row4) #creates a list which shows all of the rows with the same cocktail name in and assigns it to the data list.
        name = str(input('Enter cocktail name for more information:  '))
        for row5 in data:
            if name in row5[1]:
                print(row5) #prints the cocktail info
                break
        else:
            print('Sorry, we dont have that cocktail yet! Have another go! :)')
def searchbytown():
    import requests
    town = input('Enter town name here: ')
    url = f"https://wyre-data.p.rapidapi.com/restaurants/town/{town}" #pulls from the API - town is linked
    headers = {
        "X-RapidAPI-Host": "wyre-data.p.rapidapi.com",
        "X-RapidAPI-Key": "a8428d8345msh48339e76ce45ef7p188730jsnc872cba8828e"
    }
    response = requests.request("GET", url, headers=headers)
    results = response.json() #updated to JSON file to be read
    for result in results[:20]: #gives the first 20 food places available
        print(result['BusinessName'] + '\n' + result['AddressLine2'] + '\n') #gives business name and address line, rather than whole row.
num = 10 #repeats the program 10 times so the user can have multiple searches
for _ in itertools.repeat(None, num):
    src = input('Enter 1, 2 or 3 here to have a go:  ')
    if src == '1':
        searchbyalcohol()
    elif src == '2':
        searchbyname()
    elif src == '3':
        searchbytown()
    else:
            print('Sorry, invalid input. Please try again.')
    print('Enter 1 to search by alcohol')
    print('Enter 2 to search by name')
    print('Enter 3 to search for food establishment')