<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Provides centralized, static methods for common data and configurations.
 */
final class Sifency_Demo_Helper {

	private static $theme_slug = null;

	public static function get_theme_slug() {
		if ( is_null( self::$theme_slug ) ) {
			$theme = wp_get_theme();
			if ( $theme->parent() ) {
				$theme = $theme->parent();
			}
			self::$theme_slug = $theme->get( 'TextDomain' );
		}
		return self::$theme_slug;
	}

	/**
	 * Gets the path to the custom 'sifency-demo-data' directory inside wp-content/uploads.
	 * Creates the directory if it doesn't exist.
	 *
	 * @return string|false The path to the directory on success, false on failure.
	 */
	public static function get_demo_data_dir() {
		$upload_dir = wp_upload_dir();
		$demo_dir   = wp_normalize_path( $upload_dir['basedir'] . '/sifency-demo-data' );

		if ( ! file_exists( $demo_dir ) ) {
			if ( ! wp_mkdir_p( $demo_dir ) ) {
				return false;
			}
		}

		if ( ! file_exists( $demo_dir . '/index.html' ) ) {
			@file_put_contents( $demo_dir . '/index.html', '<!-- Silence is golden. -->' );
		}

		return $demo_dir . '/';
	}

	public static function get_filename( $type ) {
		$slug = self::get_theme_slug();
		switch ( $type ) {
			case 'wxr':
			case 'wxr_import':
				return "{$slug}-content.xml";
			case 'admin_options':
				return "{$slug}-options.json";
			case 'customizer':
				return 'customizer.dat';
			case 'widgets':
				return 'widgets.wie';
			case 'elementor_kit':
				return 'kit-settings.json';
			case 'meta-options':
				return "{$slug}-meta.json";
			default:
				return '';
		}
	}

	public static function get_filepath( $type ) {
		$filename = self::get_filename( $type );
		$dir      = self::get_demo_data_dir();

		if ( ! $dir || ! $filename ) {
			return '';
		}

		return $dir . $filename;
	}

	public static function get_export_types() {
		return array( 'wxr', 'admin_options', 'customizer', 'widgets', 'elementor_kit', 'meta-options' );
	}

	public static function get_import_types() {
		return array( 'wxr_import', 'admin_options', 'customizer', 'widgets', 'elementor_kit', 'meta-options' );
	}

	public static function get_admin_option_keys() {
		return array( 'sifency-options', 'sifency_addons_settings', 'sifency_cpt_modules_status' );
	}

	public static function get_csf_meta_keys() {
		return array( 'template-metabox', 'sifency_client_metabox', 'portfolio', 'services_options', 'award_options', 'team_options_metabox', 'testimonial_options', 'event_single_metabox', 'career_single_metabox', 'sifency_product_metabox', 'sifency_menu_options', 'sifency-metabox', 'gallery_metabox' );
	}

	public static function get_options_screen_id() {
		return 'toplevel_page_' . self::get_theme_slug() . '-options';
	}

	public static function get_friendly_names() {
		return array(
			'customizer'       => esc_html__( 'Customizer Settings', 'sifency-addons' ),
			'widgets'          => esc_html__( 'Widgets', 'sifency-addons' ),
			'elementor_kit'    => esc_html__( 'Elementor Kit Settings', 'sifency-addons' ),
			'meta-options'     => esc_html__( 'Post Meta', 'sifency-addons' ),
			'kit_file_missing' => esc_html__( 'Elementor Kit (File missing)', 'sifency-addons' ),
		);
	}
}
