// monorepo/cloud/backend/internal/maplefile/usecase/collection/harddelete.go
package collection

import (
	"context"

	"go.uber.org/zap"

	"github.com/gocql/gocql"
	"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/config"
	dom_collection "codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/domain/collection"
	"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/pkg/httperror"
)

// HardDeleteCollectionUseCase permanently deletes a collection
// Used for GDPR right-to-be-forgotten implementation
type HardDeleteCollectionUseCase interface {
	Execute(ctx context.Context, id gocql.UUID) error
}

type hardDeleteCollectionUseCaseImpl struct {
	config *config.Configuration
	logger *zap.Logger
	repo   dom_collection.CollectionRepository
}

func NewHardDeleteCollectionUseCase(
	config *config.Configuration,
	logger *zap.Logger,
	repo dom_collection.CollectionRepository,
) HardDeleteCollectionUseCase {
	logger = logger.Named("HardDeleteCollectionUseCase")
	return &hardDeleteCollectionUseCaseImpl{config, logger, repo}
}

func (uc *hardDeleteCollectionUseCaseImpl) Execute(ctx context.Context, id gocql.UUID) error {
	//
	// STEP 1: Validation.
	//

	e := make(map[string]string)
	if id.String() == "" {
		e["id"] = "Collection ID is required"
	}
	if len(e) != 0 {
		uc.logger.Warn("Failed validating collection hard deletion",
			zap.Any("error", e))
		return httperror.NewForBadRequest(&e)
	}

	//
	// STEP 2: Hard delete from database (no tombstone).
	//

	uc.logger.Info("Hard deleting collection (GDPR mode)",
		zap.String("collection_id", id.String()))

	err := uc.repo.HardDelete(ctx, id)
	if err != nil {
		uc.logger.Error("Failed to hard delete collection",
			zap.String("collection_id", id.String()),
			zap.Error(err))
		return err
	}

	uc.logger.Info("✅ Collection hard deleted successfully",
		zap.String("collection_id", id.String()))

	return nil
}
