<?php
namespace EasyChat\Library;

class BlacklistService {
    private $registry;
    private $db;
    private $prefix;

    public function __construct($registry) {
        $this->registry = $registry;
        $this->db = $registry->get('db');
        $this->prefix = easychat_db_prefix();
    }

    




    public function isBlocked($ip, $chat_id = 0) {
        if (!$this->db) {
            return false;
        }

         
        $chat_id = (int)$chat_id;
        if ($chat_id > 0) {
            $query = $this->db->query("SELECT `is_blocked` FROM `{$this->prefix}sessions` WHERE `chat_id` = " . $chat_id . " LIMIT 1");
            if ($query && $query->num_rows > 0 && (int)$query->row['is_blocked'] === 1) {
                return true;
            }
        }

         
         
         
        if ($ip !== '') {
            $query = $this->db->query("SELECT COUNT(*) AS total FROM `{$this->prefix}messages` 
                WHERE `ip` = '" . $this->db->escape($ip) . "' 
                  AND `author` = 1 
                  AND `date_added` >= DATE_SUB(NOW(), INTERVAL 5 MINUTE)");
            
            if ($query && (int)$query->row['total'] >= 30) {
                 
                if ($chat_id > 0) {
                    $this->blockChat($chat_id, 1);
                }
                return true;
            }
        }

        return false;
    }

    



    public function checkFlood($ip, $chat_id = 0) {
        $chat_id = (int)$chat_id;
        if ($this->isBlocked($ip, $chat_id)) {
            return true;
        }

        if (!$this->db) {
            return false;
        }

         
         
        if ($ip !== '') {
            $query = $this->db->query("SELECT COUNT(*) AS total FROM `{$this->prefix}messages` 
                WHERE `ip` = '" . $this->db->escape($ip) . "' 
                  AND `author` = 1 
                  AND `date_added` >= DATE_SUB(NOW(), INTERVAL 10 SECOND)");
            
            if ($query && (int)$query->row['total'] >= 5) {
                if ($chat_id > 0) {
                    $this->blockChat($chat_id, 1);
                }
                return true;
            }

             
            $query = $this->db->query("SELECT COUNT(DISTINCT `chat_id`) AS chat_count FROM `{$this->prefix}messages`
                WHERE `ip` = '" . $this->db->escape($ip) . "'
                  AND `author` = 1
                  AND `date_added` >= DATE_SUB(NOW(), INTERVAL 60 SECOND)");
            
            if ($query && (int)$query->row['chat_count'] >= 5) {
                if ($chat_id > 0) {
                    $this->blockChat($chat_id, 1);
                }
                return true;
            }
        }

         
        if ($chat_id > 0) {
            $query = $this->db->query("SELECT COUNT(*) AS total FROM `{$this->prefix}messages` 
                WHERE `chat_id` = " . $chat_id . " 
                  AND `author` = 1 
                  AND `date_added` >= DATE_SUB(NOW(), INTERVAL 10 SECOND)");
            
            if ($query && (int)$query->row['total'] >= 5) {
                $this->blockChat($chat_id, 1);
                return true;
            }
        }

        return false;
    }

    


    public function blockChat($chat_id, $blocked = 1) {
        $chat_id = (int)$chat_id;
        $blocked = (int)$blocked ? 1 : 0;
        if ($chat_id > 0 && $this->db) {
            $this->db->query("UPDATE `{$this->prefix}sessions` SET `is_blocked` = " . $blocked . ", `date_modified` = NOW() WHERE `chat_id` = " . $chat_id);
        }
    }
}
