package media

import (
	"context"

	"apps/backend/internal/middleware"
	"apps/backend/internal/response"
	"apps/backend/internal/svc"
	"apps/backend/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GenerateImageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGenerateImageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GenerateImageLogic {
	return &GenerateImageLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}

func (l *GenerateImageLogic) GenerateImage(req *types.GenerateImageReq) (*types.GenerateImageData, error) {

	if l.svcCtx.Inspire == nil {
		return nil, response.Biz(503, 503001, "media generate not configured")
	}
	uid, ok := middleware.UIDFrom(l.ctx)
	if !ok {
		return nil, response.Biz(401, 401001, "missing authorization")
	}
	img, err := l.svcCtx.Inspire.GenerateImage(l.ctx, uid, req.Prompt)
	if err != nil {
		return nil, err
	}
	return &types.GenerateImageData{Id: img.ID, Url: img.URL, Prompt: img.Prompt}, nil

}
