package api

import (
	"copter-train/db/model"
	"copter-train/db/repo"
	"copter-train/log"
	"errors"
	"time"

	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson/primitive"
)

// 新增试题
// /admin/test/create/:scope
func CreateTest(c *gin.Context, apictx *ApiSession) (interface{}, error) {
	// 验证是否为管理员
	isAdmin, err := IsAdmin(c, apictx)
	if err != nil {
		return nil, err
	}
	if !isAdmin {
		return nil, errors.New("没有权限")
	}
	db := c.Param("scope")
	if len(db) == 0 {
		return nil, errors.New("scope不能为空")
	}
	test := &model.Test{}
	err = c.ShouldBindJSON(test)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	if test.Socre == nil {
		zero := 0
		test.Socre = &zero
	}
	test.Uid = apictx.User.ID
	test.CreateTime = time.Now()
	test.UpdateTime = time.Now()
	return repo.RepoAddDbDoc(apictx.CreateRepoCtx(), db, repo.CollectionTest, test)
}

// 删除试题
// /admin/test/delete/:id/:scope
func DeleteTest(c *gin.Context, apictx *ApiSession) (interface{}, error) {
	// 验证是否为管理员
	isAdmin, err := IsAdmin(c, apictx)
	if err != nil {
		return nil, err
	}
	if !isAdmin {
		return nil, errors.New("没有权限")
	}
	db := c.Param("scope")
	if len(db) == 0 {
		return nil, errors.New("scope不能为空")
	}
	_id := c.Param("id")
	id, _ := primitive.ObjectIDFromHex(_id)
	if id.IsZero() {
		return nil, errors.New("id错误")
	}
	return repo.RepoDeleteDbDoc(apictx.CreateRepoCtx(), db, repo.CollectionTest, _id)
}

// 试题列表
// /admin/test/list/:scope
func TestList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
	db := c.Param("scope")
	if len(db) == 0 {
		return nil, errors.New("scope不能为空")
	}
	page, size, query := UtilQueryPageSize(c)
	return repo.RepoDbPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
		Db:          db,
		CollectName: repo.CollectionTest,
		Page:        page,
		Size:        size,
		Query:       query,
		// Sort:        bson.D{bson.E{Key: "sort", Value: 1}, bson.E{Key: "createTime", Value: 1}},
	})
}

// 试题详情
// /admin/test/detail/:id/:scope
func TestDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
	db := c.Param("scope")
	if len(db) == 0 {
		return nil, errors.New("scope不能为空")
	}
	_id := c.Param("id")
	id, _ := primitive.ObjectIDFromHex(_id)
	if id.IsZero() {
		return nil, errors.New("id错误")
	}
	test := &model.Test{}
	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
		Db:          db,
		CollectName: repo.CollectionTest,
		Query:       repo.Map{"_id": id},
	}, test)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	if !found {
		return nil, errors.New("未找到该数据")
	}
	return test, nil
}

// 更新试题
// /admin/test/update/:scope
func UpdateTest(c *gin.Context, apictx *ApiSession) (interface{}, error) {
	// 验证是否为管理员
	isAdmin, err := IsAdmin(c, apictx)
	if err != nil {
		return nil, err
	}
	if !isAdmin {
		return nil, errors.New("没有权限")
	}
	db := c.Param("scope")
	if len(db) == 0 {
		return nil, errors.New("scope不能为空")
	}
	test := &model.Test{}
	err = c.ShouldBindJSON(test)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	if test.Id.IsZero() {
		return nil, errors.New("id错误")
	}
	return repo.RepoUpdateDbSetDoc(apictx.CreateRepoCtx(), db, repo.CollectionTest, test.Id.Hex(), test)
}
