<?php

namespace Core\App\Models;

use PDO;

class SystemCleanup
{
    protected PDO $db;

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

    public function countRows(string $table): int
    {
        $stmt = $this->db->query("SELECT COUNT(*) FROM `$table`");
        return (int)$stmt->fetchColumn();
    }

    public function countOlderThan(string $table, string $dateColumn, int $days): int
    {
        $stmt = $this->db->prepare("SELECT COUNT(*) FROM `$table` WHERE `$dateColumn` < (NOW() - INTERVAL :days DAY)");
        $stmt->execute([':days' => $days]);
        return (int)$stmt->fetchColumn();
    }

    public function purgeNotificationsAll(): int
    {
        $this->db->exec("TRUNCATE TABLE `admin_notifications`");
        return 1;
    }

    public function purgeNotificationsOlderThan(int $days): int
    {
        return $this->purgeOlderThan('admin_notifications', 'created_at', $days);
    }

    public function purgePageViewsOlderThan(int $days): int
    {
        return $this->purgeOlderThan('page_views', 'visited_at', $days);
    }

    public function purgePageViewsAll(): int
    {
        $this->db->exec("TRUNCATE TABLE `page_views`");
        return 1;
    }

    public function purgeSecurityLogsOlderThan(int $days): int
    {
        return $this->purgeOlderThan('security_logs', 'created_at', $days);
    }

    public function purgeSecurityLogsAll(): int
    {
        $this->db->exec("TRUNCATE TABLE `security_logs`");
        return 1;
    }

    public function purgeSecurityLogsByTypeOlderThan(string $type, int $days): int
    {
        $allowed = ['404', 'warning', 'error', 'info', 'ban', 'ddos'];
        if (!in_array($type, $allowed, true)) {
            return 0;
        }

        $stmt = $this->db->prepare("DELETE FROM `security_logs` WHERE type = :type AND created_at < (NOW() - INTERVAL :days DAY)");
        $stmt->execute([':type' => $type, ':days' => $days]);
        return $stmt->rowCount();
    }

    public function purgeRateLimitsOlderThan(int $days): int
    {
        $stmt = $this->db->prepare("DELETE FROM `security_rate_limits` WHERE window_start < (UNIX_TIMESTAMP(NOW()) - (:days * 86400))");
        $stmt->execute([':days' => $days]);
        return $stmt->rowCount();
    }

    public function purgeRateLimitsAll(): int
    {
        $this->db->exec("TRUNCATE TABLE `security_rate_limits`");
        return 1;
    }

    public function purgeOlderThan(string $table, string $dateColumn, int $days): int
    {
        $stmt = $this->db->prepare("DELETE FROM `$table` WHERE `$dateColumn` < (NOW() - INTERVAL :days DAY)");
        $stmt->execute([':days' => $days]);
        return $stmt->rowCount();
    }

    public function purgeFraudAttemptsOlderThan(int $days): int
    {
        return $this->purgeOlderThan('fraud_attempts', 'created_at', $days);
    }

    public function purgeFraudAttemptsAll(): int
    {
        $this->db->exec("TRUNCATE TABLE `fraud_attempts`");
        return 1;
    }

    public function purgeAdminActivityOlderThan(int $days): int
    {
        return $this->purgeOlderThan('admin_activity_log', 'created_at', $days);
    }

    public function purgeAdminActivityAll(): int
    {
        $this->db->exec("TRUNCATE TABLE `admin_activity_log`");
        return 1;
    }

    public function purgeExpiredPasswordResets(): int
    {
        $stmt = $this->db->prepare("DELETE FROM `admin_password_resets` WHERE expires_at < NOW()");
        $stmt->execute();
        return $stmt->rowCount();
    }

    public function purgePasswordResetsOlderThan(int $days): int
    {
        return $this->purgeOlderThan('admin_password_resets', 'created_at', $days);
    }

    public function purgeUpdaterJobsOlderThan(int $days): int
    {
        $stmt = $this->db->prepare("DELETE FROM `safe_updater_jobs` WHERE status IN ('done','failed','cancelled') AND created_at < (NOW() - INTERVAL :days DAY)");
        $stmt->execute([':days' => $days]);
        return $stmt->rowCount();
    }

    public function purgeUpdaterJobsAllFinished(): int
    {
        $stmt = $this->db->prepare("DELETE FROM `safe_updater_jobs` WHERE status IN ('done','failed','cancelled')");
        $stmt->execute();
        return $stmt->rowCount();
    }

    public function purgeWhatsappMessagesOlderThan(int $days): int
    {
        return $this->purgeOlderThan('whatsapp_messages', 'created_at', $days);
    }

    public function purgeWhatsappMessagesAll(): int
    {
        $this->db->exec("TRUNCATE TABLE `whatsapp_messages`");
        return 1;
    }

    public function purgeAiLandingDraftsOlderThan(int $days): int
    {
        $stmt = $this->db->prepare("DELETE FROM `ai_landing_pages` WHERE status = 'draft' AND updated_at < (NOW() - INTERVAL :days DAY)");
        $stmt->execute([':days' => $days]);
        return $stmt->rowCount();
    }
}
