package store

import "testing"

func TestRequestPolicyAllowsOnlyListedUsers(t *testing.T) {
	policy := RequestPolicy{AllowedUserIDs: []string{"user-2"}}
	if policy.Allows("user-1") {
		t.Fatal("unlisted user was allowed")
	}
	if !policy.Allows("user-2") {
		t.Fatal("listed user was denied")
	}
}

func TestPlaybackPolicyDefaultsAndClampsDuration(t *testing.T) {
	defaults := DefaultPlaybackPolicy()
	if !defaults.PrerollEnabled || defaults.PrerollDurationMs != 6_500 {
		t.Fatalf("default playback policy = %+v", defaults)
	}
	if got := normalizePlaybackPolicy(PlaybackPolicy{PrerollDurationMs: 500}); got.PrerollDurationMs != 1_000 {
		t.Fatalf("short duration = %d, want 1000", got.PrerollDurationMs)
	}
	if got := normalizePlaybackPolicy(PlaybackPolicy{PrerollDurationMs: 60_000}); got.PrerollDurationMs != 30_000 {
		t.Fatalf("long duration = %d, want 30000", got.PrerollDurationMs)
	}
}

func TestMDBListSettingsAreOptionalAndNormalizeSources(t *testing.T) {
	defaults := DefaultMDBListSettings()
	if defaults.Enabled || defaults.APIKey != "" || len(defaults.Sources) == 0 {
		t.Fatalf("default MDBList settings = %+v", defaults)
	}
	got := normalizeMDBListSettings(MDBListSettings{
		APIKey: "  secret  ", Sources: []string{"IMDb", "imdb", "unknown", "letterboxd"},
	})
	if got.APIKey != "secret" || len(got.Sources) != 2 ||
		got.Sources[0] != "imdb" || got.Sources[1] != "letterboxd" {
		t.Fatalf("normalized MDBList settings = %+v", got)
	}
}

func TestFeaturePolicyDefaultsAreRecoverable(t *testing.T) {
	policy := normalizeFeaturePolicy(FeaturePolicy{})
	if policy.Overrides == nil || policy.SafeMode || policy.Revision != 0 {
		t.Fatalf("default feature policy = %+v", policy)
	}
	policy.Overrides["sonarr_preroll"] = false
	if policy.Overrides["sonarr_preroll"] {
		t.Fatal("explicit off override was not retained")
	}
}
