<?php

namespace BwWinner\Options;

function get_options() {
	$defaults = array(
		'importAwardAs' => array(
			'doubleGold' => 96,
			'gold' => 94,
			'silver' => 92,
			'bronze' => 90
		),
		'displayScoreAs' => array(
			'doubleGold' => 96,
			'gold' => 94,
			'silver' => 92,
			'bronze' => 86
		),
		'medals' => array(
			'doubleGold' => 0,
			'gold' => 0,
			'silver' => 0,
			'bronze' => 0
		),
		// medalsByYear: map of 'YYYY' => array of tier => attachment_id.
		// Year-specific medals take precedence over the default `medals` set.
		'medalsByYear' => array(),
		// productCta: per-site CTA labels on the single product page.
		// `type` is one of '', brewery, winery, distillery, cidery, producer, custom.
		// `buy` / `learn` are only used when type === 'custom'.
		'productCta' => array(
			'type'  => '',
			'buy'   => '',
			'learn' => '',
		),
		'database_version' => '0.0.0'
	);
	$options = \get_option( 'bw_winners_options', null );
	if ( ! is_array( $options ) ) {
		return $defaults;
	}

	foreach ( $defaults as $key => $value ) {
		if ( ! isset( $options[ $key ] ) ) {
			$options[ $key ] = $value;
		}
	}

	// Defensive: ensure productCta is shaped correctly.
	if ( ! isset( $options['productCta'] ) || ! is_array( $options['productCta'] ) ) {
		$options['productCta'] = array( 'type' => '', 'buy' => '', 'learn' => '' );
	} else {
		$options['productCta'] = array(
			'type'  => isset( $options['productCta']['type'] )  ? (string) $options['productCta']['type']  : '',
			'buy'   => isset( $options['productCta']['buy'] )   ? (string) $options['productCta']['buy']   : '',
			'learn' => isset( $options['productCta']['learn'] ) ? (string) $options['productCta']['learn'] : '',
		);
	}

	// Defensive: drop any non-4-digit-year keys from medalsByYear so a
	// historical bug (sparse arrays from numeric-string `_.set` paths)
	// can't bloat the UI with thousands of empty rows.
	if ( isset( $options['medalsByYear'] ) && is_array( $options['medalsByYear'] ) ) {
		$cleaned = array();
		foreach ( $options['medalsByYear'] as $year => $tiers ) {
			$year = (string) $year;
			if ( ! preg_match( '/^\d{4}$/', $year ) ) continue;
			if ( ! is_array( $tiers ) ) continue;
			$cleaned[ $year ] = array(
				'doubleGold' => isset( $tiers['doubleGold'] ) ? (int) $tiers['doubleGold'] : 0,
				'gold'       => isset( $tiers['gold'] )       ? (int) $tiers['gold'] : 0,
				'silver'     => isset( $tiers['silver'] )     ? (int) $tiers['silver'] : 0,
				'bronze'     => isset( $tiers['bronze'] )     ? (int) $tiers['bronze'] : 0,
			);
		}
		$options['medalsByYear'] = $cleaned;
	} else {
		$options['medalsByYear'] = array();
	}

	return $options;
}

function update_options( $update ) {
	$options = get_options();

	$keys = array(
		'importAwardAs' => array(
			'doubleGold',
			'gold',
			'silver',
			'bronze'
		),
		'displayScoreAs' => array(
			'doubleGold',
			'gold',
			'silver',
			'bronze'
		),
		'medals' => array(
			'doubleGold',
			'gold',
			'silver',
			'bronze'
		),
		'database_version'
	);

	foreach ( $keys as $index => $key ) {

		if ( is_array( $key ) ) {
			foreach ( $key as $innerKey ) {
				 if ( isset( $update[ $index ][ $innerKey ] ) ) {
					$options[ $index ][ $innerKey ] = $update[ $index ][ $innerKey ];
				}
			}
		} else if ( isset( $update[ $key ] ) ) {
			$options[ $key ] = $update[ $key ];
		}
	}

	// productCta: enum-checked type, sanitized free-text overrides.
	if ( isset( $update['productCta'] ) && is_array( $update['productCta'] ) ) {
		$allowed_types = array( '', 'brewery', 'winery', 'distillery', 'cidery', 'producer', 'custom' );
		$cta           = $options['productCta'];
		if ( isset( $update['productCta']['type'] ) ) {
			$type = (string) $update['productCta']['type'];
			if ( in_array( $type, $allowed_types, true ) ) {
				$cta['type'] = $type;
			}
		}
		if ( isset( $update['productCta']['buy'] ) ) {
			$cta['buy'] = sanitize_text_field( (string) $update['productCta']['buy'] );
		}
		if ( isset( $update['productCta']['learn'] ) ) {
			$cta['learn'] = sanitize_text_field( (string) $update['productCta']['learn'] );
		}
		$options['productCta'] = $cta;
	}

	// medalsByYear: dynamic 4-digit year keys, fixed tier keys.
	if ( isset( $update['medalsByYear'] ) && is_array( $update['medalsByYear'] ) ) {
		$tier_keys = array( 'doubleGold', 'gold', 'silver', 'bronze' );
		$cleaned   = array();
		foreach ( $update['medalsByYear'] as $year => $tiers ) {
			$year = (string) $year;
			if ( ! preg_match( '/^\d{4}$/', $year ) ) continue;
			if ( ! is_array( $tiers ) ) continue;
			$year_set = array();
			foreach ( $tier_keys as $tier ) {
				$year_set[ $tier ] = isset( $tiers[ $tier ] ) ? (int) $tiers[ $tier ] : 0;
			}
			$cleaned[ $year ] = $year_set;
		}
		$options['medalsByYear'] = $cleaned;
	}

	\update_option( 'bw_winners_options', $options );
	return $options;
}
function get_option ( $option_name ) {
	$options = get_options();
	return isset( $options[$option_name] ) ? $options[$option_name] : false;
}
function update_option ( $option_name, $option_value ) {
	$update = array();
	$update[$option_name] = $option_value;
	return update_options( $update );
}
