package collection

import (
	"context"
	"fmt"

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

// IncrementFileCount increments the file count for a collection
func (impl *collectionRepositoryImpl) IncrementFileCount(ctx context.Context, collectionID gocql.UUID) error {
	// Read current file count
	var currentCount int64
	readQuery := `SELECT file_count FROM maplefile.collections_by_id WHERE id = ?`
	if err := impl.Session.Query(readQuery, collectionID).WithContext(ctx).Scan(&currentCount); err != nil {
		if err == gocql.ErrNotFound {
			impl.Logger.Warn("collection not found for file count increment",
				zap.String("collection_id", collectionID.String()))
			return nil // Collection doesn't exist, nothing to increment
		}
		impl.Logger.Error("failed to read file count for increment",
			zap.String("collection_id", collectionID.String()),
			zap.Error(err))
		return fmt.Errorf("failed to read file count: %w", err)
	}

	// Write incremented count
	newCount := currentCount + 1
	updateQuery := `UPDATE maplefile.collections_by_id SET file_count = ? WHERE id = ?`
	if err := impl.Session.Query(updateQuery, newCount, collectionID).WithContext(ctx).Exec(); err != nil {
		impl.Logger.Error("failed to increment file count",
			zap.String("collection_id", collectionID.String()),
			zap.Error(err))
		return fmt.Errorf("failed to increment file count: %w", err)
	}

	impl.Logger.Debug("incremented file count",
		zap.String("collection_id", collectionID.String()),
		zap.Int64("old_count", currentCount),
		zap.Int64("new_count", newCount))

	return nil
}

// DecrementFileCount decrements the file count for a collection
func (impl *collectionRepositoryImpl) DecrementFileCount(ctx context.Context, collectionID gocql.UUID) error {
	// Read current file count
	var currentCount int64
	readQuery := `SELECT file_count FROM maplefile.collections_by_id WHERE id = ?`
	if err := impl.Session.Query(readQuery, collectionID).WithContext(ctx).Scan(&currentCount); err != nil {
		if err == gocql.ErrNotFound {
			impl.Logger.Warn("collection not found for file count decrement",
				zap.String("collection_id", collectionID.String()))
			return nil // Collection doesn't exist, nothing to decrement
		}
		impl.Logger.Error("failed to read file count for decrement",
			zap.String("collection_id", collectionID.String()),
			zap.Error(err))
		return fmt.Errorf("failed to read file count: %w", err)
	}

	// Write decremented count (don't go below 0)
	newCount := currentCount - 1
	if newCount < 0 {
		newCount = 0
	}
	updateQuery := `UPDATE maplefile.collections_by_id SET file_count = ? WHERE id = ?`
	if err := impl.Session.Query(updateQuery, newCount, collectionID).WithContext(ctx).Exec(); err != nil {
		impl.Logger.Error("failed to decrement file count",
			zap.String("collection_id", collectionID.String()),
			zap.Error(err))
		return fmt.Errorf("failed to decrement file count: %w", err)
	}

	impl.Logger.Debug("decremented file count",
		zap.String("collection_id", collectionID.String()),
		zap.Int64("old_count", currentCount),
		zap.Int64("new_count", newCount))

	return nil
}