from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
JS = (ROOT / "hockey_data" / "static" / "admin-directories.js").read_text(encoding="utf-8")


def test_mapping_context_and_sql_are_top_level_admin_tabs():
    assert 'data-directory-section="mapping_context"' in JS
    assert '>Переменные</button>' in JS
    assert 'data-directory-section="mapping_sql"' in JS
    assert '>SQL источники</button>' in JS


def test_mapping_load_is_resilient_per_endpoint():
    assert 'safe("variables", "/api/hockey/admin/mapping-context/variables"' in JS
    assert 'safe("sources", "/api/hockey/admin/mapping-data/sources"' in JS
    assert 'Часть Mapping API недоступна' in JS


def test_visual_mapping_has_sql_table_cell_picker_and_searchable_number_sorted_inputs():
    assert 'data-map-table-source' in JS
    assert 'data-map-table-row-search' in JS
    assert '.row.${row.index}.' in JS
    assert 'data-map-input-search' in JS
    assert 'mappingFilteredInputs' in JS
    assert 'const inputs = Array.isArray(profile?.inventory?.inputs) ? [...profile.inventory.inputs] : [];' in JS
    assert 'Number(left?.number)' in JS
    assert 'leftRank - rightRank' in JS


def test_visual_mapping_has_explicit_sql_cell_dropdowns_and_apply_input():
    assert 'data-map-quick-table-source' in JS
    assert 'data-map-quick-table-row' in JS
    assert 'data-map-quick-table-column' in JS
    assert 'data-map-quick-cell-link' in JS
    assert 'Источник → строка → столбец' in JS
    assert 'data-map-apply-input' in JS
    assert '>Применить Input</button>' in JS
    assert '>Тест поля</button>' in JS
    assert '/api/hockey/admin/vmix-mapping/test-batch' in JS


def test_sql_preview_preserves_unsaved_editor_draft():
    assert 'mappingSqlDraft: null' in JS
    assert 'Preview must never reset an unsaved editor' in JS
    assert 'state.mappingSqlDraft = {' in JS
    assert 'const liveDraft = state.mappingSqlDraft' in JS


def test_sql_editor_has_postgresql_help_and_language_auto_mapping_hint():
    assert 'Подсказки PostgreSQL' in JS
    assert 'data-sql-snippet' in JS
    assert 'name_RUS' in JS and 'name_ENG' in JS
    assert 'Автовыбор языка' in JS
    assert 'hockey-map-lang-auto' in JS


def test_sql_preview_does_not_hide_rows_after_eight():
    js = (ROOT / "hockey_data" / "static" / "admin-directories.js").read_text(encoding="utf-8")
    css = (ROOT / "hockey_data" / "static" / "admin-directories.css").read_text(encoding="utf-8")
    assert ".slice(0, 8)" not in js
    assert "(preview.rows || []).map" in js
    assert "max-height:420px" in css
    assert "position:sticky" in css


def test_mapping_has_series_autolink_and_visible_sql_table_grid():
    js = (ROOT / "hockey_data" / "static" / "admin-directories.js").read_text(encoding="utf-8")
    css = (ROOT / "hockey_data" / "static" / "admin-directories.css").read_text(encoding="utf-8")
    assert "mappingFieldSequence" in js
    assert "mappingSeriesPlan" in js
    assert "data-map-series-column" in js
    assert "data-map-series-row" in js
    assert "Автосвязать столбец" in js
    assert "Размножить строку" in js
    assert "tableGridHeight" in js
    assert "min-height:230px" in css
    assert "max-height:430px" in css


def test_mapping_vmix_fields_have_text_image_color_filters_and_hide_linked_toggle():
    js = (ROOT / "hockey_data" / "static" / "admin-directories.js").read_text(encoding="utf-8")
    assert 'data-map-field-filter="text"' in js
    assert 'data-map-field-filter="image"' in js
    assert 'data-map-field-filter="color"' in js
    assert 'data-map-hide-linked' in js
    assert 'Скрывать связанные' in js
    assert 'name.endsWith(".text")' in js
    assert 'name.endsWith(".source")' in js
    assert 'name.endsWith(".color")' in js
