<?php

	class Korkmaz_contract {

		/**
		 * Orchestrates the hooks of the plugin.
		 *
		 * @since 1.0.0
		 * @var   Korkmaz_woo_sales_contract_Loader
		 */
		protected $loader;

		/**
		 * Plugin slug used to uniquely identify the plugin.
		 *
		 * @since 1.0.0
		 * @var   string
		 */
		protected $plugin_name;

		/**
		 * Current plugin version.
		 *
		 * @since 1.0.0
		 * @var   string
		 */
		protected $version;

		/**
		 * Set the plugin name and version, load dependencies, set locale,
		 * and register hooks for both admin and public areas.
		 *
		 * @since 1.0.0
		 */
		public function __construct() {
			$this->version     = defined( 'KORKMAZ_CONTRACT_VERSION' ) ? KORKMAZ_CONTRACT_VERSION : '1.0.0';
			$this->plugin_name = 'wc-korkmaz-contract';

			$this->load_dependencies();
			$this->set_locale();
			$this->define_admin_hooks();
			$this->define_public_hooks();
		}

		/**
		 * Load required dependencies for this plugin.
		 *
		 * Includes:
		 * - Loader (orchestrates hooks)
		 * - i18n (textdomain loader)
		 * - Admin/public classes
		 * - Draft screen + PDF classes
		 *
		 * @since  1.0.0
		 * @access private
		 */
		private function load_dependencies() {
			$base = plugin_dir_path( dirname( __FILE__ ) );

			require_once $base . 'vendor/autoload.php';

			require_once $base . 'includes/class-korkmaz_contract-loader.php';
			require_once $base . 'includes/class-korkmaz_contract-i18n.php';

			require_once $base . 'admin/class-korkmaz_contract-admin.php';
			require_once $base . 'public/class-korkmaz_contract-public.php';

			require_once $base . 'includes/class-korkmaz_contract-taslak-ekrani.php';
			require_once $base . 'includes/class-korkmaz_contract_pdf.php';

			$this->loader = new Korkmaz_woo_sales_contract_Loader();
		}

		/**
		 * Set the locale for this plugin for internationalization.
		 *
		 * @since  1.0.0
		 * @access private
		 */
		private function set_locale() {
			$plugin_i18n = new Korkmaz_contract_i18n();
			$this->loader->add_action( 'init', $plugin_i18n, 'load_plugin_korkmaz_contract', 0 );
			$this->loader->add_action( 'init', $this, 'maybe_upgrade', 5 );
		}

		/**
		 * Run lightweight, backward-compatible upgrades after plugin updates.
		 *
		 * @since 2.4.2
		 */
		public function maybe_upgrade(): void {
			$target_version = '2.4.2';
			$db_version     = (string) get_option( 'korkmaz_contract_db_version', '' );

			if ( version_compare( $db_version, $target_version, '>=' ) ) {
				return;
			}

			$mail_status = str_replace( '_', '-', sanitize_key( (string) get_option( 'sozlesme_mail_durumu', '' ) ) );
			if ( 'new-order' === $mail_status ) {
				$mail_status = 'pending';
			}

			if ( in_array( $mail_status, array( 'pending', 'processing', 'on-hold', 'completed' ), true ) ) {
				update_option( 'sozlesme_mail_durumu', $mail_status, false );
			}

			require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-korkmaz_contract-activator.php';
			SCWC_Activator::activate();

			update_option( 'korkmaz_contract_db_version', $target_version, false );
		}

		/**
		 * Register all hooks related to the admin area.
		 *
		 * @since  1.0.0
		 * @access private
		 */
		private function define_admin_hooks() {
			$plugin_admin = new Korkmaz_woo_sales_contract_Admin( $this->get_plugin_name(), $this->get_version() );

			$this->loader->add_action( 'admin_init',                $plugin_admin, 'alanlar_settings_init' );
			$this->loader->add_action( 'admin_enqueue_scripts',     $plugin_admin, 'enqueue_styles' );
			$this->loader->add_action( 'admin_enqueue_scripts',     $plugin_admin, 'enqueue_scripts' );
			$this->loader->add_action( 'admin_menu',                $plugin_admin, 'add_admin_menu' );
			$this->loader->add_action( 'admin_notices',             $plugin_admin, 'missing_checkout_fields_admin_notice' );
			$this->loader->add_action( 'woocommerce_admin_order_data_after_shipping_address', $plugin_admin, 'butonlari_goster_admin' );
			$this->loader->add_action( 'woocommerce_admin_order_data_after_billing_address',  $plugin_admin, 'vergi_no_dairesi' );

			$this->loader->add_filter( 'mce_external_plugins', $plugin_admin, 'js_buton_ekle' );
			$this->loader->add_filter( 'mce_buttons',          $plugin_admin, 'buton_ekle' );

			$this->loader->add_action( 'admin_init', $plugin_admin, 'korkmaz_contract_register_settings' );

			if ( (int) get_option( 'sozlesme_ozellik_1' ) === 1 ) {
				// Legacy (HPOS devre dışı) hooks
				$this->loader->add_action( 'manage_shop_order_posts_custom_column', $plugin_admin, 'custom_orders_list_column_content', 20, 2 );
				$this->loader->add_filter( 'manage_edit-shop_order_columns',        $plugin_admin, 'custom_shop_order_column', 20 );

				// HPOS (High-Performance Order Storage) uyumlu hooks
				$this->loader->add_action( 'woocommerce_shop_order_list_table_custom_column', $plugin_admin, 'custom_orders_list_column_content_hpos', 20, 2 );
				$this->loader->add_filter( 'woocommerce_shop_order_list_table_columns',       $plugin_admin, 'custom_shop_order_column', 20 );
			}
		}

		/**
		 * Get the plugin slug.
		 *
		 * @since  1.0.0
		 * @return string
		 */
		public function get_plugin_name(): string {
			return $this->plugin_name;
		}

		/**
		 * Get the plugin version.
		 *
		 * @since  1.0.0
		 * @return string
		 */
		public function get_version(): string {
			return $this->version;
		}

		/**
		 * Register all hooks related to the public-facing functionality.
		 *
		 * @since  1.0.0
		 * @access private
		 */
		private function define_public_hooks() {
			$plugin_public = new SCWC_Public( $this->get_plugin_name(), $this->get_version() );

			$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
			$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );
			$this->loader->add_action( 'init',               $plugin_public, 'maybe_auto_insert_checkout_block', 30 );

			$this->loader->add_action( 'wp_footer',                               $plugin_public, 'sozlesme_goruntule' );
			$this->loader->add_action( 'woocommerce_review_order_before_submit',  $plugin_public, 'sozlesme_goster_odeme_sayfasi' );
			$this->loader->add_action( 'woocommerce_checkout_process',            $plugin_public, 'onayla_uyari' );

			// AJAX endpoints.
			$this->loader->add_action( 'wp_ajax_bu_fonksiyon',        $plugin_public, 'soz_fn_cagir' );
			$this->loader->add_action( 'wp_ajax_nopriv_bu_fonksiyon', $plugin_public, 'soz_fn_cagir' );
			$this->loader->add_action( 'wp_ajax_metin_getir',         $plugin_public, 'metin_getir' );
			$this->loader->add_action( 'wp_ajax_nopriv_metin_getir',  $plugin_public, 'metin_getir' );

			$this->loader->add_action( 'wp_ajax_modal_metin_duzenle',        $plugin_public, 'modalicergiduzenleme' );
			$this->loader->add_action( 'wp_ajax_nopriv_modal_metin_duzenle', $plugin_public, 'modalicergiduzenleme' );
			$this->loader->add_action( 'wp_ajax_korkmaz_cart_summary',        $plugin_public, 'cart_summary' );
			$this->loader->add_action( 'wp_ajax_nopriv_korkmaz_cart_summary', $plugin_public, 'cart_summary' );
			$this->loader->add_action( 'wp_ajax_korkmaz_contract_record_missing_checkout_fields', $plugin_public, 'record_missing_checkout_fields' );
			$this->loader->add_action( 'admin_post_korkmaz_contract_download',        $plugin_public, 'download_contract_pdf' );
			$this->loader->add_action( 'admin_post_nopriv_korkmaz_contract_download', $plugin_public, 'download_contract_pdf' );

			$this->loader->add_action( 'wp_head',                         $plugin_public, 'myplugin_ajaxurl' );
			$this->loader->add_filter( 'woocommerce_checkout_fields',     $plugin_public, 'odeme_ekrani_custom_input_ekle' );
			$this->loader->add_action( 'woocommerce_thankyou',            $plugin_public, 'front_sozlesme_goster' );
			$this->loader->add_action( 'woocommerce_checkout_process',    $plugin_public, 'tc_numara_dogrula' );
			$this->loader->add_filter( 'woocommerce_email_attachments',   $plugin_public, 'siparis_mail_ekle', 10, 4 );

			// Show contracts in "View order" screen.
			$this->loader->add_action( 'woocommerce_view_order', $plugin_public, 'view_order_sozlesme_goster', 20 );

			// Additional checkout fields + meta handling.
			$this->loader->add_action( 'woocommerce_checkout_before_order_review', $plugin_public, 'classic_checkout_hidden_contract_fields', 5 );
			$this->loader->add_action( 'woocommerce_checkout_create_order', $plugin_public, 'custom_order_meta_register', 10, 2 );

			// Blocks integration.
			$this->loader->add_action( 'woocommerce_init', $plugin_public, 'register_block_checkout_fields' );
			$this->loader->add_action( 'woocommerce_blocks_loaded', $plugin_public, 'sozlesme_block' );
			$this->loader->add_action( 'block_categories_all',      $plugin_public, 'register_Irem_block_category' );

			// Create PDFs after checkout/order processed.
			$this->loader->add_action( 'woocommerce_checkout_order_processed', $plugin_public, 'sozlesme_olustur_pdf', 10, 1 );
			$this->loader->add_action( 'woocommerce_store_api_checkout_update_order_from_request', $plugin_public, 'update_order_meta_from_request', 10, 2 );
			$this->loader->add_action( 'woocommerce_store_api_checkout_order_processed',            $plugin_public, 'sozlesme_olustur_pdf' );
		}

		/**
		 * Run the loader to execute all hooks with WordPress.
		 *
		 * @since 1.0.0
		 */
		public function run(): void {
			$this->loader->run();
		}

		/**
		 * Get the loader instance.
		 *
		 * @since  1.0.0
		 * @return Korkmaz_woo_sales_contract_Loader
		 */
		public function get_loader(): Korkmaz_woo_sales_contract_Loader {
			return $this->loader;
		}
	}
