// Copyright 2025 eat-pray-ai & OpenWaygate
// SPDX-License-Identifier: Apache-2.0

package playlist

import (
	"bytes"
	"encoding/json"
	"io"
	"math"
	"net/http"
	"reflect"
	"strings"
	"testing"

	"github.com/eat-pray-ai/yutu/pkg/common"
	"google.golang.org/api/youtube/v3"
)

func TestNewPlaylist(t *testing.T) {
	type args struct {
		opts []Option
	}

	mineTrue := true
	mineFalse := false
	svc := &youtube.Service{}

	tests := []struct {
		name string
		args args
		want IPlaylist[youtube.Playlist]
	}{
		{
			name: "with all options",
			args: args{
				opts: []Option{
					WithIds([]string{"playlist1", "playlist2"}),
					WithTitle("Test Playlist"),
					WithDescription("Test playlist description"),
					WithTags([]string{"tag1", "tag2", "tag3"}),
					WithLanguage("en"),
					WithChannelId("channel123"),
					WithPrivacy("public"),
					WithHl("en"),
					WithMaxResults(50),
					WithMine(&mineTrue),
					WithOnBehalfOfContentOwner("owner123"),
					WithOnBehalfOfContentOwnerChannel("ownerChannel123"),
					WithParts([]string{"id", "snippet"}),
					WithOutput("json"),
					WithService(svc),
				},
			},
			want: &Playlist{
				Fields: common.Fields{
					Service:                svc,
					Parts:                  []string{"id", "snippet"},
					Output:                 "json",
					Ids:                    []string{"playlist1", "playlist2"},
					MaxResults:             50,
					Hl:                     "en",
					ChannelId:              "channel123",
					OnBehalfOfContentOwner: "owner123",
				},
				Title:                         "Test Playlist",
				Description:                   "Test playlist description",
				Tags:                          []string{"tag1", "tag2", "tag3"},
				Language:                      "en",
				Privacy:                       "public",
				Mine:                          &mineTrue,
				OnBehalfOfContentOwnerChannel: "ownerChannel123",
			},
		},
		{
			name: "with no options",
			args: args{
				opts: []Option{},
			},
			want: &Playlist{Fields: common.Fields{}},
		},
		{
			name: "with nil boolean options",
			args: args{
				opts: []Option{
					WithMine(nil),
				},
			},
			want: &Playlist{Fields: common.Fields{}},
		},
		{
			name: "with false boolean options",
			args: args{
				opts: []Option{
					WithMine(&mineFalse),
				},
			},
			want: &Playlist{
				Fields: common.Fields{},
				Mine:   &mineFalse,
			},
		},
		{
			name: "with zero max results",
			args: args{
				opts: []Option{
					WithMaxResults(0),
				},
			},
			want: &Playlist{
				Fields: common.Fields{MaxResults: math.MaxInt64},
			},
		},
		{
			name: "with negative max results",
			args: args{
				opts: []Option{
					WithMaxResults(-10),
				},
			},
			want: &Playlist{
				Fields: common.Fields{MaxResults: 1},
			},
		},
		{
			name: "with empty string values",
			args: args{
				opts: []Option{
					WithTitle(""),
					WithDescription(""),
					WithLanguage(""),
					WithChannelId(""),
					WithPrivacy(""),
					WithHl(""),
					WithOnBehalfOfContentOwner(""),
					WithOnBehalfOfContentOwnerChannel(""),
				},
			},
			want: &Playlist{
				Fields:                        common.Fields{},
				Title:                         "",
				Description:                   "",
				Language:                      "",
				Privacy:                       "",
				OnBehalfOfContentOwnerChannel: "",
			},
		},
		{
			name: "with partial options",
			args: args{
				opts: []Option{
					WithTitle("My Playlist"),
					WithDescription("A great playlist"),
					WithPrivacy("private"),
					WithMaxResults(25),
				},
			},
			want: &Playlist{
				Fields:      common.Fields{MaxResults: 25},
				Title:       "My Playlist",
				Description: "A great playlist",
				Privacy:     "private",
			},
		},
	}

	for _, tt := range tests {
		t.Run(
			tt.name, func(t *testing.T) {
				if got := NewPlaylist(tt.args.opts...); !reflect.DeepEqual(
					got, tt.want,
				) {
					t.Errorf("%s\nNewPlaylist() = %v\nwant %v", tt.name, got, tt.want)
				}
			},
		)
	}
}

func TestPlaylist_Get(t *testing.T) {
	tests := []struct {
		name    string
		opts    []Option
		verify  func(*http.Request)
		wantLen int
		wantErr bool
	}{
		{
			name: "get playlists by id",
			opts: []Option{
				WithIds([]string{"playlist-id"}),
				WithMaxResults(1),
			},
			verify: func(r *http.Request) {
				if r.URL.Query().Get("id") != "playlist-id" {
					t.Errorf("expected id=playlist-id, got %s", r.URL.Query().Get("id"))
				}
			},
			wantLen: 1,
			wantErr: false,
		},
		{
			name: "get playlists by channelId",
			opts: []Option{
				WithChannelId("channel-id"),
				WithMaxResults(1),
			},
			verify: func(r *http.Request) {
				if r.URL.Query().Get("channelId") != "channel-id" {
					t.Errorf(
						"expected channelId=channel-id, got %s",
						r.URL.Query().Get("channelId"),
					)
				}
			},
			wantLen: 1,
			wantErr: false,
		},
		{
			name: "get playlists mine",
			opts: []Option{
				func(p *Playlist) {
					b := true
					p.Mine = &b
				},
				WithMaxResults(1),
			},
			verify: func(r *http.Request) {
				if r.URL.Query().Get("mine") != "true" {
					t.Errorf("expected mine=true, got %s", r.URL.Query().Get("mine"))
				}
			},
			wantLen: 1,
			wantErr: false,
		},
		{
			name: "get playlists with onBehalfOfContentOwner",
			opts: []Option{
				WithOnBehalfOfContentOwner("owner-id"),
				WithMaxResults(1),
			},
			verify: func(r *http.Request) {
				if r.URL.Query().Get("onBehalfOfContentOwner") != "owner-id" {
					t.Errorf(
						"expected onBehalfOfContentOwner=owner-id, got %s",
						r.URL.Query().Get("onBehalfOfContentOwner"),
					)
				}
			},
			wantLen: 1,
			wantErr: false,
		},
		{
			name: "get playlists with hl",
			opts: []Option{
				WithHl("es"),
				WithMaxResults(1),
			},
			verify: func(r *http.Request) {
				if r.URL.Query().Get("hl") != "es" {
					t.Errorf("expected hl=es, got %s", r.URL.Query().Get("hl"))
				}
			},
			wantLen: 1,
			wantErr: false,
		},
		{
			name: "get playlists with onBehalfOfContentOwnerChannel",
			opts: []Option{
				WithOnBehalfOfContentOwnerChannel("channel-id"),
				WithMaxResults(1),
			},
			verify: func(r *http.Request) {
				if r.URL.Query().Get("onBehalfOfContentOwnerChannel") != "channel-id" {
					t.Errorf(
						"expected onBehalfOfContentOwnerChannel=channel-id, got %s",
						r.URL.Query().Get("onBehalfOfContentOwnerChannel"),
					)
				}
			},
			wantLen: 1,
			wantErr: false,
		},
	}

	for _, tt := range tests {
		t.Run(
			tt.name, func(t *testing.T) {
				svc := common.NewTestService(
					t, http.HandlerFunc(
						func(w http.ResponseWriter, r *http.Request) {
							if tt.verify != nil {
								tt.verify(r)
							}
							w.Header().Set("Content-Type", "application/json")
							_, _ = w.Write(
								[]byte(`{
					"items": [
						{"id": "playlist-1", "snippet": {"title": "Playlist 1"}}
					]
				}`),
							)
						},
					),
				)

				opts := append([]Option{WithService(svc)}, tt.opts...)
				p := NewPlaylist(opts...)
				got, err := p.Get()
				if (err != nil) != tt.wantErr {
					t.Errorf("Playlist.Get() error = %v, wantErr %v", err, tt.wantErr)
					return
				}
				if len(got) != tt.wantLen {
					t.Errorf(
						"Playlist.Get() got length = %v, want %v", len(got), tt.wantLen,
					)
				}
			},
		)
	}
}

func TestPlaylist_Get_Pagination(t *testing.T) {
	svc := common.NewTestService(t, common.PaginationHandler("playlist"))
	p := NewPlaylist(WithService(svc), WithMaxResults(22))
	got, err := p.Get()
	if err != nil {
		t.Errorf("Playlist.Get() error = %v", err)
	}
	if len(got) != 22 {
		t.Errorf("Playlist.Get() got length = %v, want 22", len(got))
	}
}

func TestPlaylist_List(t *testing.T) {
	common.RunListTest(t, `{
			"items": [
				{
					"id": "playlist-1",
					"snippet": {
						"channelId": "channel-1",
						"title": "Playlist 1"
					}
				}
			]
		}`,
		func(svc *youtube.Service, output string) func(io.Writer) error {
			p := NewPlaylist(WithService(svc), WithOutput(output), WithIds([]string{"playlist-1"}))
			return p.List
		},
	)
}

func TestPlaylist_Insert(t *testing.T) {
	tests := []struct {
		name    string
		opts    []Option
		verify  func(*http.Request)
		wantErr bool
	}{
		{
			name: "insert playlist",
			opts: []Option{
				WithTitle("New Playlist"),
				WithDescription("Description"),
				WithPrivacy("public"),
			},
			verify: func(r *http.Request) {
				if r.Method != "POST" {
					t.Errorf("expected POST, got %s", r.Method)
				}
			},
			wantErr: false,
		},
	}

	for _, tt := range tests {
		t.Run(
			tt.name, func(t *testing.T) {
				svc := common.NewTestService(
					t, http.HandlerFunc(
						func(w http.ResponseWriter, r *http.Request) {
							if tt.verify != nil {
								tt.verify(r)
							}
							w.Header().Set("Content-Type", "application/json")
							_, _ = w.Write([]byte(`{"id": "new-playlist-id"}`))
						},
					),
				)

				opts := append([]Option{WithService(svc)}, tt.opts...)
				p := NewPlaylist(opts...)
				var buf bytes.Buffer
				if err := p.Insert(&buf); (err != nil) != tt.wantErr {
					t.Errorf("Playlist.Insert() error = %v, wantErr %v", err, tt.wantErr)
				}
			},
		)
	}
}

func TestPlaylist_Update(t *testing.T) {
	tests := []struct {
		name    string
		opts    []Option
		verify  func(*http.Request)
		wantErr bool
	}{
		{
			name: "update playlist",
			opts: []Option{
				WithIds([]string{"playlist-id"}),
				WithTitle("Updated Title"),
				WithMaxResults(1),
			},
			verify: func(r *http.Request) {
				if r.Method == "PUT" {
					parts := r.URL.Query()["part"]
					joined := strings.Join(parts, ",")
					if !strings.Contains(joined, "snippet") || !strings.Contains(
						joined, "status",
					) {
						t.Errorf(
							"expected part to contain snippet and status, got %v", parts,
						)
					}
				}
			},
			wantErr: false,
		},
		{
			name: "update playlist with all fields",
			opts: []Option{
				WithIds([]string{"playlist-id"}),
				WithTitle("Updated Title"),
				WithDescription("Updated Description"),
				WithTags([]string{"tag1", "tag2"}),
				WithLanguage("en"),
				WithPrivacy("private"),
				WithOnBehalfOfContentOwner("owner-id"),
				WithMaxResults(1),
			},
			verify: func(r *http.Request) {
				if r.Method == "PUT" {
					parts := r.URL.Query()["part"]
					joined := strings.Join(parts, ",")
					if !strings.Contains(joined, "snippet") || !strings.Contains(
						joined, "status",
					) {
						t.Errorf(
							"expected part to contain snippet and status, got %v", parts,
						)
					}
					if r.URL.Query().Get("onBehalfOfContentOwner") != "owner-id" {
						t.Errorf(
							"expected onBehalfOfContentOwner=owner-id, got %s",
							r.URL.Query().Get("onBehalfOfContentOwner"),
						)
					}

					var playlist youtube.Playlist
					if err := json.NewDecoder(r.Body).Decode(&playlist); err != nil {
						t.Errorf("failed to decode body: %v", err)
					}
					if playlist.Snippet.Title != "Updated Title" {
						t.Errorf(
							"expected title=Updated Title, got %s", playlist.Snippet.Title,
						)
					}
					if playlist.Snippet.Description != "Updated Description" {
						t.Errorf(
							"expected description=Updated Description, got %s",
							playlist.Snippet.Description,
						)
					}
					if playlist.Snippet.DefaultLanguage != "en" {
						t.Errorf(
							"expected language=en, got %s", playlist.Snippet.DefaultLanguage,
						)
					}
					if playlist.Status.PrivacyStatus != "private" {
						t.Errorf(
							"expected privacy=private, got %s", playlist.Status.PrivacyStatus,
						)
					}
				}
			},
			wantErr: false,
		},
	}

	for _, tt := range tests {
		t.Run(
			tt.name, func(t *testing.T) {
				svc := common.NewTestService(
					t, http.HandlerFunc(
						func(w http.ResponseWriter, r *http.Request) {
							if tt.verify != nil {
								tt.verify(r)
							}
							w.Header().Set("Content-Type", "application/json")
							if r.Method == "GET" {
								_, _ = w.Write(
									[]byte(`{
						"items": [
							{"id": "playlist-id", "snippet": {"title": "Old Title"}, "status": {"privacyStatus": "public"}}
						]
					}`),
								)
							} else {
								_, _ = w.Write([]byte(`{"id": "playlist-id", "snippet": {"title": "Updated Title"}}`))
							}
						},
					),
				)

				opts := append([]Option{WithService(svc)}, tt.opts...)
				p := NewPlaylist(opts...)
				var buf bytes.Buffer
				if err := p.Update(&buf); (err != nil) != tt.wantErr {
					t.Errorf("Playlist.Update() error = %v, wantErr %v", err, tt.wantErr)
				}
			},
		)
	}
}

func TestPlaylist_Delete(t *testing.T) {
	tests := []struct {
		name    string
		opts    []Option
		verify  func(*http.Request)
		wantErr bool
	}{
		{
			name: "delete playlist",
			opts: []Option{
				WithIds([]string{"playlist-id"}),
			},
			verify: func(r *http.Request) {
				if r.Method != "DELETE" {
					t.Errorf("expected DELETE, got %s", r.Method)
				}
			},
			wantErr: false,
		},
		{
			name: "delete playlist with onBehalfOfContentOwner",
			opts: []Option{
				WithIds([]string{"playlist-id"}),
				WithOnBehalfOfContentOwner("owner-id"),
			},
			verify: func(r *http.Request) {
				if r.Method != "DELETE" {
					t.Errorf("expected DELETE, got %s", r.Method)
				}
				if r.URL.Query().Get("onBehalfOfContentOwner") != "owner-id" {
					t.Errorf(
						"expected onBehalfOfContentOwner=owner-id, got %s",
						r.URL.Query().Get("onBehalfOfContentOwner"),
					)
				}
			},
			wantErr: false,
		},
	}

	for _, tt := range tests {
		t.Run(
			tt.name, func(t *testing.T) {
				svc := common.NewTestService(
					t, http.HandlerFunc(
						func(w http.ResponseWriter, r *http.Request) {
							if tt.verify != nil {
								tt.verify(r)
							}
							w.WriteHeader(http.StatusNoContent)
						},
					),
				)

				opts := append([]Option{WithService(svc)}, tt.opts...)
				p := NewPlaylist(opts...)
				var buf bytes.Buffer
				if err := p.Delete(&buf); (err != nil) != tt.wantErr {
					t.Errorf("Playlist.Delete() error = %v, wantErr %v", err, tt.wantErr)
				}
			},
		)
	}
}
