#!/usr/bin/python3
"""Tests for slpkg.sbo.generate.SBoGenerate static methods using tmp_path."""

from pathlib import Path

from slpkg.sbo.generate import SBoGenerate

# ---------------------------------------------------------------------------
# read_short_description(path, name)
# Finds line "name: name <description>" in path/slack-desc.
# Returns the description part (everything after "name: "), stripped.
# ---------------------------------------------------------------------------


def test_read_short_description_found(tmp_path: Path) -> None:
    (tmp_path / "slack-desc").write_text(
        "# HOW TO EDIT THIS FILE:\nffmpeg: ffmpeg (A complete, cross-platform solution)\nffmpeg: \n", encoding="utf-8"
    )
    result = SBoGenerate.read_short_description(tmp_path, "ffmpeg")
    assert result == "ffmpeg (A complete, cross-platform solution)"


def test_read_short_description_different_name(tmp_path: Path) -> None:
    (tmp_path / "slack-desc").write_text("yasm: yasm (A modular assembler)\n", encoding="utf-8")
    result = SBoGenerate.read_short_description(tmp_path, "yasm")
    assert result == "yasm (A modular assembler)"


def test_read_short_description_no_slack_desc(tmp_path: Path) -> None:
    """File does not exist → empty string."""
    result = SBoGenerate.read_short_description(tmp_path, "ffmpeg")
    assert result == ""


def test_read_short_description_no_matching_line(tmp_path: Path) -> None:
    """slack-desc exists but no line matches pattern → empty string."""
    (tmp_path / "slack-desc").write_text("# This is just a comment\nother: other (something else)\n", encoding="utf-8")
    result = SBoGenerate.read_short_description(tmp_path, "ffmpeg")
    assert result == ""


def test_read_short_description_empty_file(tmp_path: Path) -> None:
    (tmp_path / "slack-desc").write_text("", encoding="utf-8")
    result = SBoGenerate.read_short_description(tmp_path, "ffmpeg")
    assert result == ""


# ---------------------------------------------------------------------------
# read_info_file(info_file, start, stop)
# Returns lines from info_file between start prefix (inclusive) and stop
# prefix (exclusive).
# ---------------------------------------------------------------------------

INFO_CONTENT = (
    'PRGNAM="ffmpeg"\n'
    'VERSION="7.1"\n'
    'HOMEPAGE="https://ffmpeg.org/"\n'
    'DOWNLOAD="https://example.com/ffmpeg.tar.gz"\n'
    'MD5SUM="abc123"\n'
    'DOWNLOAD_x86_64=""\n'
    'MD5SUM_x86_64=""\n'
    'REQUIRES="libvpx x264"\n'
    'MAINTAINER="John Doe"\n'
    'EMAIL="john@example.com"\n'
)


def test_read_info_file_single_line_section(tmp_path: Path) -> None:
    f = tmp_path / "ffmpeg.info"
    f.write_text(INFO_CONTENT, encoding="utf-8")
    result = SBoGenerate.read_info_file(f, "VERSION=", "HOMEPAGE=")
    assert result == ['VERSION="7.1"']


def test_read_info_file_multi_line_section(tmp_path: Path) -> None:
    # read_info_file returns info[begin:end] — all lines from 'start' up to
    # (not including) 'stop', so continuation lines are included.
    content = 'START="value1"\nSTART_CONT="value2"\nSTOP="end"\n'
    f = tmp_path / "test.info"
    f.write_text(content, encoding="utf-8")
    result = SBoGenerate.read_info_file(f, "START=", "STOP=")
    assert result == ['START="value1"', 'START_CONT="value2"']


def test_read_info_file_requires_section(tmp_path: Path) -> None:
    f = tmp_path / "ffmpeg.info"
    f.write_text(INFO_CONTENT, encoding="utf-8")
    result = SBoGenerate.read_info_file(f, "REQUIRES=", "MAINTAINER=")
    assert result == ['REQUIRES="libvpx x264"']


def test_read_info_file_nonexistent_returns_empty(tmp_path: Path) -> None:
    f = tmp_path / "missing.info"
    result = SBoGenerate.read_info_file(f, "VERSION=", "HOMEPAGE=")
    assert result == []


def test_read_info_file_start_not_found_returns_empty(tmp_path: Path) -> None:
    f = tmp_path / "ffmpeg.info"
    f.write_text(INFO_CONTENT, encoding="utf-8")
    # 'NOSUCHKEY=' doesn't exist → begin=0, end=0 (stop also may not be found)
    result = SBoGenerate.read_info_file(f, "NOSUCHKEY=", "VERSION=")
    # begin(0) >= end(1) → returns []
    assert isinstance(result, list)


def test_read_info_file_stop_not_found_reads_to_end(tmp_path: Path) -> None:
    content = 'VERSION="7.1"\nHOMEPAGE="https://ffmpeg.org/"\n'
    f = tmp_path / "test.info"
    f.write_text(content, encoding="utf-8")
    result = SBoGenerate.read_info_file(f, "VERSION=", "NOSUCHSTOP=")
    # stop not found → returns from begin to end of file
    assert 'VERSION="7.1"' in result
