import { expect, type Locator, type Page } from '@playwright/test';
import { BasePage } from './BasePage';
import { ICustomerDetails } from '@business-logic/types/CustomerDetails';

export class ContactDetailsPage extends BasePage {
    readonly page: Page;
    issPageValue = 'contact-details';

    readonly appointmentInformationAlert: Locator;
    readonly changeZipCodeAlert: Locator;

    readonly streetAddressTextBox: Locator;
    readonly apartmentNumberTextBox: Locator;
    readonly cityTextBox: Locator;
    readonly stateTextBox: Locator;
    readonly zipCodeTextBox: Locator;
    readonly coveredLocationYesButton: Locator;
    readonly coveredLocationNoButton: Locator
    readonly notesTextBox: Locator;

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

        // Alerts
        this.appointmentInformationAlert = this.page.locator('[class*="widget-name-AppointmentInformationAlertWidget"]');
        this.changeZipCodeAlert = this.page.locator('[class*="widget-name-ChangeZipCodeAlertWidget"]');

        // Contact details form
        this.streetAddressTextBox = this.page.getByRole('textbox', { name: 'Street address' });
        this.apartmentNumberTextBox = this.page.getByRole('textbox', { name: 'Apartment number or letter (Optional)' });
        this.cityTextBox = this.page.getByRole('textbox', { name: 'City' });
        this.stateTextBox = this.page.getByRole('textbox', { name: 'state' });
        this.zipCodeTextBox = this.page.getByRole('textbox', { name: 'ZIP' });
        this.coveredLocationYesButton = this.page.locator('#vehicleProtectedQuestion-true');
        this.coveredLocationNoButton = this.page.locator('#vehicleProtectedQuestion-false');
        this.notesTextBox = this.page.getByRole('textbox', { name: 'technicianNotes' });
    }

    async validateAlertsAreVisible() {
        await expect(this.appointmentInformationAlert).toBeVisible();
        await this.appointmentInformationAlert.click(); // 
        await expect(this.appointmentInformationAlert).toContainText('Please have your vehicle keys or key card available, as your technician needs access inside the vehicle and may need to move your vehicle to complete service.');

        await expect(this.changeZipCodeAlert).toBeVisible();
        await this.changeZipCodeAlert.click();
        await expect(this.changeZipCodeAlert).toContainText('If your service ZIP code has changed, please go back and update it to ensure we have the correct information for your appointment.');
    }

    // TO-DO add method for when checkbox for same as policy address displays

    async fillContactDetails(customerDetails: ICustomerDetails) {
        await this.streetAddressTextBox.fill(customerDetails.address.street!);
        await this.cityTextBox.fill(customerDetails.address.city!);
    }

    async fillAlternateMobileStreetAndCity(alternateMobileStreetAddress: string, alternateMobileCity: string) {
        await this.streetAddressTextBox.fill(alternateMobileStreetAddress);
        await this.cityTextBox.fill(alternateMobileCity);
    }

    async fillNotes(notes?: string) {
        if (notes) {
            await this.notesTextBox.fill(notes);
        }
    }
}