"""CS 149 Project 3, Part A: Unit Testing.

You are not required to test process_args(), load_json(), or main().
To prevent needless suffering, test_format_playlist() is provided.

Author: YOUR NAME
Version: THE DATE
"""

from copy import deepcopy
from sample_data import TAGS, TRACKS

from playlist import (
    clean_tags,
    format_tags,
    add_tag_to_set,
    clean_durations,
    tags_match,
    create_playlist,
    format_playlist,
)


def test_clean_tags():
    tags = clean_tags(TAGS, ["seen live", "seen dead"])
    assert tags == ["rock", "electronic", "alternative", "pop"]
    # TODO be sure to test the limit parameter


def test_format_tags():
    tags = ["a", "b", "c", "d"]
    assert format_tags(tags) == (
        "a                        b                        c                        \n"
        "d                        \n"
    )
    # TODO try testing with more and less output


def test_add_tag_to_set():
    tags = ["rock", "classical", "hip hop", "hip-hop"]
    s = set()
    add_tag_to_set("ROCK", tags, s)
    assert s == {"rock"}
    # TODO don't forget to test the hip hop cases


def test_clean_durations():
    tracks = [
        {"name": "A", "duration": "135000"},
    ]
    clean_durations(tracks)
    assert tracks == [
        {"name": "A", "duration": 135},
    ]
    # TODO be sure to test the default duration


def test_tags_match():
    assert tags_match(TRACKS[0], {"power pop", "old wave"})
    # TODO at least one test should be "assert not"


def test_create_playlist():
    tracks = deepcopy(TRACKS)
    tracks[0]["duration"] = 100
    tracks[1]["duration"] = 120
    tracks[1]["toptags"]["tag"].append({"name": "wonky", "url": ""})
    tracks[2]["duration"] = 140
    tracks[2]["toptags"]["tag"].append({"name": "hip hop", "url": ""})
    playlist = create_playlist(tracks, {"wonky"}, 5)
    assert playlist == [tracks[1]]
    # TODO try creating playlists of length 2 and 3


def test_format_playlist():
    tracks = deepcopy(TRACKS)
    tracks[0]["duration"] = 100
    tracks[1]["duration"] = 120
    tracks[2]["duration"] = 140
    assert format_playlist([]) == (
        "TRACK NAME                                        ARTIST NAME                   LENGTH (min)\n"
        "\n"
        "DETAILS\n"
    )
    assert format_playlist(tracks[1:2]) == (
        "TRACK NAME                                        ARTIST NAME                   LENGTH (min)\n"
        "BERGHAIN                                          ROSALÍA                       2.0\n"
        "\n"
        "DETAILS\n"
        '"Berghain" is the lead-single from the fourth studio album by ROSALÍA, titled  LUX. In the 13th of October 2025, the artist shared in her Substack account two images that showed a piano and voice scores with a capital title: BERGHAIN. It was the first musical sign of the new project and landmarked the beggining of a darker, spiritual and orchestral aesthetic.\n\nIn 26th of October, ROSALÍA officially announced through an Instagram video  the release of "BERGHAIN" in collaboration with Björk and Yves Tumor <a href="http://www.last.fm/music/ROSAL%C3%8DA/_/BERGHAIN">Read more on Last.fm</a>.\n\n'
    )
    assert format_playlist(tracks[1:3]) == (
        "TRACK NAME                                        ARTIST NAME                   LENGTH (min)\n"
        "BERGHAIN                                          ROSALÍA                       2.0\n"
        "Sexo, Violencia y Llantas                         ROSALÍA                       2.3\n"
        "\n"
        "DETAILS\n"
        '"Berghain" is the lead-single from the fourth studio album by ROSALÍA, titled  LUX. In the 13th of October 2025, the artist shared in her Substack account two images that showed a piano and voice scores with a capital title: BERGHAIN. It was the first musical sign of the new project and landmarked the beggining of a darker, spiritual and orchestral aesthetic.\n\nIn 26th of October, ROSALÍA officially announced through an Instagram video  the release of "BERGHAIN" in collaboration with Björk and Yves Tumor <a href="http://www.last.fm/music/ROSAL%C3%8DA/_/BERGHAIN">Read more on Last.fm</a>.\n\n'
    )
