import { expect, type Locator, type Page } from '@playwright/test';
import { IClaimDetails, ICustomerDetails } from '@business-logic/types/CustomerDetails';
import { InsuranceBasePage } from './InsuranceBasePage';
import { STATE_ABBREVIATIONS } from '@business-logic/constants/StateAbbreviation';

export class CCPolicyInfoPage extends InsuranceBasePage {
    readonly policyNumber: Locator;
    readonly policyZip: Locator;
    readonly damageDate: Locator;
    readonly damageCause: Locator;
    readonly phoneNumber: Locator;
    readonly city: Locator;
    readonly state: Locator;
    readonly hasAdditionalDamage: Locator;
    readonly isRentalVehicle: Locator;
    readonly isOtherPartyResponsibleForCoverage: Locator;

    url = `${process.env['BASE_URL']!}/FixMyGlass/CCPolicyInfo.aspx*`;

    constructor(page: Page) {
        super(page);
        
        this.policyNumber = page.locator('#PolicyNumber');
        this.policyZip = page.locator('#PolicyZip');
        this.damageDate = page.locator('#LossDate');
        this.damageCause = page.locator('#LossCause');
        this.phoneNumber = page.locator('#PrimaryPhone');
        this.city = page.locator('#LossLocCity');
        this.state = page.locator('#LossLocState');
        this.hasAdditionalDamage = page.locator('#HasAdditionalDamage');
        this.isRentalVehicle = page.locator('#IsRentalVehicle');
        this.isOtherPartyResponsibleForCoverage = page.locator('#IsOtherPartyResponsibleForCoverage');
    }

    async hasCityInfo(): Promise<boolean> {
        await expect.soft(this.damageCause).toBeVisible();
        return this.city.isVisible();
    }

    private getStateAbbreviation(stateName: string): string {
        const abbreviation = STATE_ABBREVIATIONS[stateName];
        if (!abbreviation) {
            throw new Error(`State "${stateName}" not found in mapping`);
        }
        return abbreviation;
    }

    async populatePage(customerDetails: ICustomerDetails, claimDetails: IClaimDetails, isFillCityInfo: boolean): Promise<void> {
        try {
            await this.policyNumber.fill(claimDetails.policyNumber);
            
            await this.policyZip.fill(claimDetails.policyZip || customerDetails.address.postalCode);
            
            await this.damageDate.click();
            await this.damageDate.fill(claimDetails.damageDate);
            await this.damageCause.selectOption(claimDetails.damageCause);
            await this.damageCause.press('Tab');
            
            await this.phoneNumber.fill(customerDetails.phoneNumber);
            
            if (isFillCityInfo) {
                await this.city.fill(customerDetails.address.city);
            } 
            if (await this.state.isVisible()) {
                const stateAbbreviation = this.getStateAbbreviation(customerDetails.address.state);
                await this.state.selectOption({ value: stateAbbreviation });
            }
        } catch (error) {
            console.error('Error populating policy info page:', error);
            throw error;
        }
    }
}