package controllers

import (
	"ai_developer_admin/libs/mysql"
	"ai_developer_admin/models"
	"encoding/json"
	"github.com/beego/beego/v2/core/logs"
	"net/http"
	"time"
)

type FavoriteController struct {
	MainController
}

func (server *FavoriteController) Add() {
	info, err := server.Check()
	if err != nil {
		server.respond(http.StatusUnauthorized, err.Error())
		return
	}

	body := server.Ctx.Input.RequestBody
	appRequest := models.Favorite{}
	err = json.Unmarshal(body, &appRequest) //解析body中数据
	logs.Debug("appRequest", appRequest)
	if err != nil {
		server.respond(models.NoRequestBody, err.Error())
		return
	}
	if appRequest.TaskTypeId == 0 {
		server.respond(models.MissingParameter, "Missing task_type_id parameter")
		return
	}

	checkFavorite := &models.Favorite{UserId: info.UserID, TaskTypeId: appRequest.TaskTypeId}
	err = mysql.GetMysqlInstace().Ormer.Read(checkFavorite, "user_id", "task_type_id")
	if err == nil {
		server.respond(models.BusinessFailed, "You have already followed this model")
		return
	}
	timestamp := time.Now()
	appRequest.CreatedTime = timestamp
	appRequest.UpdatedTime = timestamp
	appRequest.Deleted = 0

	_, err = mysql.GetMysqlInstace().Ormer.Insert(&appRequest)
	if err != nil {
		server.respond(models.BusinessFailed, err.Error())
		return
	}
	server.respond(http.StatusOK, "success")
}

func (server *FavoriteController) Cancel() {
	info, err := server.Check()
	if err != nil {
		server.respond(http.StatusUnauthorized, err.Error())
		return
	}
	body := server.Ctx.Input.RequestBody
	appRequest := models.Favorite{}
	err = json.Unmarshal(body, &appRequest) //解析body中数据
	logs.Debug("appRequest", appRequest)
	if err != nil {
		server.respond(models.NoRequestBody, err.Error())
		return
	}
	if appRequest.TaskTypeId == 0 {
		server.respond(models.MissingParameter, "Missing task_type_id parameter")
		return
	}
	checkFavorite := &models.Favorite{UserId: info.UserID, TaskTypeId: appRequest.TaskTypeId}
	err = mysql.GetMysqlInstace().Ormer.Read(checkFavorite, "user_id", "task_type_id")
	if err != nil {
		server.respond(models.BusinessFailed, "You have not followed this model")
		return
	}
	_, err = mysql.GetMysqlInstace().Ormer.Delete(checkFavorite)
	if err != nil {
		server.respond(models.BusinessFailed, "failed")
		return
	}
	server.respond(http.StatusOK, "success")
}

func (server *FavoriteController) IsFavorite() {
	info, err := server.Check()
	if err != nil {
		server.respond(http.StatusUnauthorized, err.Error())
		return
	}
	body := server.Ctx.Input.RequestBody
	appRequest := models.Favorite{}
	err = json.Unmarshal(body, &appRequest) //解析body中数据
	logs.Debug("appRequest", appRequest)
	if err != nil {
		server.respond(models.NoRequestBody, err.Error())
		return
	}
	if appRequest.TaskTypeId == 0 {
		server.respond(models.MissingParameter, "Missing task_type_id parameter")
		return
	}
	responseData := struct {
		IsFavorite bool `json:"is_favorite"`
	}{
		IsFavorite: false,
	}
	checkFavorite := &models.Favorite{UserId: info.UserID, TaskTypeId: appRequest.TaskTypeId}
	err = mysql.GetMysqlInstace().Ormer.Read(checkFavorite, "user_id", "task_type_id")
	if err != nil {
		server.respond(http.StatusOK, "", responseData)
		return
	}
	responseData.IsFavorite = true
	server.respond(http.StatusOK, "", responseData)
}

func (server *FavoriteController) Favorites() {
	info, err := server.Check()
	if err != nil {
		server.respond(http.StatusUnauthorized, err.Error())
		return
	}
	body := server.Ctx.Input.RequestBody
	appRequest := models.AppRequest{}
	err = json.Unmarshal(body, &appRequest) //解析body中数据
	logs.Debug("appRequest", appRequest, string(body))

	if appRequest.Page == 0 {
		appRequest.Page = 1
	}

	if appRequest.Size == 0 {
		appRequest.Size = 10
	}
	offset := (appRequest.Page - 1) * appRequest.Size

	qs := mysql.GetMysqlInstace().Ormer.QueryTable("favorite").Filter("deleted", 0)
	if !(info.Role == 1 || info.Role == 2) {
		qs.Filter("user_id", info.UserID)
	}
	infoQs := qs.Offset(offset).Limit(appRequest.Size)
	count, err := infoQs.Count()
	logs.Debug("Levels = ", count)
	var favorites []*models.Favorite
	if count > 0 {
		infoQs.All(&favorites)
		//for _,value := favorites {
		//
		//}
	}
	total, err := qs.Count()
	responseData := struct {
		Total int64       `json:"total"`
		Data  interface{} `json:"data,omitempty"`
	}{
		Total: total,
		Data:  favorites,
	}
	server.respond(http.StatusOK, "", responseData)
}
