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

package videoAbuseReportReason

import (
	"io"
	"net/http"
	"reflect"
	"testing"

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

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

	svc := &youtube.Service{}

	tests := []struct {
		name string
		args args
		want IVideoAbuseReportReason[youtube.VideoAbuseReportReason]
	}{
		{
			name: "with all options",
			args: args{
				opts: []Option{
					WithHL("en"),
					WithParts([]string{"id", "snippet"}),
					WithOutput("json"),
					WithService(svc),
				},
			},
			want: &VideoAbuseReportReason{
				Fields: common.Fields{
					Service: svc,
					Parts:   []string{"id", "snippet"},
					Output:  "json",
					Hl:      "en",
				},
			},
		},
		{
			name: "with no options",
			args: args{
				opts: []Option{},
			},
			want: &VideoAbuseReportReason{Fields: common.Fields{}},
		},
		{
			name: "with empty string values",
			args: args{
				opts: []Option{
					WithHL(""),
					WithOutput(""),
				},
			},
			want: &VideoAbuseReportReason{
				Fields: common.Fields{
					Output: "",
					Hl:     "",
				},
			},
		},
		{
			name: "with partial options",
			args: args{
				opts: []Option{
					WithHL("ja"),
					WithParts([]string{"snippet"}),
				},
			},
			want: &VideoAbuseReportReason{
				Fields: common.Fields{
					Parts: []string{"snippet"},
					Hl:    "ja",
				},
			},
		},
	}

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

func TestVideoAbuseReportReason_Get(t *testing.T) {
	tests := []struct {
		name    string
		opts    []Option
		verify  func(*http.Request)
		wantLen int
		wantErr bool
	}{
		{
			name: "get video abuse report reasons",
			opts: []Option{
				WithHL("es"),
			},
			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,
		},
	}

	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": "reason-1", "snippet": {"label": "Reason 1"}}
					]
				}`),
							)
						},
					),
				)

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

func TestVideoAbuseReportReason_List(t *testing.T) {
	common.RunListTest(t, `{
			"items": [
				{
					"id": "reason-1",
					"snippet": {
						"label": "Reason 1"
					}
				}
			]
		}`,
		func(svc *youtube.Service, output string) func(io.Writer) error {
			va := NewVideoAbuseReportReason(WithService(svc), WithOutput(output))
			return va.List
		},
	)
}
