import { test, expect } from '../../fixtures/test-base';
import { openMiniCart, findAddToCartButton, findProductOptions, selectOptionValue } from '../../support/cart-helpers';

test.describe('Basket', () => {
    test('BSK-001 @basket: Update quantity', async ({ page, addToCart }) => {
        await addToCart('cotton_knit_cardigan_swing');
        await openMiniCart(page);
        // Look for quantity controls in mini-cart
        const qtyInput = page.locator('input[type="number"], input[aria-label*="quantity" i], spinbutton').first();
        const hasQty = await qtyInput.isVisible().catch(() => false);
        test.skip(!hasQty, 'Quantity input not found in mini-cart');
    });

    test('BSK-002 @basket: Remove last item redirects to home', async ({ page, addToCart }) => {
        await addToCart('cotton_knit_cardigan_swing');
        await openMiniCart(page);
        const deleteBtn = page.locator('button[aria-label*="delete" i], button[aria-label*="remove" i]').first();
        test.skip(!(await deleteBtn.isVisible().catch(() => false)), 'Delete button not found');
        await deleteBtn.click();
        await page.waitForTimeout(3000);
        // Should redirect to home or show empty cart
        const isHome = page.url().endsWith('/') || page.url().endsWith('/en/');
        const emptyCart = await page.getByText(/empty|カートは空/i).isVisible().catch(() => false);
        expect(isHome || emptyCart, 'BSK-002: Should redirect to home or show empty cart').toBeTruthy();
    });

    test('BSK-003 @basket: Product variants shown in cart', async ({ page, goToProduct }) => {
        await goToProduct('sr1_running');
        // Select variant options
        const options = await findProductOptions(page);
        if (options) await selectOptionValue(page, options);
        const addBtn = await findAddToCartButton(page);
        test.skip(!addBtn, 'Add to cart not found');
        await addBtn!.click();
        await page.waitForTimeout(2000);
        await openMiniCart(page);
        // Check for variant info in cart (color/size text)
        const cartContent = page.locator('[class*="drawer" i], [class*="cart" i], [id*="basket"]').first();
        await expect(cartContent).toBeVisible({ timeout: 5000 });
    });
});
