package pressreleases

import (
	"strconv"

	"github.com/gin-gonic/gin"

	"fonchain-fiee/api/pressreleases"
	"fonchain-fiee/pkg/model/login"
	"fonchain-fiee/pkg/service"
)

func Create(ctx *gin.Context) {
	var req pressreleases.CreateReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	user := login.GetUserInfoFromC(ctx)
	req.Operator = user.Name
	req.OperatorId = int32(user.ID)
	resp, err := service.PressReleasesProvider.Create(ctx, &req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
}

func Delete(ctx *gin.Context) {
	var req pressreleases.DeleteReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	resp, err := service.PressReleasesProvider.Delete(ctx, &req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
}

func Display(ctx *gin.Context) {
	var req pressreleases.DisplayReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	resp, err := service.PressReleasesProvider.Display(ctx, &req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
}

func List(ctx *gin.Context) {
	var req pressreleases.ListReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	resp, err := service.PressReleasesProvider.List(ctx, &req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
}

func Edit(ctx *gin.Context) {
	var req pressreleases.EditReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	user := login.GetUserInfoFromC(ctx)
	req.Operator = user.Name
	req.OperatorId = int32(user.ID)
	resp, err := service.PressReleasesProvider.Edit(ctx, &req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
}

func Get(ctx *gin.Context) {
	id, err := strconv.Atoi(ctx.Query("id"))
	if err != nil {
		service.Error(ctx, err)
		return
	}
	resp, err := service.PressReleasesProvider.Get(ctx, &pressreleases.GetReq{
		Id: int64(id),
	})
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
}
