package drive

import (
	"context"

	"github.com/ultisuite/ulti-backend/internal/driveroot"
	"github.com/ultisuite/ulti-backend/internal/nextcloud"
)

func (s *Service) CreateShareAtRoot(ctx context.Context, ncUserID string, ref driveroot.Ref, req createShareRequest, permissions int) (*nextcloud.ShareInfo, error) {
	if usesAlternateRoot(ref) {
		file, err := s.StatFileAtRoot(ctx, ncUserID, ref)
		if err != nil {
			return nil, err
		}
		return s.CreateShare(ctx, ncUserID, file.Path, req, permissions)
	}
	return s.CreateShare(ctx, ncUserID, ref.Path, req, permissions)
}

func (s *Service) SetFavoriteAtRoot(ctx context.Context, ncUserID string, ref driveroot.Ref, favorite bool) error {
	if usesAlternateRoot(ref) {
		// Favorites are only supported on personal NC file paths today.
		file, err := s.StatFileAtRoot(ctx, ncUserID, ref)
		if err != nil {
			return err
		}
		return s.SetFavorite(ctx, ncUserID, file.Path, favorite)
	}
	return s.SetFavorite(ctx, ncUserID, ref.Path, favorite)
}

func (s *Service) ListSharesAtRoot(ctx context.Context, ncUserID string, ref driveroot.Ref) ([]nextcloud.ShareInfo, error) {
	if usesAlternateRoot(ref) {
		file, err := s.StatFileAtRoot(ctx, ncUserID, ref)
		if err != nil {
			return nil, err
		}
		return s.ListShares(ctx, ncUserID, file.Path)
	}
	return s.ListShares(ctx, ncUserID, ref.Path)
}
