package logging

import (
	"context"
	"fmt"
	"time"

	"gogs.dmsc.dev/arp/auth"
)

// LogOperation logs a CRUD operation to the console
// Format: <datetime> <user> <operation> <entity_info>
func LogOperation(ctx context.Context, operation string, entityInfo string) {
	// Get user email from context if available
	userEmail := "anonymous"

	user, err := auth.CurrentUser(ctx)
	if err == nil && user != nil {
		userEmail = user.Email
	}

	timestamp := time.Now().Format("2006-01-02 15:04:05")
	fmt.Printf("%s %s %s %s\n", timestamp, userEmail, operation, entityInfo)
}

// LogMutation logs a mutation operation (create/update/delete)
func LogMutation(ctx context.Context, action string, entityType string, identifier string) {
	operation := fmt.Sprintf("%s_%s", action, entityType)
	LogOperation(ctx, operation, identifier)
}

// LogQuery logs a query operation
func LogQuery(ctx context.Context, queryType string, details string) {
	operation := fmt.Sprintf("QUERY_%s", queryType)
	LogOperation(ctx, operation, details)
}
