package tenant

import (
	"time"

	"quyun/v2/app/errorx"
	"quyun/v2/app/http/tenant/dto"
	"quyun/v2/app/requests"
	"quyun/v2/app/services"
	"quyun/v2/database/models"

	"github.com/gofiber/fiber/v3"
	log "github.com/sirupsen/logrus"
)

// tenantJoinAdmin 提供“租户管理员审核加入申请”的相关接口。
//
// @provider
type tenantJoinAdmin struct{}

// adminJoinRequests
//
//	@Summary	加入申请列表（租户管理）
//	@Tags		Tenant
//	@Accept		json
//	@Produce	json
//	@Param		tenantCode	path		string							true	"Tenant Code"
//	@Param		filter		query		dto.AdminTenantJoinRequestListFilter	true	"Filter"
//	@Success	200			{object}	requests.Pager{items=models.TenantJoinRequest}
//
//	@Router		/t/:tenantCode/v1/admin/join-requests [get]
//	@Bind		tenant local key(tenant)
//	@Bind		tenantUser local key(tenant_user)
//	@Bind		filter query
func (*tenantJoinAdmin) adminJoinRequests(
	ctx fiber.Ctx,
	tenant *models.Tenant,
	tenantUser *models.TenantUser,
	filter *dto.AdminTenantJoinRequestListFilter,
) (*requests.Pager, error) {
	if err := requireTenantAdmin(tenantUser); err != nil {
		return nil, err
	}
	if filter == nil {
		filter = &dto.AdminTenantJoinRequestListFilter{}
	}

	log.WithFields(log.Fields{
		"tenant_id": tenant.ID,
		"user_id":   tenantUser.UserID,
		"status":    filter.Status,
		"query_uid": filter.UserID,
	}).Info("tenant.admin.join_requests.list")

	return services.Tenant.AdminJoinRequestPage(ctx.Context(), tenant.ID, filter)
}

// adminApproveJoinRequest
//
//	@Summary	通过加入申请（租户管理）
//	@Tags		Tenant
//	@Accept		json
//	@Produce	json
//	@Param		tenantCode	path		string						true	"Tenant Code"
//	@Param		requestID	path		int64						true	"RequestID"
//	@Param		form		body		dto.AdminTenantJoinRequestDecideForm	true	"Form"
//	@Success	200			{object}	models.TenantJoinRequest
//
//	@Router		/t/:tenantCode/v1/admin/join-requests/:requestID/approve [post]
//	@Bind		tenant local key(tenant)
//	@Bind		tenantUser local key(tenant_user)
//	@Bind		requestID path
//	@Bind		form body
func (*tenantJoinAdmin) adminApproveJoinRequest(
	ctx fiber.Ctx,
	tenant *models.Tenant,
	tenantUser *models.TenantUser,
	requestID int64,
	form *dto.AdminTenantJoinRequestDecideForm,
) (*models.TenantJoinRequest, error) {
	if err := requireTenantAdmin(tenantUser); err != nil {
		return nil, err
	}
	if requestID <= 0 {
		return nil, errorx.ErrInvalidParameter.WithMsg("request_id must be > 0")
	}
	if form == nil {
		return nil, errorx.ErrInvalidParameter
	}

	log.WithFields(log.Fields{
		"tenant_id":   tenant.ID,
		"user_id":     tenantUser.UserID,
		"request_id":  requestID,
		"decide_time": time.Now(),
	}).Info("tenant.admin.join_requests.approve")

	return services.Tenant.AdminApproveJoinRequest(ctx.Context(), tenant.ID, tenantUser.UserID, requestID, form.Reason)
}

// adminRejectJoinRequest
//
//	@Summary	拒绝加入申请（租户管理）
//	@Tags		Tenant
//	@Accept		json
//	@Produce	json
//	@Param		tenantCode	path		string						true	"Tenant Code"
//	@Param		requestID	path		int64						true	"RequestID"
//	@Param		form		body		dto.AdminTenantJoinRequestDecideForm	true	"Form"
//	@Success	200			{object}	models.TenantJoinRequest
//
//	@Router		/t/:tenantCode/v1/admin/join-requests/:requestID/reject [post]
//	@Bind		tenant local key(tenant)
//	@Bind		tenantUser local key(tenant_user)
//	@Bind		requestID path
//	@Bind		form body
func (*tenantJoinAdmin) adminRejectJoinRequest(
	ctx fiber.Ctx,
	tenant *models.Tenant,
	tenantUser *models.TenantUser,
	requestID int64,
	form *dto.AdminTenantJoinRequestDecideForm,
) (*models.TenantJoinRequest, error) {
	if err := requireTenantAdmin(tenantUser); err != nil {
		return nil, err
	}
	if requestID <= 0 {
		return nil, errorx.ErrInvalidParameter.WithMsg("request_id must be > 0")
	}
	if form == nil {
		return nil, errorx.ErrInvalidParameter
	}

	log.WithFields(log.Fields{
		"tenant_id":   tenant.ID,
		"user_id":     tenantUser.UserID,
		"request_id":  requestID,
		"decide_time": time.Now(),
	}).Info("tenant.admin.join_requests.reject")

	return services.Tenant.AdminRejectJoinRequest(ctx.Context(), tenant.ID, tenantUser.UserID, requestID, form.Reason)
}
