package admin

import (
	"quyun/providers/jwt"

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

// @provider
type auth struct {
	jwt *jwt.JWT
}

type AuthBody struct {
	Username string `json:"username" validate:"required"`
	Password string `json:"password" validate:"required"`
}

type TokenResponse struct {
	Token string `json:"token"`
}

// Login
//
//	@Router	/admin/auth [post]
//	@Bind	body body
func (ctl *auth) Login(ctx fiber.Ctx, body *AuthBody) (*TokenResponse, error) {
	if body.Username == "pl.yang" && body.Password == "Xixi@0202" {
		claim := ctl.jwt.CreateClaims(jwt.BaseClaims{
			UserID: -20140202,
		})

		token, err := ctl.jwt.CreateToken(claim)
		if err != nil {
			return nil, err
		}
		return &TokenResponse{Token: token}, nil
	}
	return nil, fiber.ErrUnauthorized
}
