package handlers

import (
	"lune/talentscale/internal/modules/billing/services"
	"lune/talentscale/internal/pkg/ctxval"
	"lune/talentscale/pkg/response"

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

// SubscriptionHandler handles subscription-related endpoints
type SubscriptionHandler struct {
	billing *services.BillingService
}

// NewSubscriptionHandler creates a new subscription handler
func NewSubscriptionHandler(billing *services.BillingService) *SubscriptionHandler {
	return &SubscriptionHandler{billing: billing}
}

// GetActive handles GET /api/client/billing/subscription
// Returns the active subscription with remaining quota
func (h *SubscriptionHandler) GetActive(c *fiber.Ctx) error {
	companyID := ctxval.GetCompanyID(c)

	sub, err := h.billing.GetActiveSubscription(c.Context(), companyID)
	if err != nil {
		return response.InternalError(c, "Failed to fetch subscription: "+err.Error())
	}
	if sub == nil {
		return response.OK(c, "No active subscription", nil)
	}

	return response.OK(c, "Active subscription retrieved", sub)
}
