<?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();
			}
			// Using the TextDomain is a more reliable way to get the slug
			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 . '/'; // Return with a trailing slash
	}

	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 [ 'wxr', 'admin_options', 'customizer', 'widgets', 'elementor_kit', 'meta-options' ];
	}

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

	public static function get_admin_option_keys() {
		return [ 'sifency-options' ];
	}

	public static function get_csf_meta_keys() {
		return [ '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 [
			'wxr'                      => esc_html__( 'Main Content (XML)', 'sifency-addons' ),
			'wxr_import'               => esc_html__( 'Main Content (XML)', 'sifency-addons' ),
			'admin_options'              => esc_html__( 'Theme Options', 'sifency-addons' ),
			'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' ),
			'importer_plugin_inactive' => esc_html__( 'WordPress Importer Plugin is Inactive', 'sifency-addons' ),
			'xml_file_missing'         => esc_html__( 'Main Content (XML file missing)', 'sifency-addons' ),
			'kit_file_missing'         => esc_html__( 'Elementor Kit (File missing)', 'sifency-addons' ),
		];
	}
}