import os
import csv
import pandas as pd
from playwright.sync_api import sync_playwright, TimeoutError
 
APPLICATION_PAGE_URL = "https://iprsearch.ipindia.gov.in/PublicSearch/PublicationSearch/ApplicationStatus"
PDF_DIRECTORY = r"C:\Users\Administrator\Desktop\SHR\old\Application Status\applications_pdfs"
 
def scrape_application_data(application_number):
    """
    Scrape application data for a single application number.
    """
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False)
        page = browser.new_page()
 
        try:
            os.makedirs(PDF_DIRECTORY, exist_ok=True)
            print(f"Visiting the website for application number: {application_number}")
            page.goto(APPLICATION_PAGE_URL, wait_until="domcontentloaded", timeout=60000)
 
            page.wait_for_selector('input[name="ApplicationNumber"]', timeout=60000)
            page.fill('input[name="ApplicationNumber"]', application_number)
 
            print("Please enter the CAPTCHA manually in the browser and submit the form.")
 
            page.wait_for_url("https://iprsearch.ipindia.gov.in/PatentSearch/PatentSearch/ViewApplicationStatus", timeout=90000)
 
            if page.locator("text=Sorry, we can't load the resource").is_visible():
                print(f"Failed to load data for application number {application_number}: Sorry, we can't load the resource")
                return {"Application Number": application_number, "Status": "Resource Not Loaded"}
 
            print(f"Successfully loaded application status page for application number {application_number}.")
            pdf_path = save_pdf(page, application_number)
 
            # Extract data
            application_data = {
                'Application Number': application_number,
                'Application Type': safe_extract_text(page.locator('td:has-text("APPLICATION TYPE") + td')),
                'Date of Filing': safe_extract_text(page.locator('td:has-text("DATE OF FILING") + td')),
                'Applicant Name': safe_extract_text(page.locator('td:has-text("APPLICANT NAME") + td strong')),
                'Title of Invention': safe_extract_text(page.locator('td:has-text("TITLE OF INVENTION") + td')),
                'Field of Invention': safe_extract_text(page.locator('td:has-text("FIELD OF INVENTION") + td')),
                'E-Mail (As Per Record)': safe_extract_text(page.locator('td:has-text("E-MAIL (As Per Record)") + td')),
                'Additional Email': safe_extract_text(page.locator('td:has-text("ADDITIONAL-EMAIL (As Per Record)") + td')),
                'E-Mail (Updated Online)': safe_extract_text(page.locator('td:has-text("E-MAIL (UPDATED Online)") + td')),
                'PCT International Application Number': safe_extract_text(page.locator('td:has-text("PCT INTERNATIONAL APPLICATION NUMBER") + td')),
                'PCT International Filing Date': safe_extract_text(page.locator('td:has-text("PCT INTERNATIONAL FILING DATE") + td')),
                'Priority Date': safe_extract_text(page.locator('td:has-text("PRIORITY DATE") + td')),
                'Request For Examination Date': safe_extract_text(page.locator('td:has-text("REQUEST FOR EXAMINATION DATE") + td')),
                'Publication Date': safe_extract_text(page.locator('td:has-text("PUBLICATION DATE (U/S 11A)") + td')),
                'First Examination Report Date': safe_extract_text(page.locator('td:has-text("FIRST EXAMINATION REPORT DATE") + td')),
                'Date Of Certificate Issue': safe_extract_text(page.locator('td:has-text("Date Of Certificate Issue") + td')),
                'Post Grant Journal Date': safe_extract_text(page.locator('td:has-text("POST GRANT JOURNAL DATE") + td')),
                'Reply To FER Date': safe_extract_text(page.locator('td:has-text("REPLY TO FER DATE") + td')),
                'Application Status': safe_extract_text(page.locator('div.tab-pane.fade.active.in.Action.PatentDetails td:has-text("APPLICATION STATUS") + td h3 strong'))
            }
 
            return application_data
 
        except Exception as e:
            print(f"Error during scraping for application number {application_number}: {e}")
            return {"Application Number": application_number, "Status": "Error", "Error Message": str(e)}
 
        finally:
            browser.close()
 
def save_pdf(page, application_number):
    """
    Save the content of the page as a PDF file.
    """
    pdf_path = os.path.join(PDF_DIRECTORY, f"{application_number}.pdf")
    os.makedirs(PDF_DIRECTORY, exist_ok=True)
    page.pdf(path=pdf_path, format='A5', landscape=False)  
    print(f"PDF saved for application number {application_number} at {pdf_path}")
    return pdf_path
 
def safe_extract_text(locator, default='N/A', timeout=2):
    """
    Safely extract text from a Playwright locator with a timeout.
    """
    try:
        locator.wait_for(timeout=1000 * timeout)  
        return locator.inner_text().strip()
    except (TimeoutError, Exception):
        return default
 
def save_to_csv(data, filename):
    """
    Append data to a CSV file.
    """
    if data:
        file_exists = os.path.isfile(filename)
        with open(filename, mode='a', newline='', encoding='utf-8') as file:
            writer = csv.DictWriter(file, fieldnames=data.keys())
            if not file_exists:
                writer.writeheader()
            writer.writerow(data)
 
if __name__ == "__main__":
    input_file = r"C:\Users\Administrator\Desktop\SHR\old\Application Status\application1.xlsx"
    output_file = r"C:\Users\Administrator\Desktop\SHR\old\Application Status\appStatus.csv"
 
    print("Reading application numbers from Excel file...")
    df = pd.read_excel(input_file)
 
    if 'Application number' not in df.columns:
        print("Column 'Application number' not found in Excel file.")
        exit()
 
   
    for idx, row in df.iterrows():
        application_number = str(row['Application number']).strip()  # Adjust the column name
        print(f"\nProcessing application number: {application_number}")
 
        application_data = scrape_application_data(application_number)
        print(f"Saving extracted application data for application number {application_number}...")
        save_to_csv(application_data, output_file)
    in this remove the pdf saving part also in the csv we only need email corresponding to application no means only two fields