<?php

namespace UltraguardPro\modules\authenticator\schema;

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

use UltraguardPro\modules\core\AbstractModuleSchema;

class TwoFactorSchema extends AbstractModuleSchema {

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

    public function get_table_map(): array {
        return [
            'totp_credentials' => \UltraguardPro\modules\authenticator\db\TotpCredentialsTable::class,
        ];
    }

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

        return [
            $this->table( 'ultraguard_totp_credentials' ) =>
            "CREATE TABLE {$this->table('ultraguard_totp_credentials')} (
  id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id bigint(20) UNSIGNED NOT NULL,
  secret_key varchar(64) NOT NULL,
  backup_codes text DEFAULT NULL,
  is_enabled tinyint(1) NOT NULL DEFAULT 0,
  is_enforced tinyint(1) NOT NULL DEFAULT 0,
  last_used_at datetime DEFAULT NULL,
  last_used_code varchar(10) DEFAULT NULL,
  failed_attempts int NOT NULL DEFAULT 0,
  locked_until 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),
  UNIQUE KEY idx_user_id (user_id),
  KEY idx_is_enabled (is_enabled)
) {$cs};",
        ];
    }
}
