package response

import (
	"time"

	"github.com/gofiber/fiber/v2"
)

type Status string

const (
	Error   Status = "error"
	Success Status = "success"
)

type APIResponse struct {
	Status    Status      `json:"status"`
	Message   string      `json:"message"`
	Data      interface{} `json:"data,omitempty"`
	Metadata  interface{} `json:"metadata,omitempty"`
	Page      *int        `json:"page,omitempty"`
	Total     *int        `json:"total,omitempty"`
	Timestamp time.Time   `json:"timestamp"`
}

func NewAPIResponse(
	status Status, message string,
	data interface{}, metadata interface{},
	page *int, total *int,
) APIResponse {

	return APIResponse{
		Status:    status,
		Message:   message,
		Data:      data,
		Metadata:  metadata,
		Timestamp: time.Now(),
		Page:      page,
		Total:     total,
	}
}
func WriteJSON(c *fiber.Ctx, status int, message string, data, metadata interface{}) error {
	var apiStatus Status
	if status >= 200 && status <= 299 {
		apiStatus = Success
	} else {
		apiStatus = Error
	}
	apiRes := NewAPIResponse(apiStatus, message, data, metadata, nil, nil)

	return c.Status(status).JSON(apiRes)
}
func WritePaginatedJSON(c *fiber.Ctx, status int, message string, data, metadata interface{}, page int, total int) error {
	var apiStatus Status
	if status >= 200 && status <= 299 {
		apiStatus = Success
	} else {
		apiStatus = Error
	}
	apiRes := NewAPIResponse(apiStatus, message, data, metadata, &page, &total)

	return c.Status(status).JSON(apiRes)
}
