package site

import (
	"context"
	"fmt"

	"go.uber.org/zap"

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

// SaveSiteToRepoUseCase saves a site to the repository
type SaveSiteToRepoUseCase struct {
	repo   domainsite.Repository
	logger *zap.Logger
}

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

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

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

	return nil
}
