package v1

import (
	"quyun/v2/app/errorx"
	"quyun/v2/app/http/v1/dto"
	auth_dto "quyun/v2/app/http/v1/dto"
	"quyun/v2/app/requests"
	"quyun/v2/app/services"
	"quyun/v2/database/models"

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

// @provider
type User struct{}

// Get current user profile
//
// @Router /t/:tenantCode/v1/me [get]
// @Summary Get user profile
// @Description Get current user profile
// @Tags UserCenter
// @Accept json
// @Produce json
// @Success 200 {object} auth_dto.User
// @Bind user local key(__ctx_user)
func (u *User) Me(ctx fiber.Ctx, user *models.User) (*auth_dto.User, error) {
	// uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
	return services.User.ToAuthUserDTO(user), nil
}

// Update user profile
//
// @Router /t/:tenantCode/v1/me [put]
// @Summary Update user profile
// @Description Update user profile
// @Tags UserCenter
// @Accept json
// @Produce json
// @Param form body dto.UserUpdate true "Update form"
// @Success 200 {string} string "Updated"
// @Bind user local key(__ctx_user)
// @Bind form body
func (u *User) Update(ctx fiber.Ctx, user *models.User, form *dto.UserUpdate) error {
	return services.User.Update(ctx, user.ID, form)
}

// Submit real-name authentication
//
// @Router /t/:tenantCode/v1/me/realname [post]
// @Summary Realname auth
// @Description Submit real-name authentication
// @Tags UserCenter
// @Accept json
// @Produce json
// @Param form body dto.RealNameForm true "Realname form"
// @Success 200 {string} string "Submitted"
// @Bind user local key(__ctx_user)
// @Bind form body
func (u *User) RealName(ctx fiber.Ctx, user *models.User, form *dto.RealNameForm) error {
	return services.User.RealName(ctx, user.ID, form)
}

// Get wallet balance and transactions
//
// @Router /t/:tenantCode/v1/me/wallet [get]
// @Summary Get wallet
// @Description Get wallet balance and transactions
// @Tags UserCenter
// @Accept json
// @Produce json
// @Success 200 {object} dto.WalletResponse
// @Bind user local key(__ctx_user)
func (u *User) Wallet(ctx fiber.Ctx, user *models.User) (*dto.WalletResponse, error) {
	tenantID := getTenantID(ctx)
	return services.Wallet.GetWallet(ctx, tenantID, user.ID)
}

// Recharge wallet
//
// @Router /t/:tenantCode/v1/me/wallet/recharge [post]
// @Summary Recharge wallet
// @Description Recharge wallet
// @Tags UserCenter
// @Accept json
// @Produce json
// @Param form body dto.RechargeForm true "Recharge form"
// @Success 200 {object} dto.RechargeResponse
// @Bind user local key(__ctx_user)
// @Bind form body
func (u *User) Recharge(ctx fiber.Ctx, user *models.User, form *dto.RechargeForm) (*dto.RechargeResponse, error) {
	tenantID := getTenantID(ctx)
	return services.Wallet.Recharge(ctx, tenantID, user.ID, form)
}

// List user orders
//
// @Router /t/:tenantCode/v1/me/orders [get]
// @Summary List orders
// @Description List user orders
// @Tags UserCenter
// @Accept json
// @Produce json
// @Param status query string false "Status enum(all, unpaid, completed, refund)"
// @Success 200 {array} dto.Order
// @Bind user local key(__ctx_user)
// @Bind status query
func (u *User) ListOrders(ctx fiber.Ctx, user *models.User, status string) ([]dto.Order, error) {
	tenantID := getTenantID(ctx)
	return services.Order.ListUserOrders(ctx, tenantID, user.ID, status)
}

// Get user order detail
//
// @Router /t/:tenantCode/v1/me/orders/:id<int> [get]
// @Summary Get order detail
// @Description Get user order detail
// @Tags UserCenter
// @Accept json
// @Produce json
// @Param id path int64 true "Order ID"
// @Success 200 {object} dto.Order
// @Bind user local key(__ctx_user)
// @Bind id path
func (u *User) GetOrder(ctx fiber.Ctx, user *models.User, id int64) (*dto.Order, error) {
	tenantID := getTenantID(ctx)
	return services.Order.GetUserOrder(ctx, tenantID, user.ID, id)
}

// Get purchased content
//
// @Router /t/:tenantCode/v1/me/library [get]
// @Summary Get library
// @Description Get purchased content
// @Tags UserCenter
// @Accept json
// @Produce json
// @Success 200 {array} dto.ContentItem
// @Bind user local key(__ctx_user)
func (u *User) Library(ctx fiber.Ctx, user *models.User) ([]dto.ContentItem, error) {
	tenantID := getTenantID(ctx)
	return services.Content.GetLibrary(ctx, tenantID, user.ID)
}

// Get favorites
//
// @Router /t/:tenantCode/v1/me/favorites [get]
// @Summary Get favorites
// @Description Get favorites
// @Tags UserCenter
// @Accept json
// @Produce json
// @Success 200 {array} dto.ContentItem
// @Bind user local key(__ctx_user)
func (u *User) Favorites(ctx fiber.Ctx, user *models.User) ([]dto.ContentItem, error) {
	tenantID := getTenantID(ctx)
	return services.Content.GetFavorites(ctx, tenantID, user.ID)
}

// Add to favorites
//
// @Router /t/:tenantCode/v1/me/favorites [post]
// @Summary Add favorite
// @Description Add to favorites
// @Tags UserCenter
// @Accept json
// @Produce json
// @Param contentId query int64 true "Content ID"
// @Success 200 {string} string "Added"
// @Bind user local key(__ctx_user)
// @Bind contentId query
func (u *User) AddFavorite(ctx fiber.Ctx, user *models.User, contentId int64) error {
	tenantID := getTenantID(ctx)
	return services.Content.AddFavorite(ctx, tenantID, user.ID, contentId)
}

// Remove from favorites
//
// @Router /t/:tenantCode/v1/me/favorites/:contentId<int> [delete]
// @Summary Remove favorite
// @Description Remove from favorites
// @Tags UserCenter
// @Accept json
// @Produce json
// @Param contentId path int64 true "Content ID"
// @Success 200 {string} string "Removed"
// @Bind user local key(__ctx_user)
// @Bind contentId path
func (u *User) RemoveFavorite(ctx fiber.Ctx, user *models.User, contentId int64) error {
	tenantID := getTenantID(ctx)
	return services.Content.RemoveFavorite(ctx, tenantID, user.ID, contentId)
}

// Get liked contents
//
// @Router /t/:tenantCode/v1/me/likes [get]
// @Summary Get likes
// @Description Get liked contents
// @Tags UserCenter
// @Accept json
// @Produce json
// @Success 200 {array} dto.ContentItem
// @Bind user local key(__ctx_user)
func (u *User) Likes(ctx fiber.Ctx, user *models.User) ([]dto.ContentItem, error) {
	tenantID := getTenantID(ctx)
	return services.Content.GetLikes(ctx, tenantID, user.ID)
}

// Like content
//
// @Router /t/:tenantCode/v1/me/likes [post]
// @Summary Like content
// @Description Like content
// @Tags UserCenter
// @Accept json
// @Produce json
// @Param contentId query int64 true "Content ID"
// @Success 200 {string} string "Liked"
// @Bind user local key(__ctx_user)
// @Bind contentId query
func (u *User) AddLike(ctx fiber.Ctx, user *models.User, contentId int64) error {
	tenantID := getTenantID(ctx)
	return services.Content.AddLike(ctx, tenantID, user.ID, contentId)
}

// Unlike content
//
// @Router /t/:tenantCode/v1/me/likes/:contentId<int> [delete]
// @Summary Unlike content
// @Description Unlike content
// @Tags UserCenter
// @Accept json
// @Produce json
// @Param contentId path int64 true "Content ID"
// @Success 200 {string} string "Unliked"
// @Bind user local key(__ctx_user)
// @Bind contentId path
func (u *User) RemoveLike(ctx fiber.Ctx, user *models.User, contentId int64) error {
	tenantID := getTenantID(ctx)
	return services.Content.RemoveLike(ctx, tenantID, user.ID, contentId)
}

// Get following tenants
//
// @Router /t/:tenantCode/v1/me/following [get]
// @Summary Get following
// @Description Get following tenants
// @Tags UserCenter
// @Accept json
// @Produce json
// @Success 200 {array} dto.TenantProfile
// @Bind user local key(__ctx_user)
func (u *User) Following(ctx fiber.Ctx, user *models.User) ([]dto.TenantProfile, error) {
	tenantID := getTenantID(ctx)
	return services.Tenant.ListFollowed(ctx, tenantID, user.ID)
}

// Get notifications
//
// @Router /t/:tenantCode/v1/me/notifications [get]
// @Summary Get notifications
// @Description Get notifications
// @Tags UserCenter
// @Accept json
// @Produce json
// @Param type query string false "Type enum(all, system, order, audit, interaction)"
// @Param page query int false "Page number"
// @Success 200 {object} requests.Pager{items=[]dto.Notification}
// @Bind user local key(__ctx_user)
// @Bind typeArg query key(type)
// @Bind page query
func (u *User) Notifications(ctx fiber.Ctx, user *models.User, typeArg string, page int) (*requests.Pager, error) {
	tenantID := getTenantID(ctx)
	return services.Notification.List(ctx, tenantID, user.ID, page, typeArg)
}

// Mark notification as read
//
// @Router /t/:tenantCode/v1/me/notifications/:id<int>/read [post]
// @Summary Mark as read
// @Tags UserCenter
// @Accept json
// @Produce json
// @Param id path int64 true "Notification ID"
// @Success 200 {string} string "OK"
// @Bind user local key(__ctx_user)
// @Bind id path
func (u *User) MarkNotificationRead(ctx fiber.Ctx, user *models.User, id int64) error {
	tenantID := getTenantID(ctx)
	return services.Notification.MarkRead(ctx, tenantID, user.ID, id)
}

// Mark all notifications as read
//
// @Router /t/:tenantCode/v1/me/notifications/read-all [post]
// @Summary Mark all as read
// @Tags UserCenter
// @Accept json
// @Produce json
// @Success 200 {string} string "OK"
// @Bind user local key(__ctx_user)
func (u *User) MarkAllNotificationsRead(ctx fiber.Ctx, user *models.User) error {
	tenantID := getTenantID(ctx)
	return services.Notification.MarkAllRead(ctx, tenantID, user.ID)
}

// List my coupons
//
// @Router /t/:tenantCode/v1/me/coupons [get]
// @Summary List coupons
// @Description List my coupons
// @Tags UserCenter
// @Accept json
// @Produce json
// @Param status query string false "Status (unused, used, expired)"
// @Success 200 {array} dto.UserCouponItem
// @Bind user local key(__ctx_user)
// @Bind status query
func (u *User) MyCoupons(ctx fiber.Ctx, user *models.User, status string) ([]dto.UserCouponItem, error) {
	tenantID := getTenantID(ctx)
	return services.Coupon.ListUserCoupons(ctx, tenantID, user.ID, status)
}

// List available coupons for order amount
//
// @Router /t/:tenantCode/v1/me/coupons/available [get]
// @Summary List available coupons
// @Description List coupons available for the given order amount
// @Tags UserCenter
// @Accept json
// @Produce json
// @Param amount query int64 true "Order amount (cents)"
// @Success 200 {array} dto.UserCouponItem
// @Bind user local key(__ctx_user)
// @Bind amount query
func (u *User) AvailableCoupons(ctx fiber.Ctx, user *models.User, amount int64) ([]dto.UserCouponItem, error) {
	tenantID := getTenantID(ctx)
	return services.Coupon.ListAvailable(ctx, tenantID, user.ID, amount)
}

// Receive coupon
//
// @Router /t/:tenantCode/v1/me/coupons/receive [post]
// @Summary Receive coupon
// @Description Receive a coupon by coupon_id
// @Tags UserCenter
// @Accept json
// @Produce json
// @Param form body dto.CouponReceiveForm true "Receive form"
// @Success 200 {object} dto.UserCouponItem
// @Bind user local key(__ctx_user)
// @Bind form body
func (u *User) ReceiveCoupon(ctx fiber.Ctx, user *models.User, form *dto.CouponReceiveForm) (*dto.UserCouponItem, error) {
	tenantID := getTenantID(ctx)
	if form == nil {
		return nil, errorx.ErrInvalidParameter.WithMsg("参数无效")
	}
	return services.Coupon.Receive(ctx, tenantID, user.ID, form.CouponID)
}
