<?php
/**
 * FilecheckerSchema — Registers three DB tables for the File Checker module.
 *
 *   1. ultraguard_file_integrity  — per-file hash state & status
 *   2. ultraguard_scan_sessions   — one row per completed scan run
 *   3. ultraguard_fc_quarantine   — quarantined file audit trail
 *
 * @package UltraguardPro\modules\filechecker\schema
 * @since   6.5.0
 */

namespace UltraguardPro\modules\filechecker\schema;

use UltraguardPro\modules\core\AbstractModuleSchema;

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

class FilecheckerSchema extends AbstractModuleSchema {

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

    // ── Table map ──────────────────────────────────────────────────────────────

    public function get_table_map(): array {
        return [
            'file_integrity' => \UltraguardPro\modules\filechecker\db\FileIntegrityTable::class,
            'scan_sessions'  => \UltraguardPro\modules\filechecker\db\ScanSessionsTable::class,
            'fc_quarantine'  => \UltraguardPro\modules\filechecker\db\FcQuarantineTable::class,
        ];
    }

    // ── CREATE TABLE definitions ───────────────────────────────────────────────

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

        return [

            // ── 1. File integrity state ────────────────────────────────────────
            $this->table( 'ultraguard_file_integrity' ) =>
            "CREATE TABLE {$this->table('ultraguard_file_integrity')} (
              id               bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
              file_path        text                NOT NULL,
              file_hash        varchar(64)         NOT NULL DEFAULT '',
              file_size        bigint(20) UNSIGNED NOT NULL DEFAULT 0,
              file_type        varchar(50)         NOT NULL DEFAULT 'unknown',
              status           varchar(20)         NOT NULL DEFAULT 'clean',
              change_type      varchar(80)                  DEFAULT NULL,
              previous_hash    varchar(64)                  DEFAULT NULL,
              quarantine_path  text                         DEFAULT NULL,
              is_core_file     tinyint(1)          NOT NULL DEFAULT 0,
              is_approved      tinyint(1)          NOT NULL DEFAULT 0,
              approved_by      bigint(20) UNSIGNED          DEFAULT NULL,
              last_checked_at  datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP,
              created_at       datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP,
              updated_at       datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
              PRIMARY KEY (id),
              KEY idx_file_hash       (file_hash),
              KEY idx_status          (status),
              KEY idx_is_core_file    (is_core_file),
              KEY idx_last_checked_at (last_checked_at),
              KEY idx_is_approved     (is_approved)
            ) {$cs};",

            // ── 2. Scan sessions (history) ─────────────────────────────────────
            $this->table( 'ultraguard_scan_sessions' ) =>
            "CREATE TABLE {$this->table('ultraguard_scan_sessions')} (
              id               bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
              scan_type        varchar(20)         NOT NULL DEFAULT 'full',
              status           varchar(20)         NOT NULL DEFAULT 'running',
              files_scanned    int(11) UNSIGNED    NOT NULL DEFAULT 0,
              changes_found    int(11) UNSIGNED    NOT NULL DEFAULT 0,
              added_files      int(11) UNSIGNED    NOT NULL DEFAULT 0,
              modified_files   int(11) UNSIGNED    NOT NULL DEFAULT 0,
              suspicious       int(11) UNSIGNED    NOT NULL DEFAULT 0,
              initiated_by     bigint(20) UNSIGNED NOT NULL DEFAULT 0,
              started_at       datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP,
              completed_at     datetime                     DEFAULT NULL,
              error_message    text                         DEFAULT NULL,
              created_at       datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP,
              updated_at       datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
              PRIMARY KEY (id),
              KEY idx_status     (status),
              KEY idx_scan_type  (scan_type),
              KEY idx_started_at (started_at)
            ) {$cs};",

            // ── 3. Quarantine audit trail ──────────────────────────────────────
            $this->table( 'ultraguard_fc_quarantine' ) =>
            "CREATE TABLE {$this->table('ultraguard_fc_quarantine')} (
              id               bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
              original_path    text                NOT NULL,
              quarantine_path  text                NOT NULL,
              reason           varchar(120)        NOT NULL DEFAULT 'manual',
              file_size        bigint(20) UNSIGNED NOT NULL DEFAULT 0,
              file_hash        varchar(64)         NOT NULL DEFAULT '',
              status           varchar(20)         NOT NULL DEFAULT 'quarantined',
              quarantined_by   bigint(20) UNSIGNED NOT NULL DEFAULT 0,
              quarantined_at   datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP,
              restored_at      datetime                     DEFAULT NULL,
              deleted_at       datetime                     DEFAULT NULL,
              created_at       datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP,
              updated_at       datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
              PRIMARY KEY (id),
              KEY idx_status         (status),
              KEY idx_quarantined_at (quarantined_at)
            ) {$cs};",
        ];
    }
}
