package site

import (
	"context"
	"fmt"

	"go.uber.org/zap"

	domainsite "codeberg.org/mapleopentech/monorepo/cloud/maplepress-backend/internal/domain/site"
)

// UpdateSiteToRepoUseCase updates a site in the repository
type UpdateSiteToRepoUseCase struct {
	repo   domainsite.Repository
	logger *zap.Logger
}

// ProvideUpdateSiteToRepoUseCase creates a new UpdateSiteToRepoUseCase
func ProvideUpdateSiteToRepoUseCase(
	repo domainsite.Repository,
	logger *zap.Logger,
) *UpdateSiteToRepoUseCase {
	return &UpdateSiteToRepoUseCase{
		repo:   repo,
		logger: logger,
	}
}

// Execute updates a site in the repository (all tables)
func (uc *UpdateSiteToRepoUseCase) Execute(ctx context.Context, site *domainsite.Site) error {
	if err := uc.repo.Update(ctx, site); err != nil {
		uc.logger.Error("failed to update site in repository",
			zap.String("site_id", site.ID.String()),
			zap.Error(err))
		return fmt.Errorf("failed to update site: %w", err)
	}

	uc.logger.Info("site updated in repository",
		zap.String("site_id", site.ID.String()),
		zap.String("domain", site.Domain))

	return nil
}
