package page

import (
	"context"

	"github.com/gocql/gocql"
	"go.uber.org/zap"

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

// ValidateSiteForSearchUseCase validates that a site exists and is authorized for search
type ValidateSiteForSearchUseCase struct {
	siteRepo domainsite.Repository
	logger   *zap.Logger
}

// ProvideValidateSiteForSearchUseCase creates a new ValidateSiteForSearchUseCase
func ProvideValidateSiteForSearchUseCase(
	siteRepo domainsite.Repository,
	logger *zap.Logger,
) *ValidateSiteForSearchUseCase {
	return &ValidateSiteForSearchUseCase{
		siteRepo: siteRepo,
		logger:   logger,
	}
}

// Execute validates the site for search operations
func (uc *ValidateSiteForSearchUseCase) Execute(
	ctx context.Context,
	tenantID, siteID gocql.UUID,
) (*domainsite.Site, error) {
	// Get site from repository
	site, err := uc.siteRepo.GetByID(ctx, tenantID, siteID)
	if err != nil {
		uc.logger.Error("failed to get site", zap.Error(err))
		return nil, domainsite.ErrSiteNotFound
	}

	// Verify site is verified (skip for test mode)
	if site.RequiresVerification() && !site.IsVerified {
		uc.logger.Warn("site not verified", zap.String("site_id", siteID.String()))
		return nil, domainsite.ErrSiteNotVerified
	}

	return site, nil
}
