<?php

declare(strict_types=1);

namespace Tests\Debug;

use App\Framework\Logging\ChannelLogger;
use App\Framework\Logging\Logger;
use App\Framework\Logging\LogChannel;
use App\Framework\Logging\LogLevel;
use App\Framework\Logging\ValueObjects\LogContext;

final class TestLogger implements Logger
{
    private array $logs = [];

    public function emergency(string $message, ?LogContext $context = null): void
    {
        $this->log(LogLevel::EMERGENCY, $message, $context);
    }

    public function alert(string $message, ?LogContext $context = null): void
    {
        $this->log(LogLevel::ALERT, $message, $context);
    }

    public function critical(string $message, ?LogContext $context = null): void
    {
        $this->log(LogLevel::CRITICAL, $message, $context);
    }

    public function error(string $message, ?LogContext $context = null): void
    {
        $this->log(LogLevel::ERROR, $message, $context);
    }

    public function warning(string $message, ?LogContext $context = null): void
    {
        $this->log(LogLevel::WARNING, $message, $context);
    }

    public function notice(string $message, ?LogContext $context = null): void
    {
        $this->log(LogLevel::NOTICE, $message, $context);
    }

    public function info(string $message, ?LogContext $context = null): void
    {
        $this->log(LogLevel::INFO, $message, $context);
    }

    public function debug(string $message, ?LogContext $context = null): void
    {
        $this->log(LogLevel::DEBUG, $message, $context);
    }

    public function log(LogLevel $level, string $message, ?LogContext $context = null): void
    {
        $contextArray = $context?->toArray() ?? [];

        $this->logs[] = [
            'level' => $level->value,
            'message' => $message,
            'context' => $contextArray,
            'timestamp' => microtime(true)
        ];

        // Echo für direktes Debugging
        echo "[{$level->value}] $message\n";
        if (!empty($contextArray)) {
            echo "  Context: " . json_encode($contextArray, JSON_PRETTY_PRINT) . "\n";
        }
    }

    public function logToChannel(LogChannel $channel, LogLevel $level, string $message, ?LogContext $context = null): void
    {
        $contextArray = $context?->toArray() ?? [];

        $this->logs[] = [
            'channel' => $channel->value,
            'level' => $level->value,
            'message' => $message,
            'context' => $contextArray,
            'timestamp' => microtime(true)
        ];

        echo "[{$channel->value}:{$level->value}] $message\n";
        if (!empty($contextArray)) {
            echo "  Context: " . json_encode($contextArray, JSON_PRETTY_PRINT) . "\n";
        }
    }

    public function getLogs(): array
    {
        return $this->logs;
    }

    public function clear(): void
    {
        $this->logs = [];
    }

    // Channel logger properties (simplified for testing)
    public ChannelLogger $security {
        get => new class($this) implements ChannelLogger {
            public function __construct(private TestLogger $logger) {}
            public function debug(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::SECURITY, LogLevel::DEBUG, $message, $context);
            }
            public function info(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::SECURITY, LogLevel::INFO, $message, $context);
            }
            public function notice(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::SECURITY, LogLevel::NOTICE, $message, $context);
            }
            public function warning(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::SECURITY, LogLevel::WARNING, $message, $context);
            }
            public function error(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::SECURITY, LogLevel::ERROR, $message, $context);
            }
            public function critical(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::SECURITY, LogLevel::CRITICAL, $message, $context);
            }
            public function alert(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::SECURITY, LogLevel::ALERT, $message, $context);
            }
            public function emergency(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::SECURITY, LogLevel::EMERGENCY, $message, $context);
            }
        };
    }

    public ChannelLogger $cache {
        get => new class($this) implements ChannelLogger {
            public function __construct(private TestLogger $logger) {}
            public function debug(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::CACHE, LogLevel::DEBUG, $message, $context);
            }
            public function info(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::CACHE, LogLevel::INFO, $message, $context);
            }
            public function notice(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::CACHE, LogLevel::NOTICE, $message, $context);
            }
            public function warning(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::CACHE, LogLevel::WARNING, $message, $context);
            }
            public function error(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::CACHE, LogLevel::ERROR, $message, $context);
            }
            public function critical(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::CACHE, LogLevel::CRITICAL, $message, $context);
            }
            public function alert(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::CACHE, LogLevel::ALERT, $message, $context);
            }
            public function emergency(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::CACHE, LogLevel::EMERGENCY, $message, $context);
            }
        };
    }

    public ChannelLogger $database {
        get => new class($this) implements ChannelLogger {
            public function __construct(private TestLogger $logger) {}
            public function debug(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::DATABASE, LogLevel::DEBUG, $message, $context);
            }
            public function info(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::DATABASE, LogLevel::INFO, $message, $context);
            }
            public function notice(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::DATABASE, LogLevel::NOTICE, $message, $context);
            }
            public function warning(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::DATABASE, LogLevel::WARNING, $message, $context);
            }
            public function error(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::DATABASE, LogLevel::ERROR, $message, $context);
            }
            public function critical(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::DATABASE, LogLevel::CRITICAL, $message, $context);
            }
            public function alert(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::DATABASE, LogLevel::ALERT, $message, $context);
            }
            public function emergency(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::DATABASE, LogLevel::EMERGENCY, $message, $context);
            }
        };
    }

    public ChannelLogger $framework {
        get => new class($this) implements ChannelLogger {
            public function __construct(private TestLogger $logger) {}
            public function debug(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::FRAMEWORK, LogLevel::DEBUG, $message, $context);
            }
            public function info(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::FRAMEWORK, LogLevel::INFO, $message, $context);
            }
            public function notice(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::FRAMEWORK, LogLevel::NOTICE, $message, $context);
            }
            public function warning(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::FRAMEWORK, LogLevel::WARNING, $message, $context);
            }
            public function error(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::FRAMEWORK, LogLevel::ERROR, $message, $context);
            }
            public function critical(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::FRAMEWORK, LogLevel::CRITICAL, $message, $context);
            }
            public function alert(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::FRAMEWORK, LogLevel::ALERT, $message, $context);
            }
            public function emergency(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::FRAMEWORK, LogLevel::EMERGENCY, $message, $context);
            }
        };
    }

    public ChannelLogger $error {
        get => new class($this) implements ChannelLogger {
            public function __construct(private TestLogger $logger) {}
            public function debug(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::ERROR, LogLevel::DEBUG, $message, $context);
            }
            public function info(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::ERROR, LogLevel::INFO, $message, $context);
            }
            public function notice(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::ERROR, LogLevel::NOTICE, $message, $context);
            }
            public function warning(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::ERROR, LogLevel::WARNING, $message, $context);
            }
            public function error(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::ERROR, LogLevel::ERROR, $message, $context);
            }
            public function critical(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::ERROR, LogLevel::CRITICAL, $message, $context);
            }
            public function alert(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::ERROR, LogLevel::ALERT, $message, $context);
            }
            public function emergency(string $message, ?LogContext $context = null): void {
                $this->logger->logToChannel(LogChannel::ERROR, LogLevel::EMERGENCY, $message, $context);
            }
        };
    }
}
