// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1

package brand

import (
	"context"

	cmatrixusecase "haixun-backend/internal/model/content_matrix/domain/usecase"
	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"

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

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

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

func (l *GetBrandContentMatrixLogic) GetBrandContentMatrix(req *types.BrandPath) (resp *types.ContentMatrixData, err error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	if _, err := l.svcCtx.Brand.Get(l.ctx, tenantID, uid, req.ID); err != nil {
		return nil, err
	}
	matrix, err := l.svcCtx.ContentMatrix.Get(l.ctx, tenantID, uid, req.ID)
	if err != nil {
		return nil, err
	}
	return toContentMatrixData(matrix), nil
}

func toContentMatrixData(matrix *cmatrixusecase.MatrixSummary) *types.ContentMatrixData {
	if matrix == nil {
		return &types.ContentMatrixData{Rows: []types.ContentMatrixRowData{}}
	}
	rows := make([]types.ContentMatrixRowData, 0, len(matrix.Rows))
	for _, row := range matrix.Rows {
		rows = append(rows, types.ContentMatrixRowData{
			SortOrder:        row.SortOrder,
			SearchTag:        row.SearchTag,
			Angle:            row.Angle,
			Hook:             row.Hook,
			Text:             row.Text,
			ReferenceNotes:   row.ReferenceNotes,
			SourcePermalinks: row.SourcePermalinks,
			Rationale:        row.Rationale,
		})
	}
	return &types.ContentMatrixData{
		ID:          matrix.ID,
		BrandID:     matrix.BrandID,
		Rows:        rows,
		GeneratedAt: matrix.GeneratedAt,
		CreateAt:    matrix.CreateAt,
		UpdateAt:    matrix.UpdateAt,
	}
}
