package controllers

import (
	"intent-system/pkg/privilege"
	"intent-system/pkg/proto"
	"intent-system/pkg/sessions"

	"github.com/civet148/log"
	"github.com/gin-gonic/gin"
)

func (m *Controller) NewsList(c *gin.Context) {
	var req proto.NewsListReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx := sessions.GetContext(c)
	var ok bool
	if ctx != nil {
		ok = m.CheckPrivilege(c, ctx, privilege.NewsAccess)
	}
	resp, total, code := m.BizCore.NewsList(ctx, &req, ok)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, len(resp.List), total)
}

func (m *Controller) NewsAdd(c *gin.Context) {
	var req proto.NewsAddReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.NewsAdd)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, code := m.BizCore.NewsAdd(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, 1, 1)
}

func (m *Controller) NewsEdit(c *gin.Context) {
	var req proto.NewsEditReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.NewsEdit)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, code := m.BizCore.NewsEdit(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, 1, 1)
}

func (m *Controller) NewsDelete(c *gin.Context) {
	var req proto.NewsDeleteReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.NewsDelete)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, code := m.BizCore.NewsDelete(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, 1, 1)
}

func (m *Controller) NewsCompare(c *gin.Context) {
	var req proto.NewsCompareReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.NewsAccess)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, code := m.BizCore.NewsCompare(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, 1, 1)
}

func (m *Controller) NewsPublish(c *gin.Context) {
	var req proto.NewsPublishReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.NewsEdit)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, code := m.BizCore.NewsPublish(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, 1, 1)
}

func (m *Controller) NewsDraftList(c *gin.Context) {
	var req proto.NewsDraftListReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.NewsAccess)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, total, code := m.BizCore.NewsDraftList(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, len(resp.List), total)
}

func (m *Controller) NewsDraftEdit(c *gin.Context) {
	var req proto.NewsDraftEditReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.NewsEdit)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, code := m.BizCore.NewsDraftEdit(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, 1, 1)
}

func (m *Controller) NewsDraftPublish(c *gin.Context) {
	var req proto.NewsDraftPublishReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.NewsEdit)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, code := m.BizCore.NewsDraftPublish(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, 1, 1)
}

func (m *Controller) NewsDraftDelete(c *gin.Context) {
	var req proto.NewsDraftDeleteReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.NewsDelete)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, code := m.BizCore.NewsDraftDelete(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, 1, 1)
}

func (m *Controller) NewsTag(c *gin.Context) {
	var req proto.NewsTagReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.NewsEdit)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, code := m.BizCore.NewsTag(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, 1, 1)
}

func (m *Controller) QaList(c *gin.Context) {
	var req proto.QaListReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx := sessions.GetContext(c)
	var ok bool
	if ctx != nil {
		ok = m.CheckPrivilege(c, ctx, privilege.QA_Access)
	}
	resp, total, code := m.BizCore.QaList(ctx, &req, ok)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, len(resp.List), total)
}

func (m *Controller) QaAdd(c *gin.Context) {
	var req proto.QaAddReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.QA_Add)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, code := m.BizCore.QaAdd(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, 1, 1)
}

func (m *Controller) QaEdit(c *gin.Context) {
	var req proto.QaEditReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.QA_Edit)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, code := m.BizCore.QaEdit(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, 1, 1)
}

func (m *Controller) QaDelete(c *gin.Context) {
	var req proto.QaDeleteReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.QA_Delete)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, code := m.BizCore.QaDelete(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, 1, 1)
}

func (m *Controller) QaDraftList(c *gin.Context) {
	var req proto.QaDraftListReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.QA_Access)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, total, code := m.BizCore.QaDraftList(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, len(resp.List), total)
}

func (m *Controller) QaDraftEdit(c *gin.Context) {
	var req proto.QaDraftEditReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.QA_Edit)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, code := m.BizCore.QaDraftEdit(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, 1, 1)
}

func (m *Controller) QaDraftPublish(c *gin.Context) {
	var req proto.QaDraftPublishReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.QA_Edit)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, code := m.BizCore.QaDraftPublish(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, 1, 1)
}

func (m *Controller) QaDraftDelete(c *gin.Context) {
	var req proto.QaDraftDeleteReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.QA_Delete)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, code := m.BizCore.QaDraftDelete(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, 1, 1)
}

func (m *Controller) SubListAll(c *gin.Context) {
	var req proto.SubListAllReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.SubAccess)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, total, code := m.BizCore.SubListAll(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, len(resp.List), total)
}

func (m *Controller) SubListPushed(c *gin.Context) {
	var req proto.SubListPushedReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.SubAccess)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, total, code := m.BizCore.SubListPushed(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, len(resp.List), total)
}

func (m *Controller) SubListToday(c *gin.Context) {
	var req proto.SubListTodayReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.SubAccess)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, total, code := m.BizCore.SubListToday(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, len(resp.List), total)
}

func (m *Controller) SubAddNews(c *gin.Context) {
	var req proto.SubAddNewsReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.SubAdd)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, code := m.BizCore.SubAddNews(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, 1, 1)
}

func (m *Controller) SubEditNews(c *gin.Context) {
	var req proto.SubEditNewsReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.SubEdit)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, code := m.BizCore.SubEditNews(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, 1, 1)
}

func (m *Controller) TagList(c *gin.Context) {
	var req proto.TagListReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	resp, total, code := m.BizCore.TagList(nil, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, len(resp.List), total)
}

func (m *Controller) TagAdd(c *gin.Context) {
	var req proto.TagAddReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.TagAdd)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, code := m.BizCore.TagAdd(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, 1, 1)
}

func (m *Controller) TagEdit(c *gin.Context) {
	var req proto.TagEditReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.TagEdit)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, code := m.BizCore.TagEdit(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, 1, 1)
}

func (m *Controller) TagDelete(c *gin.Context) {
	var req proto.TagDeleteReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx, ok := m.ContextPlatformPrivilege(c, privilege.TagDelete)
	if !ok {
		log.Errorf("user authentication context is nil or privilege check failed")
		return
	}
	resp, code := m.BizCore.TagDelete(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, 1, 1)
}

func (m *Controller) NewsAsync(c *gin.Context) {
	var req proto.NewsAsyncReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx := sessions.GetContext(c)
	var ok bool
	if ctx != nil {
		ok = m.CheckPrivilege(c, ctx, privilege.NewsAccess)
	}
	resp, total, code := m.BizCore.NewsAsync(ctx, &req, ok)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, len(resp.List), total)
}

func (m *Controller) NewsAsyncBatch(c *gin.Context) {
	var req proto.NewsAsyncBatchReq

	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	//log.Infof("....................NewsAsyncBatch request: %+v", req)
	ctx := sessions.GetContext(c)
	var ok bool
	if ctx != nil {
		ok = m.CheckPrivilege(c, ctx, privilege.NewsAccess)
	}
	ok = ok
	resp, total, code := m.BizCore.NewsAsyncBatch(ctx, &req, true)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, len(resp.List), total)
}

func (m *Controller) NewsPullNew(c *gin.Context) {
	var req proto.NewsPullNewReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}
	ctx := sessions.GetContext(c)
	var ok bool
	if ctx != nil {
		ok = m.CheckPrivilege(c, ctx, privilege.NewsAccess)
	}
	ok = ok
	resp, total, code := m.BizCore.NewsPullNew(ctx, &req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}
	m.OK(c, resp, len(resp.List), total)
}
