<?php
/**
 * WordPress Settings Framework
 *
 * @link    https://github.com/gilbitron/WordPress-Settings-Framework
 * @author  Gilbert Pellegrom, James Kemp
 * @license MIT
 */

/**
 * Define your settings
 *
 * The first parameter of this filter should be wpsf_register_settings_[options_group],
 * in this case "my_example_settings".
 *
 * Your "options_group" is the second param you use when running new WordPressSettingsFramework()
 * from your init function. It's important as it differentiates your options from others.
 *
 * To use the tabbed example, simply change the second param in the filter below to 'wpsf_tabbed_settings'
 * and check out the tabbed settings function on line 156.
 */

use KaizenCoders\MagicLink\Helper;

add_filter( 'wpsf_register_settings_kc_ml', 'kc_ml_tabbed_settings' );
add_filter( 'kc_ml_settings_validate', 'kc_ml_settings_validate' );

/**
 * Settings
 *
 * @param $wpsf_settings
 *
 * @return mixed
 *
 * @since 1.0.0
 *
 */
function kc_ml_tabbed_settings( $wpsf_settings ) {
	// Tabs
	$tabs = [
		[
			'id'    => 'general',
			'title' => __( 'General', 'magic-link' ),
		],

	];

	$general_settings = [
		[
			'id'         => 'enable_magic_link',
			'title'      => __( 'Enable Magic Link', 'magic-link' ),
			'title_desc' => __( 'Allow user to login via Magic Link', 'magic-link' ),
			'type'       => 'switch',
			'default'    => 1,
			'order'      => 1,
		],

		[
			'id'         => 'force_magic_link_login',
			'title'      => __( 'Force Magic Link Login', 'magic-link' ),
			'title_desc' => __( 'Change default login behaviour and use magic link login as default login method.', 'magic-link' ),
			'desc'       => __( 'Use magic link login by default.', 'magic-link' ),
			'type'       => 'switch',
			'default'    => 0,
			'order'      => 2,
		],

		[
			'id'         => 'link_expiry',
			'title'      => __( 'Token Lifespan', 'magic-link' ),
			'title_desc' => __( 'The TTL (time to live) of the login link. WP-Cron removes expired tokens.', 'magic-link' ),
			'desc'       => __( 'How long magic links remain valid (1-1440 minutes)', 'magic-link' ),
			'type'       => 'custom',
			'output'     => kc_ml_render_token_lifespan_field(),
			'order'      => 3,
		],

		[
			'id'         => 'token_validity',
			'title'      => __( 'Token Validity', 'magic-link' ),
			'title_desc' => __( 'Specify how many times a token can be used.', 'magic-link' ),
			'type'       => 'number',
			'default'    => 1,
			'order'      => 4,
		],

		[
			'id'         => 'show_on_login_form',
			'title'      => __( 'Show on Login Form', 'magic-link' ),
			'title_desc' => __( 'Adds magic link button to WordPress login form.', 'magic-link' ),
			'default'    => 0,
			'type'       => 'switch',
			'order'      => 5,
		],

		[
			'id'         => 'login_redirect',
			'title'      => __( 'Login Redirect', 'magic-link' ),
			'title_desc' => __( 'Redirect users to custom URL after login.', 'magic-link' ),
			'desc'       => __( 'Enter a default redirect URL for all users', 'magic-link' ),
			'type'       => 'text',
			'default'    => home_url(),
			'order'      => 10,
		],
	];

	$wpsf_settings['tabs'] = apply_filters( 'kc_ml_filter_settings_tab', $tabs );

	$general_settings = apply_filters( 'kc_ml_filter_general_settings', $general_settings );

	$sections = [
		[
			'tab_id'        => 'general',
			'section_id'    => 'settings',
			'section_title' => __( 'Settings', 'url-shortify' ),
			'section_order' => 1,
			'fields'        => $general_settings,
		],
	];

	$sections = apply_filters( 'kc_ml_filter_settings_sections', $sections );

	$wpsf_settings['sections'] = $sections;

	return $wpsf_settings;
}


function kc_ml_render_token_lifespan_field() {
	$settings = KC_ML()->get_settings();

	$token_lifespan      = isset( $settings['general']['settings']['token_lifespan'] ) ? $settings['general']['settings']['token_lifespan'] : 5;
	$token_lifespan_unit = isset( $settings['general']['settings']['token_lifespan_unit'] ) ? $settings['general']['settings']['token_lifespan_unit'] :
		'minutes';

	$output = '<div style="display: flex; gap: 10px; align-items: center;">';
	$output .= '<input type="number" name="kc_ml_settings[general][settings][token_lifespan]" value="' . esc_attr( $token_lifespan ) . '" min="1" style="width: 100px;" />';
	$output .= '<select name="kc_ml_settings[general][settings][token_lifespan_unit]" style="width: 150px;">';
	$output .= '<option value="minutes" ' . selected( $token_lifespan_unit, 'minutes', false ) . '>Minutes</option>';
	$output .= '<option value="hours" ' . selected( $token_lifespan_unit, 'hours', false ) . '>Hours</option>';
	$output .= '<option value="days" ' . selected( $token_lifespan_unit, 'days', false ) . '>Days</option>';
	$output .= '</select>';
	$output .= '</div>';

	return $output;
}

/**
 * Validate/ Sanitize settings.
 *
 * @param $settings
 *
 * @return void
 *
 * @since 1.7.0
 *
 */
function kc_ml_settings_validate( $settings ) {
	return $settings;
}