package site

import (
	"context"

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

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

// ListSitesUseCase handles listing sites for a tenant
type ListSitesUseCase struct {
	repo   domainsite.Repository
	logger *zap.Logger
}

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

// ListSitesInput is the input for listing sites
type ListSitesInput struct {
	PageSize  int
	PageState []byte
}

// ListSitesOutput is the output after listing sites
type ListSitesOutput struct {
	Sites     []*domainsite.Site
	PageState []byte
}

// Execute lists all sites for a tenant
func (uc *ListSitesUseCase) Execute(ctx context.Context, tenantID gocql.UUID, input *ListSitesInput) (*ListSitesOutput, error) {
	pageSize := input.PageSize
	if pageSize == 0 {
		pageSize = 20 // Default page size
	}

	sites, nextPageState, err := uc.repo.ListByTenant(ctx, tenantID, pageSize, input.PageState)
	if err != nil {
		uc.logger.Error("failed to list sites", zap.Error(err))
		return nil, err
	}

	return &ListSitesOutput{
		Sites:     sites,
		PageState: nextPageState,
	}, nil
}
