package admin

import (
	"quyun/app/models"
	"quyun/database/fields"
	"quyun/database/schemas/public/table"

	. "github.com/go-jet/jet/v2/postgres"
	"github.com/gofiber/fiber/v3"
)

// @provider
type statistics struct{}

type StatisticsResponse struct {
	PostDraft     int64 `json:"post_draft"`
	PostPublished int64 `json:"post_published"`
	Media         int64 `json:"media"`
	Order         int64 `json:"order"`
	User          int64 `json:"user"`
	Amount        int64 `json:"amount"`
}

// dashboard statistics
//
//	@Router	/admin/statistics [get]
func (s *statistics) statistics(ctx fiber.Ctx) (*StatisticsResponse, error) {
	statistics := &StatisticsResponse{}

	var err error

	statistics.PostDraft, err = models.Posts.Count(ctx.Context(), table.Posts.Status.EQ(Int(int64(fields.PostStatusDraft))))
	if err != nil {
		return nil, err
	}
	statistics.PostPublished, err = models.Posts.Count(ctx.Context(), table.Posts.Status.EQ(Int(int64(fields.PostStatusPublished))))
	if err != nil {
		return nil, err
	}

	statistics.Media, err = models.Medias.Count(ctx.Context())
	if err != nil {
		return nil, err
	}

	statistics.Order, err = models.Orders.Count(ctx.Context(), table.Orders.Status.EQ(Int(int64(fields.OrderStatusCompleted))))
	if err != nil {
		return nil, err
	}

	statistics.User, err = models.Users.Count(ctx.Context(), BoolExp(Bool(true)))
	if err != nil {
		return nil, err
	}

	statistics.Amount, err = models.Orders.SumAmount(ctx.Context())
	if err != nil {
		return nil, err
	}

	return statistics, nil
}
