package workspaces

import (
	"context"
	"fmt"

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

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

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

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

func (l *ExportWorkspaceReportLogic) ExportWorkspaceReport(req *types.ExportReportReq) (*types.ExportReportData, error) {
	uid, ok := middleware.UIDFrom(l.ctx)
	if !ok || uid <= 0 {
		return nil, fmt.Errorf("unauthorized")
	}
	format := req.Format
	if format == "" {
		format = "pdf"
	}
	filename, ct, url, err := l.svcCtx.Growth.ExportReport(l.ctx, uid, req.WorkspaceId, req.YearMonth, format)
	if err != nil {
		return nil, err
	}
	return &types.ExportReportData{Url: url, Filename: filename, ContentType: ct, Format: format}, nil
}
