<?php

namespace EasyLinkAutomations\Actions\MicrosoftTeams;

if (!defined('ABSPATH')) {
    exit;
}

use EasyLinkAutomations\Actions\BaseAction;

final class SendChannelMessageAction extends BaseAction
{
    public static function getSlug(): string
    {
        return 'microsoft-teams/send_channel_message';
    }

    public static function getName(): string
    {
        return __('Send Channel Message', 'easylink-automations');
    }

    public static function getDescription(): string
    {
        return __('Send a message or reply in a Microsoft Teams channel', 'easylink-automations');
    }

    public static function getCategory(): string
    {
        return 'Microsoft Teams';
    }

    public static function getIcon(): string
    {
        return 'microsoft-teams';
    }

    public static function getRequiresConnection(): ?string
    {
        return 'microsoft_teams';
    }

    public static function getConfigFields(): array
    {
        return [
            'team_id' => self::remote('Team', 'microsoft-teams/teams', 'teams', ['connection_id']),
            'channel_id' => self::remote('Channel', 'microsoft-teams/channels', 'channels', ['connection_id', 'team_id']),
            'content_type' => [
                'label' => 'Content Type', 'type' => 'select', 'required' => true, 'default' => 'text',
                'options' => [
                    ['value' => 'text', 'label' => 'Text'],
                    ['value' => 'html', 'label' => 'HTML'],
                ],
            ],
            'message' => [
                'label' => 'Message', 'type' => 'textarea', 'required' => true,
                'supports_mapping' => true, 'placeholder' => 'Hello from EasyLink Automations!',
            ],
            'reply_to_id' => [
                'label' => 'Reply to Message ID', 'type' => 'text', 'required' => false,
                'supports_mapping' => true, 'placeholder' => 'Optional channel message ID',
                'description' => 'When set, sends the content as a reply to this channel message.',
            ],
        ];
    }

    public function execute(array $triggerData, array $config): array
    {
        $mapped = $this->mapConfig($config, $triggerData);
        $missing = $this->validateRequired($mapped);
        if ($missing) {
            return ['success' => false, 'message' => 'Missing required fields after mapping: ' . implode(', ', $missing), 'data' => []];
        }

        $teamId = MicrosoftTeamsRequestHelper::id($mapped['team_id']);
        $channelId = MicrosoftTeamsRequestHelper::id($mapped['channel_id']);
        $path = '/v1.0/teams/' . $teamId . '/channels/' . $channelId . '/messages';
        if (!empty($mapped['reply_to_id'])) {
            $path .= '/' . MicrosoftTeamsRequestHelper::id($mapped['reply_to_id']) . '/replies';
        }

        return MicrosoftTeamsRequestHelper::request(
            (int) ($config['connection_id'] ?? 0),
            'POST',
            $path,
            ['body' => ['contentType' => $mapped['content_type'] === 'html' ? 'html' : 'text', 'content' => (string) $mapped['message']]]
        );
    }

    private static function remote(string $label, string $source, string $key, array $dependsOn): array
    {
        return [
            'label' => $label, 'type' => 'remote_select', 'required' => true, 'supports_mapping' => true,
            'remote_source' => $source, 'options_key' => $key, 'depends_on' => $dependsOn,
        ];
    }
}
