<?php

use Dolphy\Plugin\Ajax\Action;
use Dolphy\Plugin\Ajax\Request;
use Dolphy\Plugin\Ajax\Response;
use Dolphy\Plugin\Security\TwoFA;

defined('DOLPHY_PLUGIN') or die('Access Denied!');

Action::addAccount('get', 'security_2fa', function (Request $req, WP_User $user) {
  $data = TwoFA::generateNewInfo($user);
  $data['isEnabled'] = !!TwoFA::get($user->ID);

  Response::send($data);
});

Action::addAccount('post', 'security_2fa', function (Request $req, WP_User $user) {
  $code = sanitize_text_field($req->input('code'));
  $secret = sanitize_text_field($req->input('secret'));
  $backup = $req->input('backup');

  TwoFA::enable($user, $secret, $code, $backup);

  Response::send('Two-Factor Authentication Successfully Enabled!');
});

Action::addAccount('delete', 'security_2fa', function (Request $req, WP_User $user) {
  TwoFA::disable($user->ID);
  Response::send('Two-Factor Authentication Disabled!');
});
