"""Tests for aaiclick.data.scope helpers."""

from __future__ import annotations

import pytest

from aaiclick.data.scope import (
    JOB_SCOPED_RE,
    TEMP_NAMED_RE,
    is_persistent_table,
    make_scoped_table_name,
    name_from_table,
    scope_of,
)


def test_scope_of_temp():
    assert scope_of("t_123") == "temp"
    assert scope_of("t_999999999999") == "temp"


def test_scope_of_temp_named():
    assert scope_of("t_orders_123") == "temp_named"
    assert scope_of("t_my_table_999999999999") == "temp_named"
    assert scope_of("t_CamelCase_42") == "temp_named"


def test_scope_of_global():
    assert scope_of("p_foo") == "global"
    assert scope_of("p_user_catalog") == "global"


def test_scope_of_job():
    assert scope_of("j_42_bar") == "job"
    assert scope_of("j_1234567890_my_table") == "job"


def test_scope_of_miscellaneous_prefixes_are_temp():
    # Regex requires digits + underscore after 'j_' to qualify as job-scoped.
    assert scope_of("j_nodigit_foo") == "temp"
    assert scope_of("j_42nopost") == "temp"
    assert scope_of("other_table") == "temp"


def test_is_persistent_table():
    assert is_persistent_table("p_foo") is True
    assert is_persistent_table("j_1_bar") is True
    assert is_persistent_table("t_100") is False
    assert is_persistent_table("t_named_100") is False
    assert is_persistent_table("anything_else") is False


def test_job_scoped_regex():
    assert JOB_SCOPED_RE.match("j_1_a")
    assert JOB_SCOPED_RE.match("j_987654321_table_name")
    assert not JOB_SCOPED_RE.match("j_bar")
    assert not JOB_SCOPED_RE.match("p_1_x")


def test_temp_named_regex():
    assert TEMP_NAMED_RE.match("t_foo_42")
    assert TEMP_NAMED_RE.match("t_my_table_987654321")
    # Trailing must be digits
    assert not TEMP_NAMED_RE.match("t_foo_bar")
    # Pure digits is plain temp, not temp_named
    assert not TEMP_NAMED_RE.match("t_42")
    # Name must start with letter/underscore
    assert not TEMP_NAMED_RE.match("t_1foo_42")


def test_make_scoped_table_name_global():
    assert make_scoped_table_name("global", "foo") == "p_foo"


def test_make_scoped_table_name_job():
    assert make_scoped_table_name("job", "foo", job_id=42) == "j_42_foo"


def test_make_scoped_table_name_job_requires_job_id():
    with pytest.raises(ValueError, match="scope='job' requires a job_id"):
        make_scoped_table_name("job", "foo")


def test_make_scoped_table_name_temp_named():
    assert make_scoped_table_name("temp_named", "foo", snowid=42) == "t_foo_42"


def test_make_scoped_table_name_temp_named_requires_snowid():
    with pytest.raises(ValueError, match="scope='temp_named' requires a snowid"):
        make_scoped_table_name("temp_named", "foo")


def test_name_from_table_global():
    assert name_from_table("p_orders") == "orders"
    assert name_from_table("p_user_catalog") == "user_catalog"


def test_name_from_table_job():
    assert name_from_table("j_12345_staging") == "staging"
    assert name_from_table("j_12345_multi_part_name") == "multi_part_name"


def test_name_from_table_temp_named():
    assert name_from_table("t_orders_42") == "orders"
    assert name_from_table("t_my_table_999999999999") == "my_table"


def test_name_from_table_temp_falls_back_to_table():
    assert name_from_table("t_9999999999") == "t_9999999999"
