package controller

import (
	"context"
	"errors"
	"fonchain-artshow/cmd/service"
	"fonchain-artshow/pb/artShow"
	"fonchain-artshow/pkg/m"
)

func (p *ArtShowProvider) CreateApply(_ context.Context, req *artShow.SaveApplyReq) (res *artShow.SaveApplyRes, err error) {
	if req.Applicant == "" {
		err = errors.New(m.ERROR_APPLICANT)
		return
	}
	if req.ApplyTime == "" {
		err = errors.New(m.ERROR_TIME)
		return
	}
	res = new(artShow.SaveApplyRes)
	err, applyUID := service.CreateShowApply(req)
	if err != nil {
		res.Msg = err.Error()
		err = errors.New(m.ERROR_CREATE)
		return
	}
	res.Msg = m.CREATE_SUCCESS
	res.ApplyUID = applyUID
	return
}

func (p *ArtShowProvider) UpdateApply(_ context.Context, req *artShow.SaveApplyReq) (res *artShow.SaveApplyRes, err error) {
	if req.ApplyUID == "" {
		err = errors.New(m.ERROR_INVALID_ID)
		return
	}
	res = new(artShow.SaveApplyRes)
	err, applyUID := service.UpdateShowApplyWithShowRel(req)
	if err != nil {
		res.Msg = err.Error()
		err = errors.New(m.UPDATE_FAILED)
		return
	}
	res.Msg = m.UPDATE_SUCCESS
	res.ApplyUID = applyUID
	return
}

func (p *ArtShowProvider) ApplyList(_ context.Context, req *artShow.ApplyListReq) (res *artShow.ApplyListRes, err error) {
	res = new(artShow.ApplyListRes)
	if req.Page == 0 {
		req.Page = 1
	}
	if req.PageSize == 0 {
		req.Page = 10
	}
	err, res.Total, res.Data = service.ShowApplyList(req)
	if err != nil {
		res.Msg = err.Error()
		err = errors.New(m.ERROR_QUERY)
		return
	}
	return
}

func (p *ArtShowProvider) ApplyDetail(_ context.Context, req *artShow.ApplyShowReq) (res *artShow.ApplyShowRes, err error) {
	if req.ApplyUID == "" {
		err = errors.New(m.ERROR_INVALID_ID)
		return
	}
	res = new(artShow.ApplyShowRes)
	err, res = service.ShowApplyDetail(req.ApplyUID)
	if err != nil {
		res.Msg = err.Error()
		err = errors.New(m.ERROR_QUERY)
		return
	}
	return
}

func (p *ArtShowProvider) DelApply(_ context.Context, req *artShow.DelApplyReq) (res *artShow.CommonRes, err error) {
	if len(req.ApplyUID) < 1 {
		err = errors.New(m.ERROR_INVALID_ID)
		return
	}
	res = new(artShow.CommonRes)
	err = service.DelApplyRelByApplyUID(req)
	if err != nil {
		res.Msg = err.Error()
		err = errors.New(m.ERROR_DELETE)
		return
	}
	res.Msg = m.DELETE_SUCCESS
	return
}

func (p *ArtShowProvider) UpdateApplyStatus(_ context.Context, req *artShow.UpdateApplyStatusReq) (res *artShow.CommonRes, err error) {
	if req.ApplyUID == "" {
		err = errors.New(m.ERROR_INVALID_ID)
		return
	}
	res = new(artShow.CommonRes)
	err = service.UpdateShowApplyStatus(req)
	if err != nil {
		res.Msg = err.Error()
		err = errors.New(m.UPDATE_FAILED)
		return
	}
	res.Msg = m.UPDATE_SUCCESS
	return
}
