package api

import (
	"encoding/json"
	"testing"
	"time"

	"github.com/ponzischeme89/memby/server/internal/radarr"
)

func TestBuildRadarrRowUsesDigitalReleasesAndEstimatedCinemaFallbackInFiveDayWindow(t *testing.T) {
	location := time.FixedZone("NZST", 12*60*60)
	now := time.Date(2026, 7, 30, 9, 0, 0, 0, location)
	digitalToday := time.Date(2026, 7, 30, 0, 0, 0, 0, location).UTC()
	digitalSunday := time.Date(2026, 8, 2, 0, 0, 0, 0, location).UTC()
	outside := time.Date(2026, 8, 4, 0, 0, 0, 0, location).UTC()
	theatricalOnly := time.Date(2026, 7, 1, 0, 0, 0, 0, location).UTC()
	oldDigital := time.Date(1993, 4, 9, 0, 0, 0, 0, location).UTC()
	modernRerelease := time.Date(2026, 7, 2, 0, 0, 0, 0, location).UTC()

	row, err := buildRadarrRow([]radarr.Movie{
		{ID: 1, Title: "Today", DigitalRelease: &digitalToday, Monitored: true},
		{ID: 2, Title: "Sunday", DigitalRelease: &digitalSunday, Monitored: true},
		{ID: 3, Title: "Outside", DigitalRelease: &outside, Monitored: true},
		{ID: 4, Title: "Cinema Only", InCinemas: &theatricalOnly, Monitored: true},
		{ID: 5, Title: "Old Digital Release", Year: 1993, DigitalRelease: &oldDigital, InCinemas: &modernRerelease, Monitored: true},
	}, now, location)
	if err != nil {
		t.Fatal(err)
	}
	if row.ID != "radarr-upcoming-movies" || row.Kind != "movie-schedule" ||
		row.Title != "Upcoming Movie releases" || len(row.Items) != 3 {
		t.Fatalf("unexpected row: %+v", row)
	}
	var first, second radarrScheduleItem
	if err := json.Unmarshal(row.Items[0], &first); err != nil {
		t.Fatal(err)
	}
	if err := json.Unmarshal(row.Items[1], &second); err != nil {
		t.Fatal(err)
	}
	if first.ID != "radarr:1" || first.MembyAirLabel != "Digital release today" {
		t.Fatalf("unexpected first item: %+v", first)
	}
	if second.ID != "radarr:4" || second.MembyAirLabel != "Estimated digital release tomorrow" ||
		second.MembyAvailabilityText != "Estimated digital release" {
		t.Fatalf("unexpected second item: %+v", second)
	}
	var third radarrScheduleItem
	if err := json.Unmarshal(row.Items[2], &third); err != nil {
		t.Fatal(err)
	}
	if third.ID != "radarr:2" || third.MembyAirLabel != "Digital release Sunday" {
		t.Fatalf("unexpected third item: %+v", third)
	}
}

func TestRadarrScheduleItemIncludesArtworkAndDownloadState(t *testing.T) {
	location := time.FixedZone("NZST", 12*60*60)
	release := time.Date(2026, 8, 1, 0, 0, 0, 0, location).UTC()
	added := time.Date(2026, 7, 31, 22, 15, 0, 0, time.UTC)
	movie := radarr.Movie{
		ID: 9, Title: "Arrival", DigitalRelease: &release, HasFile: true, Monitored: true,
		MovieFile: &radarr.MovieFile{DateAdded: &added},
		Images:    []radarr.Image{{CoverType: "poster"}, {CoverType: "fanart"}},
	}
	effective, ok := effectiveRadarrRelease(movie)
	if !ok {
		t.Fatal("expected a release date")
	}
	item := toRadarrScheduleItem(movie, effective, time.Date(2026, 7, 30, 9, 0, 0, 0, location), location)

	if item.MembySource != "radarr" || item.MembyPlayable ||
		item.ImageTags["Primary"] == "" || len(item.BackdropImageTags) != 1 {
		t.Fatalf("unexpected synthetic item: %+v", item)
	}
	if item.MembyAvailability != "available" || item.MembyAvailabilityText != "Added at 10:15 AM" {
		t.Fatalf("unexpected availability: %+v", item)
	}
}
