import os
from video_sweep.renamer import rename_and_move


def test_rename_and_move_movie(tmp_path):
    src = tmp_path / "source"
    tgt = tmp_path / "target"
    src.mkdir()
    tgt.mkdir()
    video = src / "movie.2023.mp4"
    video.write_text("")
    rename_and_move(str(video), "movie", str(tgt))
    assert os.path.exists(os.path.join(str(tgt), "movie [2023].mp4"))


def test_rename_and_move_series(tmp_path):
    src = tmp_path / "source"
    tgt = tmp_path / "target"
    src.mkdir()
    tgt.mkdir()
    video = src / "SeriesName (2014) - S04E01 - Other text.mkv"
    video.write_text("")
    rename_and_move(str(video), "series", str(tgt))
    expected_path = os.path.join(
        str(tgt), "SeriesName", "Season 4", "SeriesName S04E01.mkv"
    )
    assert os.path.exists(expected_path)


def test_rename_and_move_series_missing_episode(tmp_path, capsys):
    src = tmp_path / "source"
    tgt = tmp_path / "target"
    src.mkdir()
    tgt.mkdir()
    video = src / "SeriesName (2014) - Other text.mkv"
    video.write_text("")
    rename_and_move(str(video), "series", str(tgt))
    # Should not move file, should print warning
    out = capsys.readouterr().out
    assert "Warning: No episode code found" in out
    assert not os.path.exists(
        os.path.join(str(tgt), "SeriesName", "Season 4", "SeriesName S04E01.mkv")
    )


def test_rename_and_move_movie_no_year(tmp_path, capsys):
    src = tmp_path / "source"
    tgt = tmp_path / "target"
    src.mkdir()
    tgt.mkdir()
    video = src / "moviefile.mp4"
    video.write_text("")
    rename_and_move(str(video), "movie", str(tgt))
    out = capsys.readouterr().out
    assert "Warning: No year found" in out
    assert not any(f.name.startswith("moviefile") for f in tgt.iterdir())


def test_rename_and_move_target_exists(tmp_path, capsys):
    src = tmp_path / "source"
    tgt = tmp_path / "target"
    src.mkdir()
    tgt.mkdir()
    video = src / "movie.2023.mp4"
    video.write_text("")
    # Create target file first
    target = tgt / "movie [2023].mp4"
    target.write_text("already here")
    rename_and_move(str(video), "movie", str(tgt))
    out = capsys.readouterr().out
    assert "already exists" in out


def test_rename_and_move_dry_run(tmp_path, capsys):
    src = tmp_path / "source"
    tgt = tmp_path / "target"
    src.mkdir()
    tgt.mkdir()
    video = src / "movie.2023.mp4"
    video.write_text("")
    rename_and_move(str(video), "movie", str(tgt), dry_run=True)
    out = capsys.readouterr().out
    assert "Would move" in out
    assert not any(f.name.startswith("movie") for f in tgt.iterdir())


def test_rename_and_move_series_invalid(tmp_path, capsys):
    src = tmp_path / "source"
    tgt = tmp_path / "target"
    src.mkdir()
    tgt.mkdir()
    video = src / "Show SXXEYY.mkv"
    video.write_text("")
    rename_and_move(str(video), "series", str(tgt))
    out = capsys.readouterr().out
    assert "No episode code found" in out
    assert not any(f.is_file() for f in tgt.rglob("*"))
