package book

import (
	"context"
	v1 "server/api/book/v1"
	"server/internal/model"
	"server/internal/service"

	"github.com/gogf/gf/v2/frame/g"
)

func (c *ControllerV1) AppDetail(ctx context.Context, req *v1.AppDetailReq) (res *v1.AppDetailRes, err error) {
	// 从 ctx 获取用户ID
	userId := g.RequestFromCtx(ctx).GetCtxVar("id").Int64()

	out, err := service.Book().AppDetail(ctx, &model.BookAppDetailIn{
		Id:     req.Id,
		UserId: userId,
	})
	if err != nil {
		return nil, err
	}

	return &v1.AppDetailRes{
		Id:             out.Id,
		CoverUrl:       out.CoverUrl,
		Rating:         out.Rating,
		Title:          out.Title,
		Description:    out.Description,
		AuthorId:       out.AuthorId,
		Author:         out.Author,
		CategoryId:     out.CategoryId,
		Category:       out.Category,
		Status:         out.Status,
		WordsCount:     out.WordsCount,
		ChaptersCount:  out.ChaptersCount,
		ReadCount:      out.ReadCount,
		CurrentReaders: out.CurrentReaders,
		Tags:           out.Tags,
		IsRecommended:  out.IsRecommended,
		CreatedAt:      out.CreatedAt.String(),
		UpdatedAt:      out.UpdatedAt.String(),
		HasRated:       out.HasRated,
		MyRating:       out.MyRating,
		HasRead:        out.HasRead,
		ReadProgress:   out.ReadProgress,
		LastChapterId:  out.LastChapterId,
		LastReadAt:     out.LastReadAt,
		IsInBookshelf:  out.IsInBookshelf,
	}, nil
}
