package v1

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

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

// @provider
type Content struct{}

// List contents (Explore / Search)
//
// @Router /t/:tenantCode/v1/contents [get]
// @Summary List contents
// @Description List contents with filtering and pagination
// @Tags Content
// @Accept json
// @Produce json
// @Param keyword query string false "Search keyword"
// @Param genre query string false "Genre"
// @Param tenant_id query int64 false "Filter by creator"
// @Param sort query string false "Sort order" Enums(latest, hot, price_asc)
// @Param page query int false "Page number"
// @Success 200 {object} requests.Pager{items=[]dto.ContentItem}
// @Bind filter query
func (c *Content) List(
	ctx fiber.Ctx,
	filter *dto.ContentListFilter,
) (*requests.Pager, error) {
	tenantID := getTenantID(ctx)
	if tenantID > 0 {
		if filter.TenantID != nil && *filter.TenantID != tenantID {
			return nil, errorx.ErrForbidden.WithMsg("租户不匹配")
		}
		filter.TenantID = &tenantID
	}
	return services.Content.List(ctx, tenantID, filter)
}

// Get content detail
//
// @Router /t/:tenantCode/v1/contents/:id<int> [get]
// @Summary Get content detail
// @Description Get content detail by ID
// @Tags Content
// @Accept json
// @Produce json
// @Param id path int64 true "Content ID"
// @Success 200 {object} dto.ContentDetail
// @Bind id path
func (c *Content) Get(ctx fiber.Ctx, id int64) (*dto.ContentDetail, error) {
	tenantID := getTenantID(ctx)
	uid := getUserID(ctx)
	return services.Content.Get(ctx, tenantID, uid, id)
}

// Get comments for a content
//
// @Router /t/:tenantCode/v1/contents/:id<int>/comments [get]
// @Summary Get comments
// @Description Get comments for a content
// @Tags Content
// @Accept json
// @Produce json
// @Param id path int64 true "Content ID"
// @Param page query int false "Page number"
// @Success 200 {object} requests.Pager{items=[]dto.Comment}
// @Bind id path
// @Bind page query
func (c *Content) ListComments(ctx fiber.Ctx, id int64, page int) (*requests.Pager, error) {
	tenantID := getTenantID(ctx)
	uid := getUserID(ctx)
	return services.Content.ListComments(ctx, tenantID, uid, id, page)
}

// Post a comment
//
// @Router /t/:tenantCode/v1/contents/:id<int>/comments [post]
// @Summary Post comment
// @Description Post a comment to a content
// @Tags Content
// @Accept json
// @Produce json
// @Param id path int64 true "Content ID"
// @Param form body dto.CommentCreateForm true "Comment form"
// @Success 200 {string} string "Comment created"
// @Bind id path
// @Bind form body
func (c *Content) CreateComment(ctx fiber.Ctx, id int64, form *dto.CommentCreateForm) error {
	tenantID := getTenantID(ctx)
	uid := getUserID(ctx)
	return services.Content.CreateComment(ctx, tenantID, uid, id, form)
}

// Like a comment
//
// @Router /t/:tenantCode/v1/comments/:id<int>/like [post]
// @Summary Like comment
// @Description Like a comment
// @Tags Content
// @Accept json
// @Produce json
// @Param id path int64 true "Comment ID"
// @Success 200 {string} string "Liked"
// @Bind id path
func (c *Content) LikeComment(ctx fiber.Ctx, id int64) error {
	tenantID := getTenantID(ctx)
	uid := getUserID(ctx)
	return services.Content.LikeComment(ctx, tenantID, uid, id)
}

// Add like
//
// @Router /t/:tenantCode/v1/contents/:id<int>/like [post]
// @Summary Add like
// @Tags Content
// @Param id path int64 true "Content ID"
// @Success 200 {string} string "Liked"
// @Bind id path
func (c *Content) AddLike(ctx fiber.Ctx, id int64) error {
	tenantID := getTenantID(ctx)
	uid := getUserID(ctx)
	return services.Content.AddLike(ctx, tenantID, uid, id)
}

// Remove like
//
// @Router /t/:tenantCode/v1/contents/:id<int>/like [delete]
// @Summary Remove like
// @Tags Content
// @Param id path int64 true "Content ID"
// @Success 200 {string} string "Unliked"
// @Bind id path
func (c *Content) RemoveLike(ctx fiber.Ctx, id int64) error {
	tenantID := getTenantID(ctx)
	uid := getUserID(ctx)
	return services.Content.RemoveLike(ctx, tenantID, uid, id)
}

// Add favorite
//
// @Router /t/:tenantCode/v1/contents/:id<int>/favorite [post]
// @Summary Add favorite
// @Tags Content
// @Param id path int64 true "Content ID"
// @Success 200 {string} string "Favorited"
// @Bind id path
func (c *Content) AddFavorite(ctx fiber.Ctx, id int64) error {
	tenantID := getTenantID(ctx)
	uid := getUserID(ctx)
	return services.Content.AddFavorite(ctx, tenantID, uid, id)
}

// Remove favorite
//
// @Router /t/:tenantCode/v1/contents/:id<int>/favorite [delete]
// @Summary Remove favorite
// @Tags Content
// @Param id path int64 true "Content ID"
// @Success 200 {string} string "Unfavorited"
// @Bind id path
func (c *Content) RemoveFavorite(ctx fiber.Ctx, id int64) error {
	tenantID := getTenantID(ctx)
	uid := getUserID(ctx)
	return services.Content.RemoveFavorite(ctx, tenantID, uid, id)
}

// List curated topics
//
// @Router /t/:tenantCode/v1/topics [get]
// @Summary List topics
// @Description List curated topics
// @Tags Content
// @Accept json
// @Produce json
// @Success 200 {array} dto.Topic
func (c *Content) ListTopics(ctx fiber.Ctx) ([]dto.Topic, error) {
	tenantID := getTenantID(ctx)
	return services.Content.ListTopics(ctx, tenantID)
}
