"""Frontend E2E tests — manual add-asset form."""

import pytest


def _login(page):
    """Helper: login as admin."""
    page.locator("#loginUsername").fill("admin")
    page.locator("#loginPassword").fill("changeme")
    page.locator("button:has-text('Sign In')").click()
    page.wait_for_selector("#loginOverlay", state="hidden", timeout=5000)


@pytest.mark.frontend
def test_create_asset_manual_form(page, live_server):
    """Fill the manual add form and create an asset."""
    _login(page)

    # Navigate to Add Asset tab (default tab, but let's be explicit)
    page.locator(".tab-btn[data-tab='tabAddAsset']").click()
    page.wait_for_selector("#tabAddAsset.active", timeout=3000)

    # Switch to "Manual" mode
    page.locator(".mode-toggle[data-mode='manual']").click()
    page.wait_for_timeout(300)

    # Fill the form (using actual field IDs from index.html)
    page.locator("#manMachineId").fill("MANUAL-001")
    page.locator("#manName").fill("Manual Test Asset")
    page.locator("#manStatus").select_option("active")

    # Submit — button text is "Create Asset" but there are 3 on the page
    # (scan, OCR, manual). Scope to the manual mode section.
    page.locator("#addManualMode button:has-text('Create Asset')").click()

    # Should see success toast
    page.wait_for_selector("#toast.show", timeout=5000)
    toast = page.locator("#toast.show")
    toast_text = toast.inner_text().lower()
    assert "created" in toast_text or "added" in toast_text
