package tenant

import (
	"testing"

	"github.com/tidbcloud/account/internel/svc/org/tenantctx"

	. "github.com/smartystreets/goconvey/convey"
	"github.com/tidbcloud/account/idl/pbgen/proto/central"
	"github.com/tidbcloud/account/internel/dao/entity"
)

var (
	tenantID  uint64    = 1
	creatorID entity.ID = 2
)

func TestBuildCreatePocParams(t *testing.T) {
	Convey("should return ok if everything is well", t, func() {
		req := &central.CreatePocApplicationReq{
			OrgId: tenantID,
			Questions: []*central.PocQuestion{
				{
					Number:   1,
					Type:     central.PocQuestion_RADIO,
					Question: "question1",
					Options: []*central.PocQuestion_Option{
						{
							Name:       "option1",
							IsSelected: false,
						},
						{
							Name:       "option2",
							IsSelected: true,
						},
					},
					Note: "",
				},
				{
					Number:   2,
					Type:     central.PocQuestion_CHECKBOX,
					Question: "question2",
					Options: []*central.PocQuestion_Option{
						{
							Name:       "option1",
							IsSelected: false,
						},
						{
							Name:       "option2",
							IsSelected: true,
						},
						{
							Name:       "option3",
							IsSelected: true,
						},
					},
					Note: "",
				},
				{
					Number:   3,
					Type:     central.PocQuestion_TEXT,
					Question: "question3",
					Options:  nil,
					Note:     "note",
				},
			},
		}

		pocApplication := tenantctx.PocApplication{
			TenantID:  tenantID,
			CreatorID: creatorID.Uint64(),
			Questions: []tenantctx.PocQuestion{
				{
					Number:   1,
					Type:     entity.PocQuestionTypeRadio,
					Question: "question1",
					Options: []tenantctx.PocQuestionOption{
						{
							Name:       "option1",
							IsSelected: false,
						},
						{
							Name:       "option2",
							IsSelected: true,
						},
					},
					Note: "",
				},
				{
					Number:   2,
					Type:     entity.PocQuestionTypeCheckbox,
					Question: "question2",
					Options: []tenantctx.PocQuestionOption{
						{
							Name:       "option1",
							IsSelected: false,
						},
						{
							Name:       "option2",
							IsSelected: true,
						},
						{
							Name:       "option3",
							IsSelected: true,
						},
					},
					Note: "",
				},
				{
					Number:   3,
					Type:     entity.PocQuestionTypeText,
					Question: "question3",
					Options:  []tenantctx.PocQuestionOption{},
					Note:     "note",
				},
			},
			PocExtraFields: &entity.PocExtraFields{},
		}
		application, err := buildCreatePocParams(req, creatorID)
		So(err, ShouldBeNil)
		So(application, ShouldNotBeNil)
		So(*application, ShouldResemble, pocApplication)
	})

	Convey("should return error if questions is empty", t, func() {
		req := &central.CreatePocApplicationReq{
			OrgId:     tenantID,
			Questions: nil,
		}
		_, err := buildCreatePocParams(req, creatorID)
		So(err, ShouldNotBeNil)

		req.Questions = []*central.PocQuestion{}
		_, err = buildCreatePocParams(req, creatorID)
		So(err, ShouldNotBeNil)
	})

	Convey("should return error if question num is duplicated", t, func() {
		req := &central.CreatePocApplicationReq{
			OrgId: tenantID,
			Questions: []*central.PocQuestion{
				{
					Question: "question1",
					Number:   1,
				},
				{
					Question: "question2",
					Number:   1,
				},
			},
		}
		_, err := buildCreatePocParams(req, creatorID)
		So(err, ShouldNotBeNil)
	})

	Convey("should return error if question is empty", t, func() {
		req := &central.CreatePocApplicationReq{
			OrgId: tenantID,
			Questions: []*central.PocQuestion{
				{
					Number:   1,
					Type:     central.PocQuestion_TEXT,
					Question: "",
					Options:  nil,
					Note:     "note",
				},
			},
		}
		_, err := buildCreatePocParams(req, creatorID)
		So(err, ShouldNotBeNil)
	})

	Convey("should return error if question options is empty for question type radio or checkbox", t, func() {
		req1 := &central.CreatePocApplicationReq{
			OrgId: tenantID,
			Questions: []*central.PocQuestion{
				{
					Number:   1,
					Type:     central.PocQuestion_RADIO,
					Question: "question1",
					Options:  nil,
					Note:     "",
				},
			},
		}
		_, err := buildCreatePocParams(req1, creatorID)
		So(err, ShouldNotBeNil)

		req2 := &central.CreatePocApplicationReq{
			OrgId: tenantID,
			Questions: []*central.PocQuestion{
				{
					Number:   1,
					Type:     central.PocQuestion_CHECKBOX,
					Question: "question1",
					Options:  nil,
					Note:     "",
				},
			},
		}
		_, err = buildCreatePocParams(req2, creatorID)
		So(err, ShouldNotBeNil)
	})
}
