import { expect, type Locator, type Page } from '@playwright/test';
import { BasePage } from './BasePage';
import { IClaimDetails, ICustomerDetails } from '@business-logic/types/CustomerDetails';
import { defaultTestData, ITestData, VehicleDamage } from 'safelite-playwright-core';

export class CoverageStatementPage extends BasePage {
    readonly page: Page;
    readonly cancelMyClaimButton: Locator;
    readonly cancelMyClaimConfirm: Locator;
    readonly deductibleAmount: Locator;
    readonly noDeductibleRepairText: Locator; // For essential flow when there is no deductible and repair is covered
    readonly youPayYourDeductibleText: Locator; 
    readonly verifiedCoverageHeader: Locator; // For essential flow when coverage is verified
    readonly headerForITAC: Locator; // For ITAC when coverage is found
    readonly headerForNoComp: Locator; // For NoComp when coverage is found
    readonly unverifiedCoverageHeader: Locator; // For essential flows when verifying deductible
    readonly oemBullet: Locator; // For OEM bullet when OEM endorsement is present
    readonly continueSchedulingButton: Locator;     // For ITAC/NoComp
    readonly continueButton: Locator;     // For essential flows
    issPageValue = 'coverage-statement';

    constructor(page: Page) {
        super(page);
        this.page = page;

        this.cancelMyClaimButton = this.page.getByRole('link', { name: "No, I want to cancel" }); // seen for ITAC/NoComp flows
        this.cancelMyClaimConfirm = this.page.locator('div.text-center', { hasText: "No, I want to cancel" }); // on modal for cancel claim
        
        this.deductibleAmount = this.page.locator('.cost-underline');
        this.noDeductibleRepairText = this.page.getByText('According to your policy, there is no deductible for a repair.'); // no deductible for repair text
        this.youPayYourDeductibleText = this.page.getByText('You pay your deductible:'); // no deductible for replacement text
        this.verifiedCoverageHeader = this.page.getByRole('heading', { name: 'We have verified your coverage' });
        this.unverifiedCoverageHeader = this.page.getByRole('heading', { level: 5, name: "We’re verifying your coverage" });
        this.headerForITAC = this.page.getByRole('heading', { name: 'Good news! Safelite\'s price is lower than your deductible' });
        this.headerForNoComp = this.page.getByRole('heading', { name: 'Looks like your policy doesn\'t include comprehensive coverage.\nSafelite can still get you fixed up and safely back on the road.' });
        this.oemBullet = this.page.getByText('OEM (Original Equipment Manufacturer) glass replacement is included under your policy and will be installed in your vehicle.');

        this.continueSchedulingButton = page.getByRole('button', { name: 'Continue scheduling' }); // continue button for ITAC/NoComp flow
        this.continueButton = page.getByRole('button', { name: 'Continue' }); // continue button for essential flow
    }

    async scheduleOnline(){ 
        await this.continueSchedulingButton.click();
    }

    async cancelMyClaim(){
        await this.cancelMyClaimButton.click();
        await this.cancelMyClaimConfirm.click();
    }

    async handleRepairWithNoDeductibleFlow() {

        if (VehicleDamage.WindshieldOneChip || VehicleDamage.WindshieldTwoChips || VehicleDamage.WindshieldThreeChips) {
        await expect(this.verifiedCoverageHeader).toBeVisible();
        await expect(this.noDeductibleRepairText).toBeVisible();
        }
    }

    async handleReplacementOrRepairWithNoDeductibleFlow(claimDetails: IClaimDetails, VehicleDamage){
        const noDeductibleFlow = claimDetails.policyDeductible === 0;

        if (noDeductibleFlow && VehicleDamage === VehicleDamage.WindshieldOneChip || VehicleDamage.WindshieldTwoChips || VehicleDamage.WindshieldThreeChips) {
        
        // await expect(this.noDeductibleRepairText).toBeVisible();
        await this.continueButton.click();
            
        } else {
        await expect(this.youPayYourDeductibleText).toBeVisible();
        await expect(this.deductibleAmount).toContainText(`$0`, {timeout: 60000});
        await this.continueButton.click();
        }
    }

    async handleReplacementWithDeductibleFlow(claimDetails: IClaimDetails){
        const replaceWithDeductibleFlow = claimDetails.policyDeductible! > 0 && claimDetails.policyDeductible !== 9999;

        if (replaceWithDeductibleFlow) {
        await expect(this.youPayYourDeductibleText).toBeVisible();
        await expect(this.deductibleAmount).toContainText(`$${claimDetails.policyDeductible?.toLocaleString()}`, {timeout: 60000});

        await this.continueButton.click();
        }
    }


    async handleUnverifiedFlow(claimDetails: IClaimDetails){
            const unVerifiedCustomer = claimDetails.policyDeductible === -1; // if deductible is -1, we treat it as unverified flow

            if (unVerifiedCustomer) {
            await expect(this.unverifiedCoverageHeader).toBeVisible();

            await this.continueButton.click();
        }
    }

    async handleUnverifiedPolicyAfterVehicleLookupFlow(){
        await expect(this.unverifiedCoverageHeader).toBeVisible();
        await this.continueButton.click();
    }

    async handleITACFlow(){
        await expect(this.headerForITAC).toBeVisible(); // verify ITAC header is visible

        await this.continueSchedulingButton.click(); // happy path to scheduling for ITAC
     }

    async handleNoCompFlow(){
        await expect(this.headerForNoComp).toBeVisible(); // verify NoComp header is visible

        await this.continueSchedulingButton.click(); // happy path to scheduling for NoComp
     }

    async validateOEMBullet(){
            await expect(this.oemBullet).toBeVisible();
    } 

    }
