import pandas as pd

from fmatch.preprocessing.core.pre_blocking import BlockingKeyGenerator


def _blocking_keys(df: pd.DataFrame, column: str, key_length: int = 3):
    out_df, _ = BlockingKeyGenerator().generate_blocking_keys(df, column, key_length)
    return out_df["_active_block_key_"].tolist()


def test_domain_like_website_values_normalize_to_matching_keys():
    src = pd.DataFrame(
        {
            "website": [
                "https://www.acme.com/path",
                "http://beta.io/about",
                "example.org/team",
            ]
        }
    )
    ref = pd.DataFrame({"Website": ["acme.com", "www.beta.io", "https://example.org"]})

    src_keys = _blocking_keys(src, "website")
    ref_keys = _blocking_keys(ref, "Website")

    assert src_keys == ["acm", "bet", "exa"]
    assert ref_keys == ["acm", "bet", "exa"]


def test_domain_like_email_values_use_email_domain_for_key():
    src = pd.DataFrame({"email": ["Sales@Acme.com", "ops@beta.io"]})
    ref = pd.DataFrame({"Email": ["info@acme.com", "hello@beta.io"]})

    src_keys = _blocking_keys(src, "email")
    ref_keys = _blocking_keys(ref, "Email")

    assert src_keys == ["acm", "bet"]
    assert ref_keys == ["acm", "bet"]


def test_non_domain_columns_keep_existing_prefix_behavior():
    df = pd.DataFrame({"Company": ["Acme Inc.", "Beta LLC"]})
    keys = _blocking_keys(df, "Company")
    assert keys == ["acm", "bet"]

