package model

import (
    "testing"
    "time"
)

func Test_Release_DescriptionHTML(t *testing.T) {
    release := Release{
        Description: "this is\na test\n<strong>description!</strong>",
    }

    // descriptions are set by privileged users,
    // so we'll allow HTML injection here
    want := "this is<br>a test<br><strong>description!</strong>"
    got := release.GetDescriptionHTML()
    if want != string(got) {
        t.Errorf(`release description incorrectly formatted (want "%s", got "%s")`, want, got)
    }
}

func Test_Release_ReleaseDate(t *testing.T) {
    release := Release{
        ReleaseDate: time.Date(2025, time.July, 26, 16, 0, 0, 0, time.UTC),
    }

    want := "2025-07-26T16:00"
    got := release.TextReleaseDate()
    if want != got {
        t.Errorf(`release date incorrectly formatted (want "%s", got "%s")`, want, got)
    }

    want = "26 July 2025"
    got = release.PrintReleaseDate()
    if want != got {
        t.Errorf(`release date (print) incorrectly formatted (want "%s", got "%s")`, want, got)
    }
}

func Test_Release_Artwork(t *testing.T) {
    want := "testartwork.png"
    release := Release{ Artwork: want }

    got := release.GetArtwork()
    if want != got {
        t.Errorf(`correct value not returned when artwork is populated (want "%s", got "%s")`, want, got)
    }

    release = Release{}

    want = "/img/default-cover-art.png" 
    got = release.GetArtwork()
    if want != got {
        t.Errorf(`default value not returned when artwork is empty (want "%s", got "%s")`, want, got)
    }
}

func Test_Release_IsSingle(t *testing.T) {
    release := Release{
        Tracks: []*Track{},
    }

    if release.IsSingle() {
        t.Errorf("IsSingle() == true when no tracks are present")
    }

    release.Tracks = append(release.Tracks, &Track{})
    if !release.IsSingle() {
        t.Errorf("IsSingle() == false when one track is present")
    }

    release.Tracks = append(release.Tracks, &Track{})
    if release.IsSingle() {
        t.Errorf("IsSingle() == true when >1 tracks are present")
    }
}

func Test_Release_IsReleased(t *testing.T) {
    release := Release {
        ReleaseDate: time.Now(),
    }

    if !release.IsReleased() {
        t.Errorf("IsRelease() == false when release date in the past")
    }

    release.ReleaseDate = time.Now().Add(time.Hour)
    if release.IsReleased() {
        t.Errorf("IsRelease() == true when release date in the future")
    }
}

func Test_Release_PrintArtists(t *testing.T) {
    artist1 := "ari melody"
    artist2 := "aridoodle"
    artist3 := "idk"
    artist4 := "guest"
    
    release := Release {
        Credits: []*Credit{
            { Artist: Artist{ Name: artist1 }, Primary: true },
            { Artist: Artist{ Name: artist2 }, Primary: true },
            { Artist: Artist{ Name: artist3 }, Primary: false },
            { Artist: Artist{ Name: artist4 }, Primary: true },
        },
    }

    {
        want := []string{ artist1, artist2, artist4 }
        got := release.GetUniqueArtistNames(true)
        if len(want) != len(got) {
            t.Errorf(`len(GetUniqueArtistNames) (primary only) == %d, want %d`, len(got), len(want))
        }
        for i := range got {
            if want[i] != got[i] {
                t.Errorf(`GetUniqueArtistNames[%d] (primary only) == %s, want %s`, i, got[i], want[i])
            }
        }

        want = []string{ artist1, artist2, artist3, artist4 }
        got = release.GetUniqueArtistNames(false)
        if len(want) != len(got) {
            t.Errorf(`len(GetUniqueArtistNames) == %d, want %d`, len(got), len(want))
        }
        for i := range got {
            if want[i] != got[i] {
                t.Errorf(`GetUniqueArtistNames[%d] == %s, want %s`, i, got[i], want[i])
            }
        }
    }

    {
        want := "ari melody, aridoodle & guest"
        got := release.PrintArtists(true, true)
        if want != got {
            t.Errorf(`PrintArtists (primary only, ampersand) == "%s", want "%s"`, want, got)
        }

        want = "ari melody, aridoodle, guest"
        got = release.PrintArtists(true, false)
        if want != got {
            t.Errorf(`PrintArtists (primary only) == "%s", want "%s"`, want, got)
        }

        want = "ari melody, aridoodle, idk & guest"
        got = release.PrintArtists(false, true)
        if want != got {
            t.Errorf(`PrintArtists (all, ampersand) == "%s", want "%s"`, want, got)
        }

        want = "ari melody, aridoodle, idk, guest"
        got = release.PrintArtists(false, false)
        if want != got {
            t.Errorf(`PrintArtists (all) == "%s", want "%s"`, want, got)
        }
    }
}
