<?php
if( !defined('CMS_VERSION') ) exit;
$this->SetCurrentTab('smssettings');

if (!$this->CheckPermission(TwoFactor::MANAGE_SMS_PERM)) return;

if (isset($params['submit_credits'])) {
    $product_key = trim($params['product_key']);
    
    if (empty($product_key)) {
        $this->SetError($this->Lang('error_empty_license'));
    } else {
        $config = cms_utils::get_config();
        $domain = $_SERVER['HTTP_HOST'];
        
        $result = TwoFactorAPI::validate_license($product_key, $domain);
        
        if ($result && isset($result['valid']) && $result['valid'] === true) {
            if (!isset($result['license_type']) || $result['license_type'] !== 'credits') {
                $this->SetError($this->Lang('error_subscription_key_not_supported'));
            } else {
                $this->SetPreference('twofactor_sms_product_key', $product_key);
                $this->SetPreference('twofactor_smscredit_enabled', true);
                $this->SetPreference('twofactor_sms_available', true);
                $this->SetMessage($this->Lang('sms_credits_saved'));
            }
        } else {
            $error = isset($result['error']) ? $result['error'] : $this->Lang('error_invalid_license');
            $this->SetError($error);
        }
    }
    
    $this->RedirectToAdminTab();
    return;
}

if (isset($params['remove_credits'])) {
    $this->SetPreference('twofactor_sms_product_key', '');
    $this->SetPreference('twofactor_smscredit_enabled', '0');
    $twilio_configured = $this->GetPreference('twofactor_twilio_enabled', false);
    if (!$twilio_configured) {
        $this->SetPreference('twofactor_sms_available', false);
    }
    
    $this->SetMessage($this->Lang('sms_credits_removed'));
    $this->RedirectToAdminTab();
    return;
}

if (isset($params['remove_twilio'])) {
    $this->SetPreference('twofactor_twilio_api_key', '');
    $this->SetPreference('twofactor_twilio_api_secret', '');
    $this->SetPreference('twofactor_twilio_service_sid', '');
    $this->SetPreference('twofactor_twilio_enabled', '0');
    $smscredit_enabled = $this->GetPreference('twofactor_smscredit_enabled', false);
    if (!$smscredit_enabled) {
        $this->SetPreference('twofactor_sms_available', false);
    }
    
    $this->SetMessage($this->Lang('twilio_removed'));
    $this->RedirectToAdminTab();
    return;
}

if (isset($params['submit_twilio'])) {
    $api_key = trim($params['api_key']);
    $api_secret = trim($params['api_secret']);
    $service_sid = trim($params['service_sid']);
    
    if (empty($api_key) || empty($api_secret) || empty($service_sid)) {
        $this->SetError($this->Lang('error_twilio_fields_required'));
    } else {
        if (TwoFactorProviderSMS::verify_twilio_credentials($api_key, $api_secret, $service_sid)) {
            $this->SetPreference('twofactor_twilio_api_key', $api_key);
            $this->SetPreference('twofactor_twilio_api_secret', $api_secret);
            $this->SetPreference('twofactor_twilio_service_sid', $service_sid);
            $this->SetPreference('twofactor_twilio_enabled', true);
            $this->SetPreference('twofactor_sms_available', true);
            $this->SetMessage($this->Lang('twilio_settings_saved'));
        } else {
            $this->SetError($this->Lang('error_twilio_invalid_credentials'));
        }
    }
    
    $this->RedirectToAdminTab();
    return;
}

$product_key = $this->GetPreference('twofactor_sms_product_key', '');
$sms_available = $this->GetPreference('twofactor_sms_available', false);
$twilio_enabled = $this->GetPreference('twofactor_twilio_enabled', false);
$smscredit_enabled = $this->GetPreference('twofactor_smscredit_enabled', 0);
$credits_remaining = 0;
$license_plan = '';

if (!empty($product_key)) {
    $domain = $_SERVER['HTTP_HOST'];
    $result = TwoFactorAPI::validate_license($product_key, $domain);
    
    if ($result && isset($result['valid']) && $result['valid'] === true) {
        $credits_remaining = isset($result['credits_remaining']) ? $result['credits_remaining'] : 0;
        $license_plan = isset($result['plan']) ? $result['plan'] : '';
    }
}



$api_key = $this->GetPreference('twofactor_twilio_api_key', '');
$api_secret = $this->GetPreference('twofactor_twilio_api_secret', '');
$service_sid = $this->GetPreference('twofactor_twilio_service_sid', '');

$tpl = $smarty->CreateTemplate($this->GetTemplateResource('smssettings.tpl'), null, null, $smarty);
$tpl->assign('product_key', $product_key);
$tpl->assign('sms_available', $sms_available);
$tpl->assign('twilio_enabled', $twilio_enabled);
$tpl->assign('smscredit_enabled', $smscredit_enabled);
$tpl->assign('credits_remaining', $credits_remaining);
$tpl->assign('license_plan', $license_plan);
$tpl->assign('api_key', $api_key);
$tpl->assign('api_secret', $api_secret);
$tpl->assign('service_sid', $service_sid);
$tpl->assign('product_url', TwoFactor::PRODUCT_URL);
$tpl->display();
