"""
About Dialog Tests
- test_about_button_visible: The About TrainUs button is visible on the Settings page
- test_about_dialog_opens: Clicking the button opens the About modal
- test_about_dialog_shows_app_version: Application version is displayed and non-empty
- test_about_dialog_shows_db_version: Database version is displayed and non-empty
- test_about_dialog_shows_author: Author name is displayed
- test_about_dialog_shows_links: Repository and website links are present
- test_about_notice_visible: Notice text is displayed inline in the About dialog
- test_about_license_btn_opens_dialog: View License button opens the license modal on top of About
- test_about_dialog_closes: The modal can be closed with the close button
- test_about_dialog_labels_french: Labels are translated when language is French
"""
from playwright.sync_api import Page, expect


def _wait_for_db(page: Page, app_url: str):
    """Navigate and wait for the database to be ready."""
    page.goto(app_url)
    page.wait_for_function(
        "document.documentElement.getAttribute('data-db-ready') === 'true'",
        timeout=15000,
    )


def _go_to_settings(page: Page, app_url: str):
    """Navigate to the settings page and wait for DB."""
    _wait_for_db(page, app_url)
    page.click('a[href="#/settings"]')
    page.wait_for_selector('[data-testid="settings-language"]')


def _open_about(page: Page, app_url: str):
    """Navigate to settings and open the About dialog."""
    _go_to_settings(page, app_url)
    page.click('[data-testid="settings-about"]')
    page.wait_for_selector('[data-testid="modal-card"]')


def test_about_button_visible(page: Page, app_url: str):
    """The About TrainUs button is visible on the Settings page."""
    _go_to_settings(page, app_url)
    expect(page.locator('[data-testid="settings-about"]')).to_be_visible()


def test_about_dialog_opens(page: Page, app_url: str):
    """Clicking the About button opens the modal dialog."""
    _open_about(page, app_url)
    expect(page.locator('[data-testid="modal-card"]')).to_be_visible()
    expect(page.locator('[data-testid="modal-title"]')).to_be_visible()


def test_about_dialog_shows_app_version(page: Page, app_url: str):
    """Application version field is visible and contains a non-empty value."""
    _open_about(page, app_url)
    version_el = page.locator('[data-testid="about-app-version"]')
    expect(version_el).to_be_visible()
    version_text = version_el.inner_text().strip()
    assert version_text, "Application version should not be empty"
    # Version follows semver pattern (e.g. 1.0.0)
    import re
    assert re.match(r'^\d+\.\d+\.\d+', version_text), (
        f"Application version '{version_text}' does not look like a semver string"
    )


def test_about_dialog_shows_db_version(page: Page, app_url: str):
    """Database version field is visible and contains a non-empty numeric value."""
    _open_about(page, app_url)
    db_version_el = page.locator('[data-testid="about-db-version"]')
    expect(db_version_el).to_be_visible()
    db_version_text = db_version_el.inner_text().strip()
    assert db_version_text, "Database version should not be empty"
    assert db_version_text.isdigit(), (
        f"Database version '{db_version_text}' should be a numeric value"
    )


def test_about_dialog_shows_author(page: Page, app_url: str):
    """Author name 'David Florance' is displayed in the About dialog."""
    _open_about(page, app_url)
    expect(page.locator('[data-testid="modal-card"]')).to_contain_text("David Florance")


def test_about_dialog_shows_links(page: Page, app_url: str):
    """Repository and website links are present in the About dialog."""
    _open_about(page, app_url)
    expect(page.locator('[data-testid="about-repo-link"]')).to_be_visible()
    expect(page.locator('[data-testid="about-website-link"]')).to_be_visible()


def test_about_notice_visible(page: Page, app_url: str):
    """Notice text is displayed inline in the About dialog."""
    _open_about(page, app_url)
    notice = page.locator('[data-testid="about-notice"]')
    expect(notice).to_be_visible()
    expect(notice).to_contain_text("TrainUs")


def test_about_license_btn_opens_dialog(page: Page, app_url: str):
    """Clicking View License opens the license modal on top of the About dialog."""
    _open_about(page, app_url)
    btn = page.locator('[data-testid="about-license-btn"]')
    expect(btn).to_be_visible()
    expect(btn).to_be_enabled()
    btn.click()
    expect(page.locator('[data-testid="license-dialog-content"]')).to_be_visible()
    expect(page.locator('[data-testid="about-app-version"]')).to_be_visible()


def test_about_dialog_closes(page: Page, app_url: str):
    """The About modal can be dismissed with the close button."""
    _open_about(page, app_url)
    page.click('[data-testid="modal-close"]')
    expect(page.locator('[data-testid="modal-card"]')).not_to_be_visible()


def test_about_dialog_labels_french(page: Page, app_url: str):
    """About dialog labels are translated when the language is set to French."""
    _go_to_settings(page, app_url)

    # Switch to French
    page.select_option('[data-testid="settings-language"]', 'fr')
    page.wait_for_function(
        "window.__repos.settings.get('language').then(v => v === 'fr')",
        timeout=5000,
    )

    page.click('[data-testid="settings-about"]')
    page.wait_for_selector('[data-testid="modal-card"]')

    expect(page.locator('[data-testid="modal-title"]')).to_have_text("À propos de TrainUs")
    expect(page.locator('[data-testid="modal-card"]')).to_contain_text("Version de l'application")
    expect(page.locator('[data-testid="modal-card"]')).to_contain_text("Version de la base de données")

    # Reset to English
    page.click('[data-testid="modal-close"]')
    page.select_option('[data-testid="settings-language"]', 'en')
    page.wait_for_function(
        "window.__repos.settings.get('language').then(v => v === 'en')",
        timeout=5000,
    )
