package api

import (
	"net/http"

	"github.com/pingcap/dbaas/billing2/config"
	"github.com/pingcap/dbaas/billing2/handler/centralhandler"

	"github.com/gin-gonic/gin"
	"github.com/pingcap/dbaas/billing2/utils/ginutils"
)

// @Id TCPN_SwaggerDoc
// @Summary Swagger Doc
// @Tags PartnerPrivateAPI
// @Description Swagger Doc
// @Schemes
// @Accept json
// @Produce json
// @Param accessOrgId query string true "org_id"
// @Success 200 {object} []byte
// @Router /swaggerDoc [get]
func (s *API) SwaggerDocOpenAPI(c *gin.Context) {
	file, err := centralhandler.GetCentralHandler().DownloadFile(config.Get().GetString(config.S3FileTCPNSwagger))
	if err != nil {
		ginutils.ReturnError(c, err)
		return
	}

	c.Header("Content-Disposition", "attachment; filename=service.openapi.swagger.json")
	c.Data(http.StatusOK, config.Get().GetString(config.CDNFileContentType), file)
}

// @Id TCPN_UserManual
// @Summary User Manual
// @Tags PartnerPrivateAPI
// @Description User Manual
// @Schemes
// @Accept json
// @Produce json
// @Param accessOrgId query string true "org_id"
// @Success 200 {object} []byte
// @Router /userManualDoc [get]
func (s *API) UserManualOpenAPI(c *gin.Context) {
	file, err := centralhandler.GetCentralHandler().DownloadFile(config.Get().GetString(config.S3FileTCPNUserManual))
	if err != nil {
		ginutils.ReturnError(c, err)
		return
	}

	c.Header("Content-Disposition", "attachment; filename=TiDB Cloud Partner Web Console User Guide.pdf")
	c.Data(http.StatusOK, config.Get().GetString(config.CDNFileContentType), file)
}

func InitializeDocRouter(router ginutils.MyRoute) {
	s := &API{}
	router.Handle("GET", "/swaggerDoc", s.SwaggerDocOpenAPI)
	router.Handle("GET", "/userManualDoc", s.UserManualOpenAPI)
}
