package auth

import (
	"context"

	"github.com/rs/zerolog/log"
)

// setupAuditRepo is the subset of SetupAuditRepository needed by SetupAuditor.
type setupAuditRepo interface {
	RecordSetupChange(ctx context.Context, action, section, display, userID string) error
}

// SetupAuditor records admin setup changes to the audit trail.
type SetupAuditor struct {
	repo setupAuditRepo
}

// NewSetupAuditor creates a new SetupAuditor.
func NewSetupAuditor(repo setupAuditRepo) *SetupAuditor {
	return &SetupAuditor{repo: repo}
}

// Record logs a setup change using the caller's context. When the caller is in
// a transaction, the audit row participates in that same transaction; failures
// are logged but remain best-effort and do not fail the setup operation.
func (a *SetupAuditor) Record(ctx context.Context, action, section, display, userID string) {
	if a == nil || a.repo == nil {
		return
	}
	if err := a.repo.RecordSetupChange(ctx, action, section, display, userID); err != nil {
		log.Warn().Err(err).Str("action", action).Str("section", section).Msg("failed to record setup audit trail")
	}
}
