package net.siegeln.cameleer.saas.audit;

import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.UUID;

@Service
public class AuditService {

    private final AuditRepository auditRepository;

    public AuditService(AuditRepository auditRepository) {
        this.auditRepository = auditRepository;
    }

    public void log(UUID actorId, String actorEmail, UUID tenantId,
                    AuditAction action, String resource,
                    String environment, String sourceIp,
                    String result, Map<String, Object> metadata) {
        var entry = new AuditEntity();
        entry.setActorId(actorId);
        entry.setActorEmail(actorEmail);
        entry.setTenantId(tenantId);
        entry.setAction(action.name());
        entry.setResource(resource);
        entry.setEnvironment(environment);
        entry.setSourceIp(sourceIp);
        entry.setResult(result != null ? result : "SUCCESS");
        entry.setMetadata(metadata);
        auditRepository.save(entry);
    }
}
