package JobSeeker;

import java.awt.AWTException;
import java.io.IOException;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import RevalERP_HRMS.basesetup.BaseSetup;
import RevalERP_HRMS.elements.Jobseeker_Elements;
import RevalERP_HRMS.excels.Excels;
import RevalERP_HRMS.utils.CommonUtils;
import jxl.Sheet;

public class JobSeeker extends BaseSetup {
    public Jobseeker_Elements elements;
    public Sheet sheet1;
	private WebElement submitButton;

    @BeforeClass
    public void setUp() throws IOException {
        driver = getDriver();
        CommonUtils.ModuleName = "JobSeekerProfile";
        driver.manage().window().maximize();
        driver.get(baseUrl);
        CommonUtils.Reports("JobSeekerProfile.html", "JobSeekerProfile");
        elements = PageFactory.initElements(driver, Jobseeker_Elements.class);
        sheet1 = CommonUtils.readExcelSheet(Excels.filepath2 + "SeekerData.xls", 0);
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        CommonUtils.login();
    }

    @Test(priority = 1)
    public void TC_001() throws IOException, AWTException {
        CommonUtils.HRMsMenu();
        try {
            CommonUtils.captureScreenshot_pass(driver, "TC_01", " TC_01 Verify Job seeker module is displaying in HRMS");
            CommonUtils.Thread_Sleep(2);
            JavascriptExecutor js1 = (JavascriptExecutor) driver;
            js1.executeScript("arguments[0].click();", elements.JobSeekerProfile);
        } catch (Exception e) {
            CommonUtils.captureScreenshot_fail(driver, "TC_01", "TC_01 Verify Job seeker module is not displaying in HRMS ");
            System.out.println("TC_01-Fail");
        }

        try {
            if (elements.JobSeekerProfile.isDisplayed()) {
                CommonUtils.captureScreenshot_pass(driver, "TC_02", "TC_02 check the user can able to click the JobSeeker profile ");
                System.out.println("TC_02-Pass");
            }
        } catch (Exception e) {
            CommonUtils.captureScreenshot_fail(driver, "TC_02", "TC_02 check the user can able to click the JobSeeker profile not");
            System.out.println("TC_02-Fail");
        }
    }

    @Test(priority = 2)
    public void TC_004_ValidateJobSeekerFields() throws IOException {
        CommonUtils.Thread_Sleep(2);
        try {
            if (elements.JobSeekerProfile.isDisplayed()
                    && elements.FromDate.isDisplayed() && elements.ToDate.isDisplayed()
                    && elements.TechnologyDropdown.isDisplayed()
                    && elements.Status.isDisplayed()&& submitButton.isDisplayed()) {
                CommonUtils.captureScreenshot_pass(driver, "TC_03","TC_03 Verify mentioned fields are displaying in Job Seeker profile List page ");
                                      System.out.println("TC_03-Pass");
               CommonUtils.captureScreenshot_pass(driver, "TC_04","TC_04 Verify once we applied for the job from our portal, applied candidate detaiils are binding in Job seeker module  ");
                                      System.out.println("TC_04-Pass");                    
            }
        } catch (Exception e) {
            CommonUtils.captureScreenshot_fail(driver, "TC_03","Verify mentioned fields are not displaying in Job Seeker profile List page");               
            System.out.println("TC_03-Fail");
            CommonUtils.captureScreenshot_fail(driver, "TC_04"," TC_04 Verify once we applied for the job from our portal, applied candidate detaiils are not  binding in Job seeker module ");               
            System.out.println("TC_04-Fail");
        }
    }

    @Test(priority = 3)
    public void TC_006_DownloadResumeValidation() throws IOException {
        try {
            elements.Action.click();
            elements.ActionEdit.click();
            try {
            	if(elements.Action.isDisplayed()&&elements.ActionEdit.isDisplayed()) {
            		CommonUtils.captureScreenshot_pass(driver, "TC-05", "TC_05 check the user can able to click Action(…)/Edit option  ");
    				System.out.println("TC-05-Passed");
    			}

    		} catch (Exception e) {
    			CommonUtils.captureScreenshot_fail(driver, "TC-05", " TC_05 check the user can not able to click Action(…)/Edit option  ");
    			System.out.println("TC-05-Failed");
    		} 
	
            
            
            		try {
            			if(elements.FirstName.isDisplayed()&&elements.LastName.isDisplayed()&&elements.Mobile.isDisplayed()&&
            				elements.Email.isDisplayed()&&elements.AppliedDate.isDisplayed()&&elements.Technology.isDisplayed()&& elements.Message.isDisplayed()&&elements.Resume.isDisplayed()) {
            				CommonUtils.captureScreenshot_pass(driver, "TC-06", "TC_06 Verify after click on Candidate name, Mentioned details displaying ");
            				System.out.println("TC-06-Passed");
            				CommonUtils.captureScreenshot_pass(driver, "TC-07", "TC_07 Verify all the details are correctly binding and disaplying ");
            				System.out.println("TC-07-Passed");
            			}

            		} catch (Exception e) {
            			CommonUtils.captureScreenshot_pass(driver, "TC-06", "TC_06 Verify after click on Candidate name, Mentioned details not displaying ");
        				System.out.println("TC-06-Failed");
            			CommonUtils.captureScreenshot_fail(driver, "TC-07", " TC_07 Verify all the details are not  correctly binding and disaplying ");
            			System.out.println("TC-07-Failed");
            		} 



           
            elements.file.click();
            CommonUtils.captureScreenshot_pass(driver, "TC_08", " TC_08 Verify are we able to download the  resume");
            System.out.println("TC_08-Pass");
        } catch (Exception e) {
            CommonUtils.captureScreenshot_fail(driver, "TC_08", "Verify are we able not to download the  resume ");
            System.out.println("TC_08-Fail: " + e.getMessage());
        }
    
            elements.Radiobtn.click();
            elements.Proceed.click();
            elements.Rejected.click();
            elements.Save.click();
}


  
    @Test(priority = 4)
    public void TC_007_CompareMobileNumbers() throws IOException {
        Map<String, String> expectedMobileMap = new HashMap<>();
        int rows = sheet1.getRows();
        for (int i = 1; i < rows; i++) {
            String name = sheet1.getCell(0, i).getContents().trim();
            String mobile = sheet1.getCell(1, i).getContents().trim();
            expectedMobileMap.put(name, mobile);
        }

        List<WebElement> cards = driver.findElements(By.xpath("//div[@class='card py-2 px-0 rounded-0']"));
        Assert.assertTrue(cards.size() > 0, "❌ No mobile found!");

        for (int i = 0; i < cards.size(); i++) {
            try {
                cards = driver.findElements(By.xpath("//div[@class='card py-2 px-0 rounded-0']")); // re-fetch to avoid stale
                WebElement card = cards.get(i);
                String name = card.findElement(By.xpath(".//*[contains(@class, 'name')]")).getText().trim();
                String actualMobile = card.findElement(By.xpath("//*id='mobile']")).getText().trim();

                if (expectedMobileMap.containsKey(name)) {
                    String expectedMobile = expectedMobileMap.get(name);
                    System.out.println("✅ Checking " + name + ": Expected = " + expectedMobile + ", Actual = " + actualMobile);
                    Assert.assertEquals(actualMobile, expectedMobile, "❌ Mobile number mismatch for " + name);
                } else {
                	CommonUtils.captureScreenshot_pass(driver, "TC-09", "TC_09 Verify that all candidate profiles are visible by default in Job Seeker Mobile with applying any filters ");
    				System.out.println("TC-09-Passed");
                    System.out.println("⚠️ No expected data found for: " + name);
                }
            } catch (Exception e) {
            	CommonUtils.captureScreenshot_pass(driver, "TC-09", "TC_09 Verify that all candidate profiles are visible by default in Job Seeker Mobile without applying any filters ");
				System.out.println("TC-09-Failed");
				
				
            }
        }
    }

    @Test(priority = 5)
    public void TC_008_SearchFieldValidation() {
        try {
            elements.Search.click();
            elements.FromDate.click();
            elements.ToDate.click();
            elements.TechnologyDropdown.click();
            elements.TechnologyDropdownOption.click();
            elements.Status.click();

            System.out.printf("✅ TC_09 -  Verify that the user is able to view job seekers after applying filters.", "Navigate to the Candidate Profiles section Apply specific filters (e.g., Search word, From and To Date, TechnplogyDropdown,statusDropdown, Click on the submit Filter");
        } catch (Exception e) {
            System.out.println("❌ TC_09 -Verify that the user is not able to view job seekers after applying filters" + e.getMessage());
        }
    }

    @AfterClass
    public void closeTC() {
        if (driver != null) {
            // driver.quit();
        }
    }
}
