<?php

namespace Discord\Actions;

use Discord\DiscordSender;
use Discord\Converters\HtmlToDiscordMarkdownConverter;

class DiscordActions
{
    private DiscordSender $sender;
    private HtmlToDiscordMarkdownConverter $converter;

    public function __construct()
    {
        $this->sender = new DiscordSender();
        $this->converter = new HtmlToDiscordMarkdownConverter();
    }

    public function sendTemplate(string $channelId, string $htmlContent, ?string $command = null): array
    {
        $content = $this->converter->convert($htmlContent);
        $images = $this->converter->extractImages($htmlContent);
        
        $contentWithoutImages = $this->converter->removeImages($htmlContent);
        $content = $this->converter->convert($contentWithoutImages);

        if (!empty($images)) {
            return $this->sender->sendMessageWithImages($channelId, $content, $images);
        }

        return $this->sender->sendMessage($channelId, $content);
    }

    public function sendScheduledMessage(string $channelId, string $htmlContent, ?array $buttons = null): array
    {
        $content = $this->converter->convert($htmlContent);
        $images = $this->converter->extractImages($htmlContent);
        
        $contentWithoutImages = $this->converter->removeImages($htmlContent);
        $content = $this->converter->convert($contentWithoutImages);

        if (!empty($images)) {
            return $this->sender->sendMessageWithImages($channelId, $content, $images);
        }

        return $this->sender->sendMessage($channelId, $content, null, $buttons);
    }

    public function sendWithTranslation(string $channelId, string $htmlContent, array $translations): array
    {
        $content = $this->converter->convert($htmlContent);
        
        $embed = [
            'title' => '📝 Mensaje Original',
            'description' => $content,
            'color' => 3447003,
            'footer' => [
                'text' => 'Traducciones disponibles en los botones'
            ]
        ];

        $buttons = [];
        foreach ($translations as $lang => $translatedText) {
            $buttons[] = [
                'label' => strtoupper($lang),
                'custom_id' => "translate_{$lang}",
                'style' => 1
            ];
        }

        return $this->sender->sendMessage($channelId, '', $embed, $buttons);
    }

    public function translateMessage(string $channelId, string $messageId, string $originalText, string $targetLang, string $translatedText): array
    {
        $embed = [
            'title' => "🌐 Traducción ({strtoupper($targetLang)})",
            'description' => $translatedText,
            'color' => 3066993,
            'fields' => [
                [
                    'name' => 'Original',
                    'value' => mb_substr($originalText, 0, 1024),
                    'inline' => false
                ]
            ]
        ];

        return $this->sender->sendMessage($channelId, '', $embed);
    }

    public function handleButtonInteraction(string $channelId, string $messageId, string $customId): array
    {
        if (str_starts_with($customId, 'translate_')) {
            $lang = str_replace('translate_', '', $customId);
            return ['action' => 'translate', 'lang' => $lang];
        }

        return ['action' => 'unknown'];
    }
}
