<?php
/**
 * Password page utilities.
 *
 * Centralizes configuration and defaults for the password-protected
 * page used by LinkCentral. This keeps defaults, fallbacks, and options
 * in one place and is reused by both the settings UI and front-end
 * template.
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

if ( ! function_exists( 'linkcentral_get_password_page_settings' ) ) {
	/**
	 * Get all password page settings, including defaults, raw values, and effective values.
	 *
	 * Returns an associative array with:
	 * - show_logo (bool)
	 * - defaults (array: title, subtitle, password_label, button_label, error_message)
	 * - raw (array: title, subtitle, password_label, button_label, error_message)
	 * - text (array: effective title, subtitle, password_label, button_label, error_message)
	 * - colors (array: background, container, button)
	 * - text_mode (string: 'dark'|'light')
	 * - text_color (string: hex)
	 * - button_text_color (string: hex)
	 *
	 * @return array
	 */
	function linkcentral_get_password_page_settings() {
		// Translatable defaults match the original template strings.
		$defaults = array(
			'title'          => __( 'Password Protected', 'linkcentral' ),
			'subtitle'       => __( 'This link is password protected. To view it please enter your password below.', 'linkcentral' ),
			'password_label' => __( 'Password', 'linkcentral' ),
			'button_label'   => __( 'Submit', 'linkcentral' ),
			'error_message'  => __( 'The password you entered is incorrect. Please try again.', 'linkcentral' ),
		);

		// Raw values from options (may be empty string to mean "use default").
		$raw = array(
			'title'          => get_option( 'linkcentral_password_page_title', '' ),
			'subtitle'       => get_option( 'linkcentral_password_page_subtitle', '' ),
			'password_label' => get_option( 'linkcentral_password_page_password_label', '' ),
			'button_label'   => get_option( 'linkcentral_password_page_button_label', '' ),
			'error_message'  => get_option( 'linkcentral_password_page_error_message', '' ),
		);

		// Effective text values with fallback to translatable defaults.
		$text = array(
			'title'          => $raw['title'] !== '' ? $raw['title'] : $defaults['title'],
			'subtitle'       => $raw['subtitle'] !== '' ? $raw['subtitle'] : $defaults['subtitle'],
			'password_label' => $raw['password_label'] !== '' ? $raw['password_label'] : $defaults['password_label'],
			'button_label'   => $raw['button_label'] !== '' ? $raw['button_label'] : $defaults['button_label'],
			'error_message'  => $raw['error_message'] !== '' ? $raw['error_message'] : $defaults['error_message'],
		);

		// Colors: empty string means "no override, use theme/defaults".
		$colors = array(
			'background' => get_option( 'linkcentral_password_page_background_color', '' ),
			'container'  => get_option( 'linkcentral_password_page_container_background_color', '' ),
			'button'     => get_option( 'linkcentral_password_page_button_color', '' ),
		);

		$text_mode        = get_option( 'linkcentral_password_page_text_color_mode', 'dark' );
		$text_is_light    = ( 'light' === $text_mode );
		$text_color       = $text_is_light ? '#FFFFFF' : '#000000';
		$button_text_color = $text_is_light ? '#000000' : '#FFFFFF';

		return array(
			'show_logo'         => (bool) get_option( 'linkcentral_password_page_show_logo', false ),
			'defaults'          => $defaults,
			'raw'               => $raw,
			'text'              => $text,
			'colors'            => $colors,
			'text_mode'         => $text_mode,
			'text_color'        => $text_color,
			'button_text_color' => $button_text_color,
		);
	}
}

