<?php

namespace WeDevs\AppBridge\Integrations\WooCommerce\REST;

use WeDevs\AppBridge\Core\Contracts\SettingsSchemaInterface;
use WeDevs\AppBridge\Core\REST\AbstractSettingsController;
use WeDevs\AppBridge\Integrations\WooCommerce\LicenseManager;
use WeDevs\AppBridge\Integrations\WooCommerce\SettingsSchema;
use WP_REST_Server;

class SettingsController extends AbstractSettingsController {

	protected $namespace = 'app-bridge/v1/pocket-store';

	protected $base = 'admin/settings';

	private LicenseManager $license_manager;

	public function __construct( LicenseManager $license_manager ) {
		$this->license_manager = $license_manager;
	}

	protected function schema(): SettingsSchemaInterface {
		return new SettingsSchema( $this->license_manager );
	}

	/**
	 * Register admin routes (inherited) plus public mobile-facing route.
	 *
	 * GET app-bridge/v1/pocket-store/settings — no auth, returns merged
	 * Pocket Store options for mobile clients.
	 */
	public function register_routes(): void {
		parent::register_routes();

		register_rest_route(
			$this->namespace,
			'/settings',
			[
				[
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => [ $this, 'get_public_settings' ],
					'permission_callback' => '__return_true',
				],
			]
		);
	}

	public function get_public_settings() {
		$schema     = new SettingsSchema( $this->license_manager );
		$settings   = get_option( $schema->get_option_key(), [] );
		$settings   = is_array( $settings ) ? $settings : [];
		$assets_url = defined( 'APP_BRIDGE_ASSETS' ) ? APP_BRIDGE_ASSETS : '';

		$app_lang_codes = $schema->normalize_language_codes( $settings['app_languages'] ?? null );
		if ( empty( $app_lang_codes ) ) {
			$app_lang_codes = [ 'en' ];
		}

		$options = [
			'app_logo'                    => isset( $settings['app_logo'] ) && $settings['app_logo'] ? $settings['app_logo'] : $assets_url . '/images/app-logo.default.png',
			'about_us_page_id'            => isset( $settings['about_us_page_id'] ) ? absint( $settings['about_us_page_id'] ) : 0,
			'contact_us_page_id'          => isset( $settings['contact_us_page_id'] ) ? absint( $settings['contact_us_page_id'] ) : 0,
			'privacy_policy_page_id'      => isset( $settings['privacy_policy_page_id'] ) ? absint( $settings['privacy_policy_page_id'] ) : 0,
			'primary_button_color_1'      => isset( $settings['primary_button_color_1'] ) ? $settings['primary_button_color_1'] : '#FF9472',
			'primary_button_color_2'      => isset( $settings['primary_button_color_2'] ) ? $settings['primary_button_color_2'] : '#F2709C',
			'text_color_on_primary' => isset( $settings['text_color_on_primary'] ) ? $settings['text_color_on_primary'] : '#FFFFFF',
			'display_sold_item_count'     => isset( $settings['display_sold_item_count'] ) ? (bool) $settings['display_sold_item_count'] : true,
			'app_languages'               => array_map(
				static fn ( $code ) => $schema->resolve_language( $code ),
				$app_lang_codes
			),
			'default_app_language'        => $schema->resolve_language( (string) ( $settings['default_app_language'] ?? 'en' ) ),
		];

		$options['about_us_page_url']       = $options['about_us_page_id'] ? get_permalink( $options['about_us_page_id'] ) : null;
		$options['contact_us_page_url']     = $options['contact_us_page_id'] ? get_permalink( $options['contact_us_page_id'] ) : null;
		$options['privacy_policy_page_url'] = $options['privacy_policy_page_id'] ? get_permalink( $options['privacy_policy_page_id'] ) : null;

		return rest_ensure_response( $options );
	}
}
