// Copyright 2026 Moko Consulting <hello@mokoconsulting.tech>
// SPDX-License-Identifier: GPL-3.0-or-later

package repo

import (
	"fmt"
	"net/http"

	"code.mokoconsulting.tech/MokoConsulting/MokoGitea/modules/badge"
	"code.mokoconsulting.tech/MokoConsulting/MokoGitea/services/context"
)

// GetRepoBadge returns an SVG badge for the repository.
func GetRepoBadge(ctx *context.APIContext) {
	badgeType := ctx.PathParam("type")
	style := ctx.FormString("style")

	svg, err := badge.FormatRepoBadgeSVG(ctx, ctx.Repo.Repository, badgeType, style)
	if err != nil {
		ctx.APIErrorInternal(fmt.Errorf("rendering badge: %w", err))
		return
	}

	ctx.Resp.Header().Set("Content-Type", "image/svg+xml")
	ctx.Resp.Header().Set("Cache-Control", "public, max-age=300")
	ctx.Resp.WriteHeader(http.StatusOK)
	_, _ = ctx.Resp.Write([]byte(svg))
}
