<?php

namespace UltraguardPro\modules\ip_intelligence\schema;

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

use UltraguardPro\modules\core\AbstractModuleSchema;

class IpIntelligenceSchema extends AbstractModuleSchema {

    public function get_module_id(): string {
        return 'ip_intelligence';
    }

    public function get_table_map(): array {
        return [
            'threat_feed'         => \UltraguardPro\modules\ip_intelligence\db\ThreatFeedTable::class,
            'ip_reputation_cache' => \UltraguardPro\modules\ip_intelligence\db\IpReputationCacheTable::class,
        ];
    }

    public function get_schemas(): array {
        $cs = $this->charset_collate();

        return [
            $this->table( 'ultraguard_threat_feed' ) =>
            "CREATE TABLE {$this->table('ultraguard_threat_feed')} (
  id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  ip_address varchar(45) NOT NULL,
  ip_type varchar(20) NOT NULL DEFAULT 'ipv4',
  threat_category varchar(50) NOT NULL,
  confidence tinyint(3) UNSIGNED NOT NULL DEFAULT 50,
  source varchar(50) NOT NULL,
  country_code varchar(2) DEFAULT NULL,
  is_active tinyint(1) NOT NULL DEFAULT 1,
  expires_at datetime DEFAULT NULL,
  last_seen_at datetime DEFAULT NULL,
  reports_count int(10) UNSIGNED NOT NULL DEFAULT 0,
  created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY idx_ip_source (ip_address, source),
  KEY idx_is_active (is_active),
  KEY idx_threat_category (threat_category),
  KEY idx_expires_at (expires_at)
) {$cs};",
            $this->table( 'ultraguard_ip_reputation_cache' ) =>
            "CREATE TABLE {$this->table('ultraguard_ip_reputation_cache')} (
  id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  ip_address varchar(45) NOT NULL,
  abuse_score tinyint(3) UNSIGNED NOT NULL DEFAULT 0,
  is_threat tinyint(1) NOT NULL DEFAULT 0,
  threat_types varchar(255) DEFAULT NULL,
  country_code varchar(2) DEFAULT NULL,
  isp varchar(200) DEFAULT NULL,
  is_tor tinyint(1) NOT NULL DEFAULT 0,
  is_vpn tinyint(1) NOT NULL DEFAULT 0,
  checked_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  expires_at datetime NOT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY idx_ip_address (ip_address),
  KEY idx_is_threat (is_threat),
  KEY idx_expires_at (expires_at)
) {$cs};",
        ];
    }
}
