package auth

import (
	"quyun/v2/app/http/v1/dto"
	"quyun/v2/app/services"
	"quyun/v2/database/models"
	"quyun/v2/pkg/consts"

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

// @provider
type Auth struct{}

// SendOTP sends an OTP to the provided phone number.
//
// @Router /t/:tenantCode/v1/auth/otp [post]
// @Summary Send OTP
// @Description Send OTP to phone number
// @Tags Auth
// @Accept json
// @Produce json
// @Param form body dto.SendOTPForm true "Phone number"
// @Success 200 {object} string "OTP sent"
// @Bind form body
func (a *Auth) SendOTP(ctx fiber.Ctx, form *dto.SendOTPForm) error {
	return services.User.SendOTP(ctx, form.Phone)
}

// Login logs in or registers a user with OTP.
//
// @Router /t/:tenantCode/v1/auth/login [post]
// @Summary Login or Register with OTP
// @Description Login or register user using phone number and OTP
// @Tags Auth
// @Accept json
// @Produce json
// @Param form body dto.LoginForm true "Login form"
// @Success 200 {object} dto.LoginResponse
// @Bind form body
func (a *Auth) Login(ctx fiber.Ctx, form *dto.LoginForm) (*dto.LoginResponse, error) {
	tenantID := int64(0)
	if t := ctx.Locals(consts.CtxKeyTenant); t != nil {
		if tenant, ok := t.(*models.Tenant); ok {
			tenantID = tenant.ID
		}
	}
	return services.User.LoginWithOTP(ctx, tenantID, form.Phone, form.OTP)
}
