package page

import (
	"context"

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

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

// ValidateSiteUseCase validates site status and verification
type ValidateSiteUseCase struct {
	siteRepo domainsite.Repository
	logger   *zap.Logger
}

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

// Execute validates the site and returns it if valid
func (uc *ValidateSiteUseCase) 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
}
