import urllib.request
import urllib.parse
import os
import time

API_KEY = "3Ttow6PwfJ9gEaSBhDT1c4L0ZgUGTTho343dUkwa"
VOICES = ["Corazon", "Dominic"]

phrases = [
    # Greetings & Pleasantries
    ("greetings", "001", "Bonġu"),
    ("greetings", "002", "Bonġu"),
    ("greetings", "003", "Wara nofsinhar it-tajjeb"),
    ("greetings", "004", "Bonswa"),
    ("greetings", "005", "Il-lejl it-tajjeb"),
    ("greetings", "006", "Ċaw - Saħħa"),
    ("greetings", "007", "Narak aktar tard"),
    ("greetings", "008", "Narak għada"),
    ("greetings", "009", "Kif int?"),
    ("greetings", "010", "Jiena tajjeb, grazzi"),
    ("greetings", "011", "Mhux ħażin, grazzi"),
    ("greetings", "012", "Tajjeb ħafna, grazzi"),
    ("greetings", "013", "Jekk jogħġbok"),
    ("greetings", "014", "Grazzi"),
    ("greetings", "015", "Grazzi ħafna"),
    ("greetings", "016", "Prosit"),
    ("greetings", "017", "Skużani"),
    ("greetings", "018", "Jiddispjaċini"),
    ("greetings", "019", "Tinkwetax"),
    ("greetings", "020", "L-ebda problema"),
    ("greetings", "021", "Iva"),
    ("greetings", "022", "Le"),
    ("greetings", "023", "Forsi"),
    ("greetings", "024", "Naturalment"),
    ("greetings", "025", "Jisimni"),
    ("greetings", "026", "X'jismek?"),
    ("greetings", "027", "Għandi pjaċir"),
    ("greetings", "028", "Minn fejn int?"),
    ("greetings", "029", "Titkellem bl-Ingliż?"),
    ("greetings", "030", "Ma nifhimx"),
    ("greetings", "031", "Jekk jogħġbok itkellem bil-mod"),
    ("greetings", "032", "Tista' terġa' tgħid?"),
    ("greetings", "033", "Il-Ġublew it-tajjeb!"),
    ("greetings", "034", "Prosit!"),
    ("greetings", "035", "Saħħa!"),
    ("greetings", "036", "Merħba!"),
    ("greetings", "037", "Ġurnata tajba!"),
    ("greetings", "038", "Inħobb Malta!"),

    # Getting Around
    ("getting-around", "001", "Fejn hi l-mitħalfa tal-karozza tal-linja?"),
    ("getting-around", "002", "Liema karozza tal-linja tmur Valletta?"),
    ("getting-around", "003", "Liema karozza tmur Sliema?"),
    ("getting-around", "004", "Liema karozza tmur San Ġiljan?"),
    ("getting-around", "005", "Din il-karozza tieqaf fl-ajruport?"),
    ("getting-around", "006", "X'ħin hi l-karozza li jmiss?"),
    ("getting-around", "007", "Kemm-il darba tiġi l-karozza?"),
    ("getting-around", "008", "Biljett wieħed, jekk jogħġbok"),
    ("getting-around", "009", "Biljett ta' ritorn, jekk jogħġbok"),
    ("getting-around", "010", "Karta tal-jum, jekk jogħġbok"),
    ("getting-around", "011", "Minn fejn nista' nieħu taxi?"),
    ("getting-around", "012", "Jekk jogħġbok sejjaħ taxi"),
    ("getting-around", "013", "Wassalni f'dan l-indirizz"),
    ("getting-around", "014", "Wassalni fl-ajruport"),
    ("getting-around", "015", "Kemm jiswi biex tmur Valletta?"),
    ("getting-around", "016", "Ieqaf hawn, jekk jogħġbok"),
    ("getting-around", "017", "Kemm hija 'l bogħod?"),
    ("getting-around", "018", "Hija 'l bogħod bil-mixi?"),
    ("getting-around", "019", "Dur ix-xellug"),
    ("getting-around", "020", "Dur il-lemin"),
    ("getting-around", "021", "Mur dritt"),
    ("getting-around", "022", "Ħdejn it-traffku"),
    ("getting-around", "023", "Fejn hu t-terminal tal-vapur?"),
    ("getting-around", "024", "X'ħin jitlaq il-vapur?"),
    ("getting-around", "025", "X'ħin jasal il-vapur?"),
    ("getting-around", "026", "Nista' nikri karozza hawn?"),
    ("getting-around", "027", "Fejn nista' nipparkja?"),
    ("getting-around", "028", "Jiena mitluf"),
    ("getting-around", "029", "Tista' turini fuq il-mappa?"),
    ("getting-around", "030", "Il-linja tlettax tmur Valletta"),
    ("getting-around", "031", "Hemm taxi tal-ilma?"),
    ("getting-around", "032", "Fejn hu l-waterfront ta' Valletta?"),

    # Shopping & Money
    ("shopping", "001", "Kemm jiswi dan?"),
    ("shopping", "002", "Dan huwa għali wisq"),
    ("shopping", "003", "Għandek xi ħaġa irħas?"),
    ("shopping", "004", "Nieħdu"),
    ("shopping", "005", "Ma rridx"),
    ("shopping", "006", "Nista' nħallas bil-kard?"),
    ("shopping", "007", "Taċċetta flus kontanti?"),
    ("shopping", "008", "Taċċetta contactless?"),
    ("shopping", "009", "Fejn hu l-eqreb ATM?"),
    ("shopping", "010", "Qed inħares biss, grazzi"),
    ("shopping", "011", "Għandek dan f'daqs differenti?"),
    ("shopping", "012", "Nista' nipprova dan?"),
    ("shopping", "013", "Fejn hi l-kamra tal-bdil?"),
    ("shopping", "014", "Nista' nieħu riċevuta?"),
    ("shopping", "015", "X'ħin tiftaħ?"),
    ("shopping", "016", "X'ħin tagħlaq?"),
    ("shopping", "017", "Is-suq"),
    ("shopping", "018", "Is-supermarket"),
    ("shopping", "019", "Għandek skont?"),
    ("shopping", "020", "Hemm bejgħ speċjali?"),
    ("shopping", "021", "Nista' nibdel dan?"),
    ("shopping", "022", "Għandi bżonn rifużjoni"),
    ("shopping", "023", "Dan huwa maqsur"),
    ("shopping", "024", "Għandek borża?"),

    # Emergencies
    ("emergencies", "001", "Ajjut!"),
    ("emergencies", "002", "Sejjaħ il-pulizija!"),
    ("emergencies", "003", "Sejjaħ l-ambulanza!"),
    ("emergencies", "004", "Sejjaħ tabib!"),
    ("emergencies", "005", "Sejjaħ il-brigata tan-nar!"),
    ("emergencies", "006", "Għandi bżonn sptar"),
    ("emergencies", "007", "Kien hemm inċident"),
    ("emergencies", "008", "Serquli"),
    ("emergencies", "009", "Ieqaf, ħalliel!"),
    ("emergencies", "010", "Tilfet il-passaport tiegħi"),
    ("emergencies", "011", "Tilfet il-kartiera tiegħi"),
    ("emergencies", "012", "Tilfet il-mowbajl tiegħi"),
    ("emergencies", "013", "Jiena midrub"),
    ("emergencies", "014", "Inħossni marid"),
    ("emergencies", "015", "Ma nistax nieħu n-nifs"),
    ("emergencies", "016", "Jien allerġiku għal"),
    ("emergencies", "017", "Jiena dijabetiku"),
    ("emergencies", "018", "Għandi bżonn mediċina"),
    ("emergencies", "019", "Fejn hu l-eqreb sptar?"),
    ("emergencies", "020", "Għandi bżonn insejjaħ l-ambaxxata tiegħi"),
    ("emergencies", "021", "Numru ta' emerġenza: mija u tnejn"),
    ("emergencies", "022", "Stazzjon tal-pulizija"),

    # Eating & Drinking
    ("eating", "001", "Mejda għal tnejn, jekk jogħġbok"),
    ("eating", "002", "Mejda għal erbgħa, jekk jogħġbok"),
    ("eating", "003", "Għandek riżervazzjoni?"),
    ("eating", "004", "Għandi riżervazzjoni f'isem"),
    ("eating", "005", "Il-menù, jekk jogħġbok"),
    ("eating", "006", "X'tirrakkomanda?"),
    ("eating", "007", "X'inhu l-ispeċjal tal-lum?"),
    ("eating", "008", "Nieħu dan"),
    ("eating", "009", "Nieħu l-istess"),
    ("eating", "010", "Ilma, jekk jogħġbok"),
    ("eating", "011", "Ordinarja jew gassuża?"),
    ("eating", "012", "Birra, jekk jogħġbok"),
    ("eating", "013", "Birra kbira, jekk jogħġbok"),
    ("eating", "014", "Tazza inbid aħmar, jekk jogħġbok"),
    ("eating", "015", "Tazza inbid abjad, jekk jogħġbok"),
    ("eating", "016", "Flixkun inbid, jekk jogħġbok"),
    ("eating", "017", "Kafè, jekk jogħġbok"),
    ("eating", "018", "Te, jekk jogħġbok"),
    ("eating", "019", "Mingħajr silġ, jekk jogħġbok"),
    ("eating", "020", "Il-kont, jekk jogħġbok"),
    ("eating", "021", "Nistgħu naqsmu l-kont?"),
    ("eating", "022", "Is-servizz inkluż?"),
    ("eating", "023", "Żomm il-fdal"),
    ("eating", "024", "Kien tajjeb ħafna!"),
    ("eating", "025", "Komplimeniti lill-kok"),
    ("eating", "026", "Jiena veġetarjan"),
    ("eating", "027", "Jiena vegan"),
    ("eating", "028", "Għandi allerġija għall-ġewż"),
    ("eating", "029", "Għandi allerġija għall-gluttin"),
    ("eating", "030", "Rawnd ieħor, jekk jogħġbok!"),
    ("eating", "031", "Pastizz, jekk jogħġbok"),
    ("eating", "032", "X'inhu pastizzi?"),
    ("eating", "033", "Fejn hemm ristorant tajjeb?"),
    ("eating", "034", "Hemm veduta tal-baħar?"),
    ("eating", "035", "Mejda barra, jekk jogħġbok"),
    ("eating", "036", "Nista' nara l-menù tad-deżżerta?"),
    ("eating", "037", "Għandi biżżejjed, grazzi"),
    ("eating", "038", "Fejn hu l-aħjar post għall-ħut?"),
    ("eating", "039", "Tagħmel takeaway?"),
    ("eating", "040", "Dan it-tiġieqa qawwi?"),

    # Socialising & Dating
    ("socialising", "001", "Trid xi ħaġa tixrob?"),
    ("socialising", "002", "Nista' nixtrilek xi ħaġa?"),
    ("socialising", "003", "X'qed tixrob?"),
    ("socialising", "004", "Tidher sabiħa"),
    ("socialising", "005", "Għandek tbissima sabiħa"),
    ("socialising", "006", "Inti Malti?"),
    ("socialising", "007", "Jiena mill-Ingilterra"),
    ("socialising", "008", "Din hija l-ewwel darba li żżur Malta"),
    ("socialising", "009", "Kemm ilu hawn?"),
    ("socialising", "010", "Trid tiżfen?"),
    ("socialising", "011", "Tiżfen tajjeb ħafna"),
    ("socialising", "012", "Tista' tagħtini n-numru tiegħek?"),
    ("socialising", "013", "Inti fuq Instagram?"),
    ("socialising", "014", "Jogħġobni"),
    ("socialising", "015", "Inti fuq kumiku ħafna"),
    ("socialising", "016", "Inħobb Malta ħafna"),
    ("socialising", "017", "Fejn joħorġu n-nies?"),
    ("socialising", "018", "Hemm bar tajjeb qrib hawn?"),
    ("socialising", "019", "Paceville hija ż-żona tal-ħajja bil-lejl"),
    ("socialising", "020", "Il-mużika hija sabiħa"),
    ("socialising", "021", "Inti single?"),
    ("socialising", "022", "Jiena f'relazzjoni"),
    ("socialising", "023", "Inti attraenti ħafna"),
    ("socialising", "024", "Trid tmur mixi?"),
    ("socialising", "025", "Trid niltaqgħu mill-ġdid?"),
    ("socialising", "026", "Din kienet filgħaxija mill-isbaħ"),
    ("socialising", "027", "Għamilt ħin tajjeb ħafna"),
    ("socialising", "028", "Kien pjaċir"),
    ("socialising", "029", "Ejja nkunu ħbieb"),
    ("socialising", "030", "Fejn huma l-aħjar nightclubs?"),
    ("socialising", "031", "X'ħin jagħlaq?"),
    ("socialising", "032", "Naqsmu taxi?"),

    # Sightseeing & What's On
    ("sightseeing", "001", "Liema hi l-aħjar bajja?"),
    ("sightseeing", "002", "Fejn hi l-eqreb bajja?"),
    ("sightseeing", "003", "Hemm festa din il-ġimgħa?"),
    ("sightseeing", "004", "Kif immur il-Blue Lagoon?"),
    ("sightseeing", "005", "X'ħin jitlaq il-vapur għal Kemmuna?"),
    ("sightseeing", "006", "Hemm qorti tat-tennis qrib?"),
    ("sightseeing", "007", "Hemm kamra tas-snooker?"),
    ("sightseeing", "008", "Hemm pixxina?"),
    ("sightseeing", "009", "Hemm golf course?"),
    ("sightseeing", "010", "Fejn hi l-belt il-qadima ta' Valletta?"),
    ("sightseeing", "011", "Fejn huma t-Tempji Megalitiċi?"),
    ("sightseeing", "012", "X'ħin jagħlaq?"),
    ("sightseeing", "013", "Hemm dħul?"),
    ("sightseeing", "014", "Żewġ adulti, jekk jogħġbok"),
    ("sightseeing", "015", "Adult wieħed u tifel wieħed"),
    ("sightseeing", "016", "Fejn nista' nixtri biljetti?"),
    ("sightseeing", "017", "Nixtieq nżur Valletta"),
    ("sightseeing", "018", "Nixtieq nżur it-Tliet Ibliet"),
    ("sightseeing", "019", "Nixtieq nżur Mdina"),
    ("sightseeing", "020", "Jiswa biex iżżur Għawdex?"),
    ("sightseeing", "021", "X'ħin jitlaq il-vapur għal Għawdex?"),
    ("sightseeing", "022", "Fejn hu l-eqreb ċentru tad-diving?"),
    ("sightseeing", "023", "Hemm festa tar-raħal dan il-weekend?"),
    ("sightseeing", "024", "Fejn huma l-logħob tan-nar?"),
    ("sightseeing", "025", "Il-fotografija hija permessa?"),
    ("sightseeing", "026", "Fejn hi l-aħjar veduta?"),
    ("sightseeing", "027", "Hemm tour guidat?"),
    ("sightseeing", "028", "Għandek audio guide?"),
    ("sightseeing", "029", "X'inhu l-aħjar ħin biex iżżur?"),
    ("sightseeing", "030", "Hemm vjaġġ bil-vapur li nista' nieħu?"),
    ("sightseeing", "031", "Fejn nista' nagħmel snorkelling?"),
    ("sightseeing", "032", "Fejn hu l-Popeye Village?"),
    ("sightseeing", "033", "Valletta hija l-kapitali ta' Malta?"),

    # Weather
    ("weather", "001", "Kif hi t-temp illum?"),
    ("weather", "002", "Se joqgħod jagħmel ix-xita?"),
    ("weather", "003", "Illum huwa sħun ħafna"),
    ("weather", "004", "Kemm hu sabiħ it-temp"),
    ("weather", "005", "Hemm ftit sħab"),
    ("weather", "006", "Qed tagħmel ix-xita"),
    ("weather", "007", "Dejjem hekk bix-xemx?"),
    ("weather", "008", "Kemm jum tax-xemx ikollu Malta?"),
    ("weather", "009", "X'temperatura għandha l-baħar?"),
    ("weather", "010", "Il-baħar huwa kwiet illum?"),
    ("weather", "011", "Qiegħed jiġi maltemp?"),
    ("weather", "012", "Hemm ir-riħ wisq illum"),
    ("weather", "013", "Ix-xemx qawwija ħafna"),
    ("weather", "014", "X'inhi l-previżjoni għall-weekend?"),
    ("weather", "015", "Għandi bżonn krema tax-xemx?"),
    ("weather", "016", "Liema xahar huwa l-iktar sħun?"),
    ("weather", "017", "Is-silġ jaqa' xi kultant Malta?"),
    ("weather", "018", "Kif inhu x-xitwa?"),
    ("weather", "019", "Temp ideali għall-bajja"),
    ("weather", "020", "L-ilma huwa sħun?"),

    # Accommodation
    ("accommodation", "001", "Għandi riżervazzjoni"),
    ("accommodation", "002", "Għandek kamra vojta?"),
    ("accommodation", "003", "Kemm jiswi fil-lejl?"),
    ("accommodation", "004", "Kemm jiswi fil-ġimgħa?"),
    ("accommodation", "005", "Il-kolazzjon huwa inkluż?"),
    ("accommodation", "006", "Hemm Wi-Fi?"),
    ("accommodation", "007", "X'inhu l-password tal-Wi-Fi?"),
    ("accommodation", "008", "X'ħin hu l-check-in?"),
    ("accommodation", "009", "X'ħin hu l-check-out?"),
    ("accommodation", "010", "Nista' noħroġ aktar tard?"),
    ("accommodation", "011", "Nista' nħalli l-bagalji hawn?"),
    ("accommodation", "012", "Il-kundizzjonatur tal-arja mhuwiex jaħdem"),
    ("accommodation", "013", "L-ilma sħun mhux jaħdem"),
    ("accommodation", "014", "Għandi bżonn aktar xugamani"),
    ("accommodation", "015", "Fejn hu l-lift?"),
    ("accommodation", "016", "Hemm pixxina?"),
    ("accommodation", "017", "Hemm parking?"),
    ("accommodation", "018", "Hemm veduta tal-baħar?"),
    ("accommodation", "019", "Nista' naħjar għal lejl ieħor?"),
    ("accommodation", "020", "Il-kamra tiegħi ma ntlaqtitx"),
    ("accommodation", "021", "Hemm ħoss min-naħa tal-ġenb"),

    # Health & Pharmacy
    ("health", "001", "Fejn hi l-ispiżerija?"),
    ("health", "002", "Għandi bżonn riċetta"),
    ("health", "003", "Għandi uġigħ ta' ras"),
    ("health", "004", "Għandi uġigħ ta' żaqq"),
    ("health", "005", "Għandi deni"),
    ("health", "006", "Għandi uġigħ fil-gerżuma"),
    ("health", "007", "Għandi ħruq tax-xemx"),
    ("health", "008", "Naqsitni meduża"),
    ("health", "009", "Għandi bżonn krema tax-xemx"),
    ("health", "010", "Għandek pilloli kontra l-uġigħ?"),
    ("health", "011", "Għandek xi ħaġa kontra d-dijarea?"),
    ("health", "012", "Għandek repellent għall-insetti?"),
    ("health", "013", "Għandi bżonn tabib tas-snien"),
    ("health", "014", "Hemm tabib hawn?"),
    ("health", "015", "Jiena dijabetiku"),
    ("health", "016", "Nieħu din il-mediċina"),
    ("health", "017", "L-ilma huwa sigur biex tixrob?"),
    ("health", "018", "Qiegħed iduru bija"),
    ("health", "019", "Ngiditni xi ħaġa"),
    ("health", "020", "Għandi bżonn faxxa"),

    # Practical Everyday
    ("practical", "001", "Fejn hi l-uffiċċju tal-posta?"),
    ("practical", "002", "Fejn hi l-bank?"),
    ("practical", "003", "Hemm Wi-Fi b'xejn hawn?"),
    ("practical", "004", "Fejn nista' nixtri SIM card?"),
    ("practical", "005", "Nista' niċċargja l-mowbajl hawn?"),
    ("practical", "006", "X'inhu l-indirizz?"),
    ("practical", "007", "Għandi bżonn nibgħat pakkett"),
    ("practical", "008", "Hemm lavanderija qrib?"),
    ("practical", "009", "Hemm tojlit pubbliku qrib?"),
    ("practical", "010", "Nista' nuża din il-brikka?"),
    ("practical", "011", "Għandek gazzetta bl-Ingliż?"),
    ("practical", "012", "X'ħin jiftaħ il-bank?"),
    ("practical", "013", "X'inhuma l-ħinijiet tal-ftuħ?"),
    ("practical", "014", "Għandi bżonn mappa"),
    ("practical", "015", "Fejn hu l-uffiċċju tal-informazzjoni turistika?"),
    ("practical", "016", "Nista' nieħu riċevuta?"),

    # Expat Life
    ("expat", "001", "Qiegħed infittex appartament kiri"),
    ("expat", "002", "Kemm hu l-kera fix-xahar?"),
    ("expat", "003", "Huma inklużi l-kontijiet?"),
    ("expat", "004", "Huwa arredat?"),
    ("expat", "005", "Meta jkun disponibbli?"),
    ("expat", "006", "Nista' nara l-appartament?"),
    ("expat", "007", "Għandi bżonn niftaħ kont fil-bank"),
    ("expat", "008", "Fejn hu l-uffiċċju ta' Identità Malta?"),
    ("expat", "009", "Kif napplika għar-residenza?"),
    ("expat", "010", "Għandi bżonn nirreġistra l-indirizz tiegħi"),
    ("expat", "011", "Għandi bżonn kard tas-saħħa"),
    ("expat", "012", "Fejn nista' nsib skola tajba?"),
    ("expat", "013", "Hemm komunità ta' expatriates hawn?"),
    ("expat", "014", "Irrid nitgħallem aktar Malti"),
    ("expat", "015", "Fejn huma ż-żoni tajba biex tgħix?"),
    ("expat", "016", "Huwa sigur hawn?"),
    ("expat", "017", "Jiena self-employed"),
    ("expat", "018", "Qiegħed infittex xogħol"),
    ("expat", "019", "Kif inhu l-ispiża tal-ħajja?"),
    ("expat", "020", "Kif inħallas il-kontijiet tiegħi?"),
    ("expat", "021", "Għandi bżonn numru tat-taxxa"),
    ("expat", "022", "Fejn hi l-eqreb skola?"),
    ("expat", "023", "Kemm tieħu ż-żmien ir-residenza?"),
]

def generate_audio(text, voice, output_path):
    url = f"https://api.narakeet.com/text-to-speech/mp3?voice={urllib.parse.quote(voice)}"
    req = urllib.request.Request(url, method='POST')
    req.add_header('Accept', 'application/octet-stream')
    req.add_header('Content-Type', 'text/plain')
    req.add_header('x-api-key', API_KEY)
    req.data = text.encode('utf-8')
    with urllib.request.urlopen(req) as response:
        with open(output_path, 'wb') as f:
            f.write(response.read())

total = len(phrases) * len(VOICES)
done = 0
errors = []

for voice in VOICES:
    voice_dir = os.path.join("audio", voice.lower())
    for category, num, text in phrases:
        cat_dir = os.path.join(voice_dir, category)
        os.makedirs(cat_dir, exist_ok=True)
        filename = f"{num}.mp3"
        filepath = os.path.join(cat_dir, filename)
        if os.path.exists(filepath):
            done += 1
            print(f"[{done}/{total}] Already exists: {voice}/{category}/{filename}")
            continue
        try:
            generate_audio(text, voice, filepath)
            done += 1
            print(f"[{done}/{total}] ✅ {voice}/{category}/{filename}")
            time.sleep(0.3)
        except Exception as e:
            done += 1
            errors.append(f"{voice}/{category}/{filename}: {e}")
            print(f"[{done}/{total}] ❌ ERROR — {voice}/{category}/{filename}: {e}")

print("\n=== DONE ===")
print(f"Generated: {total - len(errors)} files")
if errors:
    print(f"Errors ({len(errors)}):")
    for e in errors:
        print(f"  - {e}")
else:
    print("All files generated successfully! 🎉")

