package page

import (
	"context"

	"go.uber.org/zap"

	domainpage "codeberg.org/mapleopentech/monorepo/cloud/maplepress-backend/internal/domain/page"
)

// UpsertPageUseCase saves or updates a page in the repository
type UpsertPageUseCase struct {
	pageRepo domainpage.Repository
	logger   *zap.Logger
}

// ProvideUpsertPageUseCase creates a new UpsertPageUseCase
func ProvideUpsertPageUseCase(
	pageRepo domainpage.Repository,
	logger *zap.Logger,
) *UpsertPageUseCase {
	return &UpsertPageUseCase{
		pageRepo: pageRepo,
		logger:   logger,
	}
}

// Execute saves or updates a page in the database
func (uc *UpsertPageUseCase) Execute(ctx context.Context, page *domainpage.Page) error {
	if err := uc.pageRepo.Upsert(ctx, page); err != nil {
		uc.logger.Error("failed to upsert page",
			zap.String("page_id", page.PageID),
			zap.Error(err))
		return err
	}

	return nil
}
