from playwright.sync_api import sync_playwright
import concurrent.futures
import time

# user_data = [
#     {
#         "username": "muskan123",
#         "password": "Kumari@3011",
#         "passport": "123456789",
#         "country": "India",
#         "birth_year": "2001",
#     },
#     {
#         "username": "muskankum",
#         "password": "Muskan@3012",
#         "passport": "123456789",
#         "country": "India",
#         "birth_year": "2001",
#     },
#     {
#         "username": "katz",
#         "password": "P@ss12344444",
#         "passport": "123456789",
#         "country": "India",
#         "birth_year": "2001",
#     }
# ]


def run_browser_session() -> bool:

    with sync_playwright() as p:
        
        # Start timer
        s = time.time()

        # Launch a Chromium browser
        browser = p.chromium.launch(headless=False, slow_mo=800)
        context = browser.new_context()

        # Start tracing for screenshots, snapshots, and sources
        context.tracing.start(screenshots=True, snapshots=True, sources=True)

        # Create a new page and navigate to the website
        page = context.new_page()
        page.goto("https://smart.gdrfad.gov.ae/HomePage.aspx?GdfraLocale=en-US")

        # Fill in username and password, then click on the login button
        page.get_by_label("lblUsername").fill("john9898")
        page.get_by_label("lblPassword").fill("Kalpesh@08")
        page.get_by_role("button", name="Login").click()

        # Click on the link to create a new application
        page.get_by_role("link", name="New Application Create new").click()

        # Click on the link to create a new 5 years tourism entry application
        page.get_by_role("link", name="New 5 Years Tourism Entry").click()

        # Fill Emirate in address inside UAE
        page.locator(
            "#s2id_EmaratechSG_Theme_wt789_block_wtFormContent_SmartChannels_Application_CW_wt629_block_WebPatterns_wtcntAppSimpleSecExpandable_block_wtContent_wtContent_wtcmbAddressInsideEmiratesId").get_by_role(
            "link", name="-- Select -- ").click()
        page.locator("li").filter(has_text="DUBAI").click()

        # Select purpose of visit
        page.locator(
            "#s2id_EmaratechSG_Theme_wt789_block_wtFormContent_SmartChannels_Application_CW_wt586_block_WebPatterns_wtcntAppSimpleSecExpandable_block_wtContent_wtContent_wtcmbVisitPurpose").get_by_role(
            "link", name="-- Select -- ").click()
        page.locator("li").filter(has_text="Tourism").click()

        # Select passport type
        page.locator(
            "#s2id_EmaratechSG_Theme_wt789_block_wtFormContent_SmartChannels_Application_CW_wt217_block_WebPatterns_wtcntAppSimpleSecExpandable_block_wtContent_wtContent_wtcmbPassportTypeId").get_by_role(
            "link", name="-- Select -- ").click()
        page.locator("li").filter(has_text="Normal").click()

        # Fill in passport number
        page.get_by_label("Passport Number").fill("P100000")

        # Select current nationality
        page.locator(
            "#s2id_EmaratechSG_Theme_wt789_block_wtFormContent_SmartChannels_Application_CW_wt217_block_WebPatterns_wtcntAppSimpleSecExpandable_block_wtContent_wtContent_wtcmbApplicantNationality").get_by_role(
            "link", name="-- Select -- ").click()
        page.get_by_role("combobox", name="Current Nationality", exact=True).fill("india")
        page.get_by_text("INDIA", exact=True).click()

        # Select previous nationality
        page.locator(
            "#s2id_EmaratechSG_Theme_wt789_block_wtFormContent_SmartChannels_Application_CW_wt217_block_WebPatterns_wtcntAppSimpleSecExpandable_block_wtContent_wtContent_wtcmbApplicantPreviousNationality").get_by_role(
            "link", name="-- Select -- ").click()
        page.get_by_role("combobox", name="Previous Nationality", exact=True).fill("india")
        page.get_by_text("INDIA", exact=True).click()

        # Click Search Data button
        page.get_by_role("button", name="Search Data").click()

        # Enter mother's name in english
        page.get_by_label("Mother Name En").fill("SIYA DEO")

        # Click button to translate mother's name to arabic
        page.locator('css=div[onclick="translateInputText(\'inpMotherNameEn\');"] img').click()

        # Enter mother's name in english
        page.get_by_label("Mother Name En").fill("SIYA DEO")

        # Fill religion
        page.locator(
            "#s2id_EmaratechSG_Theme_wt789_block_wtFormContent_SmartChannels_Application_CW_wt437_block_WebPatterns_wtcntAppSimpleSecExpandable_block_wtContent_wtContent_wtcmbApplicantReligion").get_by_role(
            "link", name="-- Select -- ").click()
        page.locator("li").filter(has_text="HINDHU").click()

        # Fill marital status
        page.locator(
            "#s2id_EmaratechSG_Theme_wt789_block_wtFormContent_SmartChannels_Application_CW_wt437_block_WebPatterns_wtcntAppSimpleSecExpandable_block_wtContent_wtContent_wtcmbApplicantMaritalStatus").get_by_role(
            "link", name="-- Select -- ").click()
        page.locator("li").filter(has_text="SINGLE").click()

        # Fill education
        page.locator(
            "#s2id_EmaratechSG_Theme_wt789_block_wtFormContent_SmartChannels_Application_CW_wt437_block_WebPatterns_wtcntAppSimpleSecExpandable_block_wtContent_wtContent_wtcmbApplicantEducation").get_by_role(
            "link", name="-- Select -- ").click()
        page.locator("li").filter(has_text="B.SC. COMMERCE & BUSINESS ADMN").click()

        # Fill language
        page.locator(
            "#s2id_EmaratechSG_Theme_wt789_block_wtFormContent_SmartChannels_Application_CW_wt437_block_WebPatterns_wtcntAppSimpleSecExpandable_block_wtContent_wtContent_wtcmbApplicantLanguage1").get_by_role(
            "link", name="-- Select -- ").click()
        page.locator("li").filter(has_text="ENGLISH").click()

        # Fill coming from country
        page.locator(
            "#s2id_EmaratechSG_Theme_wt789_block_wtFormContent_SmartChannels_Application_CW_wt437_block_WebPatterns_wtcntAppSimpleSecExpandable_block_wtContent_wtContent_wtcmbComingFromCountry").get_by_role(
            "link", name="-- Select -- ").click()
        page.get_by_role("combobox", name="Coming From Country", exact=True).fill("india")
        page.get_by_text("INDIA", exact=True).click()

        # Fill bank statement country
        page.locator(
            "#s2id_EmaratechSG_Theme_wt789_block_wtFormContent_SmartChannels_Application_CW_wt437_block_WebPatterns_wtcntAppSimpleSecExpandable_block_wtContent_wtContent_wtcmbBankStatementCountry").get_by_role(
            "link", name="-- Select -- ").click()
        page.get_by_role("combobox", name="Bank Satement Country", exact=True).fill("india")
        page.get_by_text("INDIA", exact=True).click()

        # Fill profession
        page.get_by_placeholder("Type or click for list").click()
        page.get_by_placeholder("Type or click for list").fill("MANAGER")

        # Fill email
        page.get_by_label("Email", exact=True).fill("rpa.support@visaero.com")
        page.get_by_label("Approval Email Copy Recipient").fill("rpa.support@visaero.com")

        # Fill contact details mobile number
        page.locator(
            "#EmaratechSG_Theme_wt789_block_wtFormContent_SmartChannels_Application_CW_wt629_block_WebPatterns_wtcntAppSimpleSecExpandable_block_wtContent_wtContent_wtinpMobileNumber").fill(
            "0559856719")

        # Fill preferred sms langauge
        page.locator(
            "#s2id_EmaratechSG_Theme_wt789_block_wtFormContent_SmartChannels_Application_CW_wt629_block_WebPatterns_wtcntAppSimpleSecExpandable_block_wtContent_wtContent_wtcmbPreferredLanguage").get_by_role(
            "link", name="-- Select -- ").click()
        page.locator("li").filter(has_text="ENGLISH").click()

        # Click address inside the UAE city
        page.locator(
            "#s2id_EmaratechSG_Theme_wt789_block_wtFormContent_SmartChannels_Application_CW_wt629_block_WebPatterns_wtcntAppSimpleSecExpandable_block_wtContent_wtContent_wtcmbAddressInsideCityId").get_by_role(
            "link", name="-- Select -- ").click()
        page.locator("li").filter(has_text="Dubai").click()

        # Fill address inside the UAE street, building/villa, floor and flat/villa number
        page.get_by_label("Street").fill("SHAIKH ZAYED ROAD")
        page.get_by_label("Building/Villa").fill("LATIFA TOWER")
        page.get_by_label("Floor").fill("3")
        page.get_by_label("Flat/Villa no.").fill("310")

        # Fill address outside the UAE country, city
        page.locator(
            "#s2id_EmaratechSG_Theme_wt789_block_wtFormContent_SmartChannels_Application_CW_wt629_block_WebPatterns_wtcntAppSimpleSecExpandable_block_wtContent_wtContent_wtcmbApplicantOutsideCountry").get_by_role(
            "link", name="-- Select -- ").click()
        page.get_by_role("combobox", name="Country", exact=True).fill("india")
        page.get_by_text("INDIA", exact=True).click()
        page.get_by_role("textbox", name="City *").fill("MUMBAI")

        # Fill address outside the UAE address
        page.get_by_label("Address").fill("MALAPPURAM DIST, KERALA -676507.^H301318430/03/2009 JEDDAH")

        # Fill address outside the UAE mobile number
        page.locator(
            "#EmaratechSG_Theme_wt789_block_wtFormContent_SmartChannels_Application_CW_wt629_block_WebPatterns_wtcntAppSimpleSecExpandable_block_wtContent_wtContent_wtinpAddressOutsideMobileNumber").click()
        page.locator(
            "#EmaratechSG_Theme_wt789_block_wtFormContent_SmartChannels_Application_CW_wt629_block_WebPatterns_wtcntAppSimpleSecExpandable_block_wtContent_wtContent_wtinpAddressOutsideMobileNumber").press_sequentially(
            "0501234567")

        # Fill applicant faith
        page.locator(
            "#s2id_EmaratechSG_Theme_wt789_block_wtFormContent_SmartChannels_Application_CW_wt437_block_WebPatterns_wtcntAppSimpleSecExpandable_block_wtContent_wtContent_wtcmbApplicantFaith").get_by_role(
            "link", name="-- Select -- ").click()
        page.locator("li").filter(has_text="UnKnown").click()

        # Address Inside UAE area
        page.get_by_role("link", name="Select Area ").click()
        page.locator("li").filter(has_text="Trade Centre 1").click()

        # Go on next page
        # page.get_by_role("button", name="Continue").click()

        # Stop tracing and close the context and browser
        context.tracing.stop(path="trace.zip")
        # ---------------------
        context.close()
        browser.close()
        
        # End timer
        e = time.time()
        
        # Print time taken
        print(e - s)

    return True


def main():
    with concurrent.futures.ProcessPoolExecutor(max_workers=4) as executor:
        futures = [executor.submit(run_browser_session) for _ in range(4)]

        # Optionally, wait for all futures to complete if needed.
        # concurrent.futures.wait(futures)


if __name__ == "__main__":
    main()

modify the code so that a separate trace.zip file is created for each process, the name of the file should be trace_firstname_lastname_processnumber