package billing

import (
	"log/slog"
	"time"

	"github.com/gin-gonic/gin"

	"github.com/tidbcloud/AgentTeam/internal/api"
	"github.com/tidbcloud/AgentTeam/internal/api/dto"
)

type Dependencies struct {
	SessionAuth               SessionAuthenticator
	Organizations             OrganizationProvider
	BillingClient             BillingClient
	SeatBilling               SeatBillingStore
	Alipay                    AlipayClient
	Enabled                   bool
	PublicBaseURL             string
	ReturnBaseURL             string
	EmailVerificationRequired bool
	Now                       func() time.Time
	Logger                    *slog.Logger
}

func RegisterRoutes(router gin.IRouter, dependencies Dependencies) {
	if router == nil {
		return
	}
	handler := &Handler{
		enabled:       dependencies.Enabled,
		organizations: dependencies.Organizations,
		billingClient: dependencies.BillingClient,
		seatBilling:   dependencies.SeatBilling,
		alipay:        dependencies.Alipay,
		publicBaseURL: dependencies.PublicBaseURL,
		returnBaseURL: dependencies.ReturnBaseURL,
		now:           dependencies.Now,
		logger:        dependencies.Logger,
	}

	authenticated := router.Group(
		"/api/v1/billing",
		api.RequireAuthenticatedUser(dependencies.SessionAuth),
		api.RequireVerifiedIdentity(dependencies.EmailVerificationRequired),
	)
	authenticated.GET("/summary", api.HandleAuth(handler.Summary))
	authenticated.GET("/ledger", api.HandleAuth(handler.Ledger))
	authenticated.GET("/model-usage", api.HandleAuth(handler.ModelUsage))
	authenticated.GET("/payments", api.HandleAuth(handler.Payments))
	authenticated.GET("/pricing", api.HandleAuth(handler.Pricing))
	authenticated.POST("/credit-recharge-orders", api.HandleAuthJSON[dto.BillingCreditRechargeOrderCreateRequest](handler.CreateOrder))
	authenticated.GET("/credit-recharge-orders/:orderId", api.HandleAuth(handler.GetOrder))
	authenticated.POST("/credit-recharge-orders/:orderId/sync", api.HandleAuth(handler.SyncOrder))
	authenticated.GET("/seat-summary", api.HandleAuth(handler.SeatSummary))
	authenticated.GET("/seat-plans", api.HandleAuth(handler.SeatPlans))
	authenticated.GET("/seat-subscription", api.HandleAuth(handler.SeatSubscription))
	authenticated.GET("/seat-orders", api.HandleAuth(handler.ListSeatOrders))
	authenticated.POST("/seat-orders", api.HandleAuthJSON[dto.BillingSeatOrderCreateRequest](handler.CreateSeatOrder))
	authenticated.GET("/seat-orders/:orderId", api.HandleAuth(handler.GetSeatOrder))
	authenticated.POST("/seat-orders/:orderId/sync", api.HandleAuth(handler.SyncSeatOrder))
	authenticated.POST("/seat-orders/:orderId/cancel", api.HandleAuth(handler.CancelSeatOrder))
	authenticated.GET("/history", api.HandleAuth(handler.History))

	router.POST("/api/v1/billing/alipay/notify", handler.AlipayNotify)
	router.GET("/api/v1/billing/alipay/return", handler.AlipayReturn)
	router.POST("/api/v1/billing/seat-orders/alipay/notify", handler.SeatAlipayNotify)
	router.GET("/api/v1/billing/seat-orders/alipay/return", handler.SeatAlipayReturn)

	// Deprecated: compatibility ingress for payment provider configurations that
	// still point at the pre-versioned callback URLs. New deployments should use
	// the /api/v1/billing/... callback URLs above.
	router.POST("/api/billing/alipay/notify", handler.AlipayNotify)
	router.GET("/api/billing/alipay/return", handler.AlipayReturn)
	router.POST("/api/billing/seat-orders/alipay/notify", handler.SeatAlipayNotify)
	router.GET("/api/billing/seat-orders/alipay/return", handler.SeatAlipayReturn)
}
