import { test, expect } from '../../fixtures/test-base';
import {
    selectBankTransfer,
    hasBankTransferMinimumWarning,
} from '../../support/payment-helpers';
import {
    acceptTermsAndPlaceOrder,
    fillShippingAddress,
    TEST_SHIPPING_ADDRESS,
} from '../../support/checkout-helpers';
import { siteConfig } from '../../config/siteConfig';

test.describe('Bank Transfer Payments', () => {
    test('PAY-011 @payment @bank-transfer: Bank transfer below minimum shows info', async ({ page, addToCartAndCheckout }) => {
        test.setTimeout(120_000);

        // Add product #1 (Cardigan ¥27,500) - below 30,000 yen minimum for bank transfer
        await addToCartAndCheckout('cotton_knit_cardigan_swing');

        // Fill shipping address
        await fillShippingAddress(page, TEST_SHIPPING_ADDRESS);
        await page.waitForTimeout(2000);

        // Bank transfer should not be selectable, but informational text should be visible
        const bankTransferInfo = page.locator(
            ':text("bank transfer"), :text("銀行振込"), :text("30,000"), :text("¥30,000"), :text("３万")'
        ).first();
        const hasInfo = await bankTransferInfo.isVisible({ timeout: 10000 }).catch(() => false);

        expect(
            hasInfo,
            'Bank transfer informational text (e.g., "available for orders over ¥30,000") should be visible'
        ).toBeTruthy();

        await page.screenshot({ path: `test-results/pay-011-bank-transfer-info-${Date.now()}.png` });
    });

    test('PAY-012 @payment @bank-transfer: Bank transfer below minimum shows warning', async ({ page, addToCartAndCheckout }) => {
        test.setTimeout(120_000);

        // Add product #4 (Green Rose) - low value item, below 30,000 yen minimum
        await addToCartAndCheckout('green_rose_100g_tin');

        // Fill shipping address
        await fillShippingAddress(page, TEST_SHIPPING_ADDRESS);
        await page.waitForTimeout(2000);

        // Try to select bank transfer
        const selected = await selectBankTransfer(page);

        if (selected) {
            // Bank transfer was selectable - check for minimum warning
            const hasWarning = await hasBankTransferMinimumWarning(page);
            expect(
                hasWarning,
                'Warning about 30,000 yen minimum for bank transfer should be displayed'
            ).toBeTruthy();
        } else {
            // Bank transfer not available at all for low-value orders - also acceptable
            const paymentMethods = page.locator(
                'label:has-text("Bank Transfer"), label:has-text("銀行振込"), input[value*="bank" i]'
            );
            const bankTransferVisible = await paymentMethods.first().isVisible().catch(() => false);

            expect(
                bankTransferVisible,
                'Bank transfer should either show a warning or not be available for low-value orders'
            ).toBeFalsy();
        }

        await page.screenshot({ path: `test-results/pay-012-bank-minimum-${Date.now()}.png` });
    });
});
