<?php
// @future: When features grow, convert sections to vtabs:
//   [vtab] General      - enable, expiry, button label
//   [vtab] Appearance   - button/popup styling
//   [vtab] Social       - Twitter, Facebook, WhatsApp share
//   [vtab] Custom Message (pro) - pre-filled message to recipient
//   [vtab] Password (pro)       - password-protect share link
//   [vtab] QR Code (pro)        - QR code in popup

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

$settings = \Lex\Settings\V2\Settings::getInstance( 'lean_csns' );
$sr       = $settings->sectionRenderer;

$defaults      = [];
$defaults_file = dirname( __DIR__ ) . '/default-settings.php';
if ( file_exists( $defaults_file ) ) {
	$loaded = include $defaults_file;
	if ( is_array( $loaded ) ) {
		$defaults = $loaded;
	}
}

// ============================================
// Permalink info content (shared between inline info and the Important section)
// ============================================
$permalink_structure = get_option( 'permalink_structure' );
$is_plain            = empty( $permalink_structure );
$permalinks_url      = admin_url( 'options-permalink.php' );

$info_content = '<strong>' . esc_html__( 'Fix 404 Errors on Share Links', 'lean-cart-share-and-save' ) . '</strong><br>';
if ( $is_plain ) {
	$info_content .= esc_html__( 'Share links require pretty permalinks. ', 'lean-cart-share-and-save' );
}
$info_content .= sprintf(
	/* translators: %s is a link to WordPress Settings -> Permalinks page */
	esc_html__( 'If share links or My Account -> Saved Carts show 404 errors: Go to %s and click "Save Changes" (no need to change anything). This refreshes WordPress URL rules and fixes 404 issues.', 'lean-cart-share-and-save' ),
	'<a href="' . esc_url( $permalinks_url ) . '">' . esc_html__( 'Settings -> Permalinks', 'lean-cart-share-and-save' ) . '</a>'
);
$info_content .= '<br><em>' . esc_html__( 'Why this happens: WordPress needs to refresh URL routing when new link structures are added.', 'lean-cart-share-and-save' ) . '</em>';

// ============================================
// SECTION: Share Cart Settings
// ============================================
$sr->startSection( 'cart_share_settings', esc_html__( 'Share Cart Settings', 'lean-cart-share-and-save' ) );

$settings->fieldRenderer->render( 'checkbox', 'cart_share.enable', [
	'label'          => esc_html__( 'Enable Share Cart', 'lean-cart-share-and-save' ),
	'checkbox_label' => esc_html__( 'Yes', 'lean-cart-share-and-save' ),
	'desc'           => esc_html__( 'Allow users to share their cart via a link', 'lean-cart-share-and-save' ),
	'default'        => $defaults['cart_share']['enable'] ?? '1',
] );

$settings->fieldRenderer->render( 'info', 'cart_share.permalink_notice_inline', [
	'content' => $info_content,
] );

$settings->fieldRenderer->render( 'number', 'cart_share.expiry_days', [
	'label'   => esc_html__( 'Share Expiry (Days)', 'lean-cart-share-and-save' ),
	'desc'    => esc_html__( 'Set to 0 for no expiration, or the number of days before a shared cart link expires', 'lean-cart-share-and-save' ),
	'min'     => 0,
	'step'    => 1,
	'default' => $defaults['cart_share']['expiry_days'] ?? '0',
] );

$settings->fieldRenderer->render( 'text', 'cart_share.share_button_label', [
	'label'       => esc_html__( 'Share Button Label', 'lean-cart-share-and-save' ),
	'desc'        => esc_html__( 'Customize the text shown on the Share Cart button. Leave empty to use the default.', 'lean-cart-share-and-save' ),
	'placeholder' => esc_html__( 'Share Cart', 'lean-cart-share-and-save' ),
	'default'     => $defaults['cart_share']['share_button_label'] ?? '',
] );

$settings->fieldRenderer->render( 'select', 'cart_share.button_position', [
	'label'   => esc_html__( 'Button Position', 'lean-cart-share-and-save' ),
	'desc'    => esc_html__( 'Where the Share Cart and Save Cart buttons appear on the cart page.', 'lean-cart-share-and-save' ),
	'options' => [
		'default'       => esc_html__( 'Default', 'lean-cart-share-and-save' ),
		'after_table'   => esc_html__( 'After cart table', 'lean-cart-share-and-save' ),
		'before_totals' => esc_html__( 'Before cart totals', 'lean-cart-share-and-save' ),
	],
	'default' => $defaults['cart_share']['button_position'] ?? 'default',
	'classes' => [ 'lcss-no-border-bottom' ],
] );

// ============================================
// PRO GROUP: Advanced Features
// Grouped inside the same section. Unlocked by Pro_Settings_Unlocker when pro is active.
// ============================================
$sr->startProGroup( [
	'id'    => 'share-cart-advanced',
	'title' => esc_html__( 'Advanced Settings', 'lean-cart-share-and-save' ),
] );

$settings->fieldRenderer->render( 'checkbox', 'cart_share.enable_custom_message', [
	'label'          => esc_html__( 'Enable Custom Message', 'lean-cart-share-and-save' ),
	'checkbox_label' => esc_html__( 'Yes', 'lean-cart-share-and-save' ),
	'desc'           => esc_html__( 'Allow cart sharers to include a personal message shown to the recipient when the cart is loaded.', 'lean-cart-share-and-save' ),
	'default'        => $defaults['cart_share']['enable_custom_message'] ?? '1',
	'classes'        => [ 'lex-field--clickable' ],
	'disabled'       => true,
	'pro'            => [ 'badge_position' => false, 'onclick' => 'openUpgradeModal' ],
] );

$settings->fieldRenderer->render( 'textarea', 'cart_share.message_default', [
	'label'       => esc_html__( 'Default Message', 'lean-cart-share-and-save' ),
	'desc'        => esc_html__( 'This text is pre-filled in the message box when the share popup opens. The sharer can edit or clear it before sending. Shows on top of the loaded cart.', 'lean-cart-share-and-save' ),
	'placeholder' => esc_html__( 'e.g. Hey! I thought you might love these picks.', 'lean-cart-share-and-save' ),
	'default'     => $defaults['cart_share']['message_default'] ?? '',
	'classes'     => [ 'lex-field--clickable' ],
	'disabled'    => true,
	'pro'         => [ 'badge_position' => false, 'onclick' => 'openUpgradeModal' ],
] );

$settings->fieldRenderer->render( 'checkbox', 'cart_share.enable_qr_code', [
	'label'          => esc_html__( 'Enable QR Code', 'lean-cart-share-and-save' ),
	'checkbox_label' => esc_html__( 'Yes', 'lean-cart-share-and-save' ),
	'desc'           => esc_html__( 'Show a QR code in the share popup so recipients can scan with their phone.', 'lean-cart-share-and-save' ),
	'default'        => $defaults['cart_share']['enable_qr_code'] ?? '1',
	'classes'        => [ 'lex-field--clickable' ],
	'disabled'       => true,
	'pro'            => [ 'badge_position' => false, 'onclick' => 'openUpgradeModal' ],
] );

// CS4: Password protection for shared cart URLs.
$settings->fieldRenderer->render( 'checkbox', 'cart_share.enable_password_protection', [
	'label'          => esc_html__( 'Enable Password Protection', 'lean-cart-share-and-save' ),
	'checkbox_label' => esc_html__( 'Yes', 'lean-cart-share-and-save' ),
	'desc'           => esc_html__( 'Allow cart sharers to set a password that recipients must enter before the cart loads.', 'lean-cart-share-and-save' ),
	'default'        => $defaults['cart_share']['enable_password_protection'] ?? '0',
	'classes'        => [ 'lex-field--clickable' ],
	'disabled'       => true,
	'pro'            => [ 'badge_position' => false, 'onclick' => 'openUpgradeModal' ],
] );

$sr->endProGroup();

$sr->endSection();

// Render Save & Reset buttons
$sr->renderSubmitButtons();
