package jsonapi

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

type Resource struct {
	Type       string         `json:"type"`
	ID         string         `json:"id,omitempty"`
	Attributes map[string]any `json:"attributes,omitempty"`
}

type Document struct {
	Data any `json:"data"`
	Meta any `json:"meta,omitempty"`
}

type ErrorSource struct {
	Pointer string `json:"pointer,omitempty"`
}

type ErrorObject struct {
	Status    string       `json:"status"`
	Code      string       `json:"code,omitempty"`
	ErrorCode string       `json:"error_code,omitempty"`
	Title     string       `json:"title,omitempty"`
	Detail    string       `json:"detail,omitempty"`
	Source    *ErrorSource `json:"source,omitempty"`
}

type ErrorDocument struct {
	Errors []ErrorObject `json:"errors"`
}

func Write(c *fiber.Ctx, status int, data any) error {
	return c.Status(status).JSON(Document{Data: data})
}

func WriteWithMeta(c *fiber.Ctx, status int, data any, meta any) error {
	return c.Status(status).JSON(Document{Data: data, Meta: meta})
}

func WriteErrors(c *fiber.Ctx, status int, errs []ErrorObject) error {
	return c.Status(status).JSON(ErrorDocument{Errors: errs})
}
