<?php
namespace EasyChat\Library\Ai;

use EasyChat\Library\Messenger\MessengerLogger;
use EasyChat\Library\Messenger\ReplyCapability;
use EasyChat\Model\Session;








class AiReplyEligibilityGuard {

    private $registry;

    public function __construct($registry) {
        $this->registry = $registry;
    }

    public function checkChatId(int $chat_id): array {
        if ($chat_id <= 0) {
            return [
                'allowed' => false,
                'reason' => 'chat_missing',
                'reply_capability' => [],
            ];
        }

        $reply_capability = (new ReplyCapability($this->registry))->forChatId($chat_id);
        if (empty($reply_capability['can_auto_reply'])) {
            $read_only = empty($reply_capability['can_reply']);
            return [
                'allowed' => false,
                'reason' => $read_only ? 'ai_reply_read_only' : 'ai_auto_reply_not_allowed',
                'detail' => (string)($reply_capability['auto_reply_reason'] ?? $reply_capability['reason'] ?? 'channel_policy'),
                'reply_capability' => $reply_capability,
            ];
        }

        return [
            'allowed' => true,
            'reason' => '',
            'reply_capability' => $reply_capability,
        ];
    }

    public function logSkip(int $chat_id, int $message_id, string $provider_code, string $layer, array $eligibility): void {
        $provider_code = strtolower(preg_replace('/[^a-z0-9_\-]/i', '', $provider_code));
        if ($provider_code === '') {
            $provider_code = $this->providerForChat($chat_id);
        }
        if ($provider_code === '') {
            $provider_code = 'max';
        }

        $reply_capability = is_array($eligibility['reply_capability'] ?? null)
            ? $eligibility['reply_capability']
            : [];

        try {
            $eligibility_reason = (string)($eligibility['reason'] ?? '');
            $event = $eligibility_reason === 'ai_reply_connected_channel'
                ? 'ai_reply_skipped_connected_channel'
                : ($eligibility_reason === 'ai_auto_reply_not_allowed'
                    ? 'ai_reply_skipped_channel_policy'
                    : 'ai_reply_skipped_read_only');
            (new MessengerLogger($this->registry, $provider_code))->info($event, [
                'provider' => $provider_code,
                'chat_id' => $chat_id,
                'message_id' => $message_id,
                'layer' => $layer,
                'reason' => (string)($eligibility['detail'] ?? $eligibility['reason'] ?? 'read_only'),
                'chat_type' => (string)($reply_capability['chat_type'] ?? ''),
            ]);
        } catch (\Throwable $e) {
             
        }
    }

    private function providerForChat(int $chat_id): string {
        if ($chat_id <= 0) {
            return '';
        }

        try {
            $chat = (new Session($this->registry))->getSessionById($chat_id);
        } catch (\Throwable $e) {
            return '';
        }

        return strtolower(trim((string)($chat['source_provider'] ?? '')));
    }
}
