<?php

declare(strict_types=1);

namespace App\Framework\Console\Result;

use App\Framework\Console\ConsoleOutputInterface;
use App\Framework\Console\ConsoleStyle;
use App\Framework\Console\ExitCode;

/**
 * Text Result - Simple text output with styling
 *
 * The most basic console result type for displaying text messages.
 * Supports different styles (success, error, warning, info) via factory methods.
 *
 * Example:
 *   return TextResult::success("User created successfully!");
 *   return TextResult::error("Failed to connect to database");
 */
final readonly class TextResult implements ConsoleResult
{
    public readonly array $data;

    /**
     * Create text result
     *
     * Use factory methods (success/error/warning/info) for convenience.
     */
    public function __construct(
        public readonly string $message,
        public readonly ConsoleStyle $style = ConsoleStyle::INFO,
        public readonly ExitCode $exitCode = ExitCode::SUCCESS,
    ) {
        $this->data = [
            'message' => $this->message,
            'style' => $this->style->value,
            'exit_code' => $this->exitCode->value,
        ];
    }

    /**
     * Create success result (green text, exit code 0)
     */
    public static function success(string $message): self
    {
        return new self($message, ConsoleStyle::SUCCESS, ExitCode::SUCCESS);
    }

    /**
     * Create error result (red text, exit code 1)
     */
    public static function error(string $message): self
    {
        return new self($message, ConsoleStyle::ERROR, ExitCode::FAILURE);
    }

    /**
     * Create warning result (yellow text, exit code 0)
     */
    public static function warning(string $message): self
    {
        return new self($message, ConsoleStyle::WARNING, ExitCode::SUCCESS);
    }

    /**
     * Create info result (blue text, exit code 0)
     */
    public static function info(string $message): self
    {
        return new self($message, ConsoleStyle::INFO, ExitCode::SUCCESS);
    }

    /**
     * Create plain result (no styling, exit code 0)
     */
    public static function plain(string $message): self
    {
        return new self($message, ConsoleStyle::NONE, ExitCode::SUCCESS);
    }

    /**
     * Render text to console output
     */
    public function render(ConsoleOutputInterface $output): void
    {
        $output->writeLine($this->message, $this->style);
    }
}
