<?php

namespace App\Core\Services\Security;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

/**
 * Security Logger Service
 * Logs security-related events for monitoring and auditing
 */
class SecurityLogger
{
    /**
     * Log login attempt
     */
    public function logLoginAttempt(string $email, bool $success, ?string $ip = null, ?string $userAgent = null): void
    {
        $context = [
            'event' => 'login_attempt',
            'email' => $email,
            'success' => $success,
            'ip' => $ip ?? request()->ip(),
            'user_agent' => $userAgent ?? request()->userAgent(),
            'timestamp' => now()->toDateTimeString(),
        ];

        if ($success) {
            Log::info('Successful login attempt', $context);
        } else {
            Log::warning('Failed login attempt', $context);
        }
    }

    /**
     * Log permission violation
     */
    public function logPermissionViolation(string $action, ?string $resource = null): void
    {
        Log::warning('Permission violation', [
            'event' => 'permission_violation',
            'user_id' => Auth::id(),
            'user_email' => Auth::user()?->email,
            'action' => $action,
            'resource' => $resource,
            'ip' => request()->ip(),
            'user_agent' => request()->userAgent(),
            'timestamp' => now()->toDateTimeString(),
        ]);
    }

    /**
     * Log API abuse
     */
    public function logApiAbuse(string $endpoint, int $attempts, ?string $ip = null): void
    {
        Log::warning('API abuse detected', [
            'event' => 'api_abuse',
            'endpoint' => $endpoint,
            'attempts' => $attempts,
            'ip' => $ip ?? request()->ip(),
            'user_agent' => request()->userAgent(),
            'timestamp' => now()->toDateTimeString(),
        ]);
    }

    /**
     * Log file upload attempt
     */
    public function logFileUpload(string $filename, bool $success, ?string $reason = null): void
    {
        $context = [
            'event' => 'file_upload',
            'user_id' => Auth::id(),
            'filename' => $filename,
            'success' => $success,
            'ip' => request()->ip(),
            'timestamp' => now()->toDateTimeString(),
        ];

        if ($reason) {
            $context['reason'] = $reason;
        }

        if ($success) {
            Log::info('File upload successful', $context);
        } else {
            Log::warning('File upload failed', $context);
        }
    }

    /**
     * Log sensitive action
     */
    public function logSensitiveAction(string $action, array $details = []): void
    {
        Log::info('Sensitive action performed', [
            'event' => 'sensitive_action',
            'user_id' => Auth::id(),
            'user_email' => Auth::user()?->email,
            'action' => $action,
            'details' => $details,
            'ip' => request()->ip(),
            'user_agent' => request()->userAgent(),
            'timestamp' => now()->toDateTimeString(),
        ]);
    }

    /**
     * Log addon installation/activation
     */
    public function logAddonAction(string $action, string $addonSlug, bool $success, ?string $reason = null): void
    {
        $context = [
            'event' => 'addon_action',
            'user_id' => Auth::id(),
            'action' => $action,
            'addon_slug' => $addonSlug,
            'success' => $success,
            'ip' => request()->ip(),
            'timestamp' => now()->toDateTimeString(),
        ];

        if ($reason) {
            $context['reason'] = $reason;
        }

        if ($success) {
            Log::info('Addon action successful', $context);
        } else {
            Log::warning('Addon action failed', $context);
        }
    }

    /**
     * Log security exception
     */
    public function logSecurityException(\Exception $exception, array $context = []): void
    {
        Log::error('Security exception', array_merge([
            'event' => 'security_exception',
            'exception' => get_class($exception),
            'message' => $exception->getMessage(),
            'file' => $exception->getFile(),
            'line' => $exception->getLine(),
            'user_id' => Auth::id(),
            'ip' => request()->ip(),
            'user_agent' => request()->userAgent(),
            'timestamp' => now()->toDateTimeString(),
        ], $context));
    }
}
