package site

import (
	"context"

	"go.uber.org/zap"

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

// ValidateDomainUseCase checks if a domain is available for registration
type ValidateDomainUseCase struct {
	repo   domainsite.Repository
	logger *zap.Logger
}

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

// Execute validates if a domain can be used for a new site
func (uc *ValidateDomainUseCase) Execute(ctx context.Context, domain string) error {
	// Check if domain already exists
	exists, err := uc.repo.DomainExists(ctx, domain)
	if err != nil {
		uc.logger.Error("failed to check domain existence",
			zap.String("domain", domain),
			zap.Error(err))
		return err
	}

	if exists {
		uc.logger.Warn("domain already exists", zap.String("domain", domain))
		return domainsite.ErrDomainAlreadyExists
	}

	uc.logger.Info("domain is available", zap.String("domain", domain))
	return nil
}
