<?php

declare(strict_types=1);

namespace App\Audit;

use App\Models\DberpAuditLog;
use DBCore\Audit\Loggers\DatabaseAuditLogger;

/**
 * DBErp 审计写入：在 DBCore 基础上追加 correlation_id 等宿主字段。
 */
final class DberpDatabaseAuditLogger extends DatabaseAuditLogger
{
    public function __construct(string $modelClass = DberpAuditLog::class)
    {
        parent::__construct($modelClass);
    }

    /**
     * @param  array<string, mixed>  $context
     * @return array<string, mixed>
     */
    protected function mapContextToAttributes(string $action, array $context): array
    {
        $attributes = parent::mapContextToAttributes($action, $context);

        if (isset($context['correlation_id']) && is_string($context['correlation_id']) && $context['correlation_id'] !== '') {
            $attributes['correlation_id'] = substr($context['correlation_id'], 0, 36);
        }

        return $attributes;
    }
}
