import { Locator, Page, expect } from "@playwright/test";
import { CommonPage } from "../../utils/tinymce";
import { WebAllLocators } from "../locator";

export class AdminSellerPage {
    // Locators
    readonly locators: WebAllLocators;

    readonly page: Page;

    constructor(page: Page) {
        this.page = page;
        this.locators = new WebAllLocators(page);
    }

    // Methods
    async goto() {
        await this.page.goto("admin/marketplace/sellers");
    }

    async createSeller(
        email: string,
        slug: string,
        name: string,
        number: number,
        password: string
    ) {
        await this.locators.addSellerButton.click();
        await this.locators.sellerNameInput.fill(name);
        await this.locators.sellerPhoneNumber.fill(number.toString());
        await this.locators.emailInput.fill(email);
        await this.locators.slugInput.fill(slug);
        await this.locators.passwordInput.fill(password);
        await this.locators.confirmPasswordInput.fill(password);

        await this.locators.createSellerSaveButton.click();
        await this.locators.successToast.waitFor({
            state: "attached",
            timeout: 30000,
        });
        await expect(this.locators.successToast).toBeVisible();
        await this.locators.backButton.click();
    }

    async approveFirstSeller() {
        await this.locators.sellerCheckbox.click();
        await this.locators.selectActionDropdown.click();
        await this.locators.updateStatusOption.hover();
        await this.locators.approvedOption.click();
        await this.locators.agreeButton.click();
        await this.locators.page.waitForTimeout(2000);
        await expect(this.locators.updateSuccessToast).toBeVisible();
    }

    async updateSellerProfile(
        description: string,
        address: string,
        businessname: string
    ) {
        const commonPage = new CommonPage(this.locators.page);
        await this.locators.editButton.click();

        await commonPage.fillInTinymce(
            this.locators.sellerDescriptionIframe,
            description
        );
        await this.locators.businessName.fill(businessname);
        await this.locators.address.fill(address);
        await this.locators.addressCity.fill("Noida");
        await this.locators.addressCountry.selectOption("IN");
        await this.locators.addressState.selectOption("UP");
        await this.locators.addressZip.fill("201301");
        await this.locators.commissionToggle.click();
        await this.locators.commissionInput.fill("10");
        await this.locators.productType.selectOption("1");
        await this.locators.sellerMetaTitleInput.fill(businessname);
        await this.locators.sellerSaveButton.click();

        await this.locators.page.waitForTimeout(3000);
        await expect(this.locators.updateSuccessToast).toBeVisible();
    }
}
