<?php

namespace PloverCRM\Core\Api\Services;

use PloverCRM\Common\BaseLogger;

/**
 * API Logger Service
 * 
 * Handles logging for REST API operations
 */
class Logger extends BaseLogger
{
    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct(
            'api',      // Channel name
            'api',      // Log directory name
            'api',      // Log file prefix
            [
                'module' => 'system',
                'feature' => 'api',
                'persist' => false,
            ]
        );
    }

    /**
     * Log API request
     *
     * @param string $endpoint
     * @param string $method
     * @param array $params
     */
    public function logRequest(string $endpoint, string $method, array $params = []): void
    {
        $this->logInfo('API request received', [
            'endpoint' => $endpoint,
            'method' => $method,
            'params' => $params
        ]);
    }

    /**
     * Log API response
     *
     * @param string $endpoint
     * @param int $status_code
     * @param float $execution_time
     */
    public function logResponse(string $endpoint, int $status_code, float $execution_time): void
    {
        $this->logInfo('API response sent', [
            'endpoint' => $endpoint,
            'status_code' => $status_code,
            'execution_time_ms' => round($execution_time * 1000, 2)
        ]);
    }

    /**
     * Log API error
     *
     * @param string $endpoint
     * @param string $error
     * @param array $context
     */
    public function logApiError(string $endpoint, string $error, array $context = []): void
    {
        $this->logError('API error', array_merge([
            'endpoint' => $endpoint,
            'error' => $error
        ], $context));
    }

    /**
     * Log permission denied
     *
     * @param string $endpoint
     * @param int $user_id
     */
    public function logPermissionDenied(string $endpoint, int $user_id = 0): void
    {
        $this->logWarning('API permission denied', [
            'endpoint' => $endpoint,
            'user_id' => $user_id
        ]);
    }
}
