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

import (
	"go.uber.org/zap"

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

// HardDeleteFileMetadataUseCase permanently deletes file metadata
// Used for GDPR right-to-be-forgotten implementation
type HardDeleteFileMetadataUseCase interface {
	Execute(id gocql.UUID) error
}

type hardDeleteFileMetadataUseCaseImpl struct {
	config *config.Configuration
	logger *zap.Logger
	repo   dom_file.FileMetadataRepository
}

func NewHardDeleteFileMetadataUseCase(
	config *config.Configuration,
	logger *zap.Logger,
	repo dom_file.FileMetadataRepository,
) HardDeleteFileMetadataUseCase {
	logger = logger.Named("HardDeleteFileMetadataUseCase")
	return &hardDeleteFileMetadataUseCaseImpl{config, logger, repo}
}

func (uc *hardDeleteFileMetadataUseCaseImpl) Execute(id gocql.UUID) error {
	//
	// STEP 1: Validation.
	//

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

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

	uc.logger.Info("Hard deleting file metadata (GDPR mode)",
		zap.String("file_id", id.String()))

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

	uc.logger.Info("✅ File metadata hard deleted successfully",
		zap.String("file_id", id.String()))

	return nil
}
