// codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/usecase/filemetadata/restore.go
package filemetadata

import (
	"context"

	"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"
)

type RestoreFileMetadataUseCase interface {
	Execute(ctx context.Context, id gocql.UUID) error
}

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

func NewRestoreFileMetadataUseCase(
	config *config.Configuration,
	logger *zap.Logger,
	repo dom_file.FileMetadataRepository,
) RestoreFileMetadataUseCase {
	logger = logger.Named("RestoreFileMetadataUseCase")
	return &restoreFileMetadataUseCaseImpl{config, logger, repo}
}

func (uc *restoreFileMetadataUseCaseImpl) Execute(ctx context.Context, 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 restoration",
			zap.Any("error", e))
		return httperror.NewForBadRequest(&e)
	}

	//
	// STEP 2: Restore file metadata using repository method.
	//

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

	uc.logger.Info("File metadata successfully restored",
		zap.String("file_id", id.String()))

	return nil
}
