from pathlib import Path

import pytest

from hockey_data.models import Team
from hockey_data.router import TeamDirectoryPayload
from hockey_data.service import normalise_team_color_hex


def test_team_model_has_persistent_color_column():
    assert "color_hex" in Team.__table__.columns.keys()
    column = Team.__table__.columns["color_hex"]
    assert column.nullable is False


def test_team_color_normalisation():
    assert normalise_team_color_hex("#282e66") == "#282E66"
    assert normalise_team_color_hex("e5cea8") == "#E5CEA8"
    assert normalise_team_color_hex("") == ""
    with pytest.raises(ValueError, match="#RRGGBB"):
        normalise_team_color_hex("#12345")


def test_team_payload_carries_color():
    payload = TeamDirectoryPayload(color_hex="#282E66")
    assert payload.color_hex == "#282E66"


def test_admin_team_form_contains_synced_color_picker():
    js = Path("hockey_data/static/admin-directories.js").read_text(encoding="utf-8")
    assert 'name="color_hex"' in js
    assert 'data-team-color-picker' in js
    assert 'color_hex: normalizedTeamColor' in js
