package main

import (
	"log"
	"net/http"

	"github.com/FernandoVideira/LK_API_Temp/internal/store"
)

// GetFeed godoc
//
//	@Summary		Fetches the user feed
//	@Description	Fetches the user feed
//	@Tags			feed
//	@Accept			json
//	@Produce		json
//	@Param			limit	query		int		false	"Limit"
//	@Param			offset	query		int		false	"Offset"
//	@Param			sort	query		string	false	"Sort"
//	@Param			tags	query		string	false	"Tags"
//	@Param			search	query		string	false	"Search"
//	@Param			since	query		string	false	"Since"
//	@Param			until	query		string	false	"Until"
//	@Success		200		{object}	[]store.PostWithMetadata
//	@Failure		400		{object}	error
//	@Failure		500		{object}	error
//	@Security		ApiKeyAuth
//	@Router			/users/feed [get]
func (api *api) getUserFeedHandler(w http.ResponseWriter, r *http.Request) {
	fq := store.PaginatedFeedQuery{
		Limit:  20,
		Offset: 0,
		Sort:   "desc",
		Tags:   []string{},
		Search: "",
	}

	fq, err := fq.Parse(r)
	if err != nil {
		api.badRequestError(w, r, err)
		return
	}

	if err := Validate.Struct(fq); err != nil {
		api.badRequestError(w, r, err)
		return
	}

	ctx := r.Context()
	user := getUserFromContext(r)
	log.Println(user)
	feed, err := api.store.Posts.GetUserFeed(ctx, user.ID, fq)
	if err != nil {
		api.internalServerError(w, r, err)
		return
	}

	if err := api.jsonResponse(w, http.StatusOK, feed); err != nil {
		api.internalServerError(w, r, err)
	}
}
