package middleware

import (
	"backend/internal/types"
	errs "backend/pkg/library/errors"
	"backend/pkg/permission/domain/entity"
	"backend/pkg/permission/domain/token"
	"context"

	"github.com/zeromicro/go-zero/rest/httpx"

	"backend/pkg/permission/domain/usecase"
	uc "backend/pkg/permission/usecase"
	"net/http"
)

type AuthMiddlewareParam struct {
	TokenSec     string
	TokenUseCase usecase.TokenUseCase
}

type AuthMiddleware struct {
	TokenSec     string
	TokenUseCase usecase.TokenUseCase
}

func NewAuthMiddleware(param AuthMiddlewareParam) *AuthMiddleware {
	return &AuthMiddleware{
		TokenSec:     param.TokenSec,
		TokenUseCase: param.TokenUseCase,
	}
}

func (m *AuthMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		// 解析 Header
		header := types.Authorization{}
		if err := httpx.ParseHeaders(r, &header); err != nil {
			e := errs.AuthInvalidPosixTimeError("failed to parse headers")
			httpx.WriteJsonCtx(r.Context(), w, e.HTTPStatus(), types.Resp{
				Code:    e.DisplayCode(),
				Message: e.Error(),
			})

			return
		}

		// 驗證 Token
		claim, err := uc.ParseClaims(header.Authorization, m.TokenSec, true)
		if err != nil {

			e := errs.AuthInvalidPosixTimeError(err.Error())
			httpx.WriteJsonCtx(r.Context(), w, e.HTTPStatus(), types.Resp{
				Code:    e.DisplayCode(),
				Message: e.Error(),
			})

			return
		}

		// 驗證 Token 是否在黑名單中
		if _, err := m.TokenUseCase.ValidationToken(r.Context(), entity.ValidationTokenReq{Token: header.Authorization}); err != nil {
			e := errs.AuthUnauthorizedError("failed to use this token")
			httpx.WriteJsonCtx(r.Context(), w, e.HTTPStatus(), types.Resp{
				Code:    e.DisplayCode(),
				Message: e.Error(),
			})

			return
		}

		// 設置 context 並傳遞給下一個處理器
		ctx := SetContext(r, claim)
		next(w, r.WithContext(ctx))
	}
}

func SetContext(r *http.Request, claim uc.TokenClaims) context.Context {
	ctx := context.WithValue(r.Context(), token.KeyRole, claim.Role())
	ctx = context.WithValue(ctx, token.KeyUID, claim.UID())
	ctx = context.WithValue(ctx, token.KeyDeviceID, claim.DeviceID())
	ctx = context.WithValue(ctx, token.KeyScope, claim.Scope())
	ctx = context.WithValue(ctx, token.KeyLoginID, claim.LoginID())

	return ctx
}
