package app

import (
	"bwallet/pkg/errno"
	"bwallet/pkg/handler"
	"bwallet/pkg/util"
	"bwallet/service/client_app_service"
	"github.com/gin-gonic/gin"
	"time"
)

func AppExpired(c *gin.Context) {
	arg := c.Query("key")
	if arg == "" {
		handler.SendResponse(c, errno.ErrValidation, nil)
		return
	}

	app_service := client_app_service.ClientApp{
		AppKey:   arg,
		PageNum:  util.GetPage(c),
		PageSize: util.GetLimit(c),
	}

	total, err := app_service.Count()
	if err != nil {
		handler.SendResponse(c, errno.ErrCountClientApp, nil)
		return
	}

	if 0 == total {
		handler.SendResponse(c, errno.ErrClientAppNotFound, nil)
		return
	}

	app, err := app_service.GetAll()
	if err != nil {
		handler.SendResponse(c, errno.InternalServerError, nil)
		return
	}

	data := false
	if time.Now().Unix() < app[0].Deadline {
		data = true
	}

	handler.SendResponse(c, nil, data)
}
