"""
Iteration 20 regression tests
Modules/features: release-bundle push cleanup + worldcup import + LiveTV scale-mode wiring
"""

from pathlib import Path


ROOT = Path("/app")


def _read(rel_path: str) -> str:
    p = ROOT / rel_path
    assert p.exists(), f"Missing file: {p}"
    return p.read_text(encoding="utf-8")


def test_broadcast_view_has_push_cleanup_controls_and_version_filter_inputs():
    txt = _read("release-bundle/nadiplayer/admin_panel/views/broadcast.php")
    assert 'data-testid="push-cleanup-card"' in txt
    assert 'name="action" value="cleanup_old_push_tokens"' in txt
    assert 'name="keep_app_version"' in txt
    assert 'name="hard_reset_tokens" value="1"' in txt
    assert 'data-testid="cleanup-push-tokens-btn"' in txt
    assert 'name="target_app_version"' in txt


def test_admin_index_handles_cleanup_action_and_version_targeted_broadcast_queries():
    txt = _read("release-bundle/nadiplayer/admin_panel/index.php")
    assert "if ($action === 'cleanup_old_push_tokens')" in txt
    assert "DELETE FROM fcm_tokens" in txt
    assert "WHERE app_version IS NOT NULL AND app_version <> ?" in txt
    assert "SELECT device_id, token FROM fcm_tokens WHERE user_id = ? AND is_active = 1 AND app_version = ?" in txt
    assert "SELECT device_id, token FROM fcm_tokens WHERE is_active = 1 AND app_version = ?" in txt


def test_settings_view_exposes_push_enabled_and_push_target_app_version_controls():
    txt = _read("release-bundle/nadiplayer/admin_panel/views/settings.php")
    assert 'name="push_enabled" value="1"' in txt
    assert 'name="push_target_app_version"' in txt


def test_worldcup_view_has_footballdata_import_form():
    txt = _read("release-bundle/nadiplayer/admin_panel/views/worldcup.php")
    assert 'data-testid="worldcup-footballdata-import-card"' in txt
    assert 'name="action" value="import_worldcup_football_data"' in txt
    assert 'name="football_data_api_key"' in txt
    assert 'name="football_data_competition"' in txt
    assert 'data-testid="worldcup-footballdata-import-btn"' in txt


def test_admin_index_contains_footballdata_import_handler():
    txt = _read("release-bundle/nadiplayer/admin_panel/index.php")
    assert "if ($action === 'import_worldcup_football_data')" in txt
    assert "https://api.football-data.org/v4/competitions/" in txt
    assert "X-Auth-Token: " in txt


def test_sql_cleanup_script_resets_push_related_tables_without_users_or_xtream_deletes():
    txt = _read("release-bundle/nadiplayer/sql/reset_push_keep_users_xtream.sql")
    assert "DELETE FROM fcm_broadcasts;" in txt
    assert "DELETE FROM fcm_tokens;" in txt
    assert "DELETE FROM stats_heartbeats;" in txt
    assert "DELETE FROM installs;" in txt
    assert "DELETE FROM users" not in txt
    assert "DELETE FROM xtream" not in txt


def test_livetv_has_scale_toggle_buttons_and_passes_mode_to_players():
    txt = _read("frontend/src/pages/LiveTV.tsx")
    assert 'data-testid="livetv-scale-toggle"' in txt
    assert 'data-testid="livetv-scale-contain-btn"' in txt
    assert 'data-testid="livetv-scale-cover-btn"' in txt
    assert 'data-testid="livetv-scale-fill-btn"' in txt
    assert 'setLiveScaleMode("contain")' in txt
    assert 'setLiveScaleMode("cover")' in txt
    assert 'setLiveScaleMode("fill")' in txt
    assert "<VideoPlayer" in txt and "liveScaleMode={liveScaleMode}" in txt
    assert "<InlinePreviewPlayer" in txt and "liveScaleMode={liveScaleMode}" in txt


def test_video_player_applies_object_fit_from_live_scale_mode_for_live_streams():
    txt = _read("frontend/src/components/VideoPlayer.tsx")
    assert "liveScaleMode?: \"contain\" | \"cover\" | \"fill\";" in txt
    assert 'objectFit: live ? liveScaleMode : "contain"' in txt


def test_prior_worldcup_and_firebase_icon_related_assets_still_present():
    worldcup_view = _read("release-bundle/nadiplayer/admin_panel/views/worldcup.php")
    assert 'data-testid="worldcup-save-match-btn"' in worldcup_view
    assert 'data-testid="worldcup-import-json-btn"' in worldcup_view

    google_services = _read("frontend/android/app/google-services.json")
    assert '"project_id": "nadiplayer"' in google_services

    firebase_sa = _read("release-bundle/nadiplayer/api/secrets/firebase-service-account.json")
    assert '"type": "service_account"' in firebase_sa
    assert '"project_id": "nadiplayer"' in firebase_sa
