from keble_helpers.ai.helpers import (
    PromptUrlExtractionResult,
    extract_urls_from_prompt,
    is_image_url,
)


def test_extract_urls_from_prompt_empty_prompt():
    result = extract_urls_from_prompt("")
    assert isinstance(result, PromptUrlExtractionResult)
    assert result.all_urls == []
    assert result.image_urls == []
    assert result.webpage_urls == []


def test_extract_urls_from_prompt_extracts_and_classifies_urls():
    prompt = (
        "Check https://example.com/docs. "
        "Image: https://cdn.example.com/cat.png, "
        "another image in markdown ![cat](https://img.example.com/cat.webp). "
        "Duplicate https://example.com/docs should be ignored."
    )
    result = extract_urls_from_prompt(prompt)

    assert result.all_urls == [
        "https://example.com/docs",
        "https://cdn.example.com/cat.png",
        "https://img.example.com/cat.webp",
    ]
    assert result.image_urls == [
        "https://cdn.example.com/cat.png",
        "https://img.example.com/cat.webp",
    ]
    assert result.webpage_urls == ["https://example.com/docs"]


def test_extract_urls_from_prompt_ignores_non_http_urls():
    prompt = (
        "ftp://example.com/file.jpg should be ignored. "
        "Also plain example.com should be ignored. "
        "But https://example.com/page should be extracted."
    )
    result = extract_urls_from_prompt(prompt)

    assert result.all_urls == ["https://example.com/page"]
    assert result.image_urls == []
    assert result.webpage_urls == ["https://example.com/page"]


def test_is_image_url():
    assert is_image_url("https://example.com/file.jpg") is True
    assert is_image_url("https://example.com/file.JPEG") is True
    assert is_image_url("https://example.com/file.txt") is False
    assert is_image_url("https://example.com/path") is False
