<?php


namespace Gigneo;

use Carbon_Fields\Container\Container;
use Carbon_Fields\Field\Field;
use WP_Term_Query;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Class Post_Meta
 *
 * Plugin post-meta class for Gigneo.
 *
 * @since 0.0.1
 */
class Post_Meta {


	/**
	 * The single instance of the class.
	 *
	 * @var Post_Meta|null
	 * @since 0.0.1
	 */
	private static $instance = null;

	/**
	 * Constructor for the class.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		//Register fields
		add_action( 'carbon_fields_register_fields', array( $this, 'register_post_meta' ) );

		//Save service post meta
		add_action( 'save_post_gigneo_service', array( $this, 'save_service_post_meta' ) );
	}

	/**
	 * Ensures only one instance of the class is loaded or can be loaded.
	 *
	 * @return Post_Meta
	 * @since 0.0.1
	 */
	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Register post meta fields.
	 *
	 * @since 0.0.1
	 */
	public function register_post_meta() {

		//Freelancer/Employer post meta
		$this->register_profile_post_meta();

		//Service post meta
		$this->register_service_post_meta();

		//Job post meta
		$this->register_job_post_meta();

		//Proposal post meta
		$this->register_proposal_post_meta();
	}

	/**
	 * Register profile (freelancer,employer) post meta fields.
	 *
	 * @since 0.0.1
	 */
	public function register_profile_post_meta() {

		//Basic Information
		$gigneo_basic_info_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/profile/basic-info.php';
		$gigneo_basic_info_fields = apply_filters( 'gigneo_pm_profile_basic_info_tab_fields', $gigneo_basic_info_fields );

		//Bio
		$gigneo_bio_field = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/profile/bio.php';
		$gigneo_bio_field = apply_filters( 'gigneo_pm_profile_bio_tab_fields', $gigneo_bio_field );

		//Intro & Cover
		$gigneo_media_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/profile/media.php';
		$gigneo_media_fields = apply_filters( 'gigneo_pm_profile_media_tab_fields', $gigneo_media_fields );

		//Language
		$gigneo_language_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/profile/language.php';
		$gigneo_language_fields = apply_filters( 'gigneo_pm_profile_language_fields', $gigneo_language_fields );

		//Address & Location
		$gigneo_location_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/profile/location.php';


		//Education & Experience
		$gigneo_qualification_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/profile/qualification.php';
		$gigneo_qualification_fields = apply_filters( 'gigneo_pm_profile_qualification_fields', $gigneo_qualification_fields );

		//Certificate & Award
		$gigneo_achievement_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/profile/achievement.php';
		$gigneo_achievement_fields = apply_filters( 'gigneo_pm_profile_achievement_fields', $gigneo_achievement_fields );

		//Portfolio & FAQ
		$gigneo_showcase_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/profile/showcase.php';
		$gigneo_showcase_fields = apply_filters( 'gigneo_pm_profile_showcase_fields', $gigneo_showcase_fields );

		//License & Payout
		$gigneo_accreditation_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/profile/accreditation.php';
		$gigneo_accreditation_fields = apply_filters( 'gigneo_pm_profile_accreditation_fields', $gigneo_accreditation_fields );

		//Social Profiles
		$gigneo_social_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/profile/social.php';
		$gigneo_social_fields = apply_filters( 'gigneo_pm_profile_social_fields', $gigneo_social_fields );

		//Commission Fee
		$gigneo_fee_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/profile/fee.php';
		$gigneo_fee_fields = apply_filters( 'gigneo_pm_profile_fee_fields', $gigneo_fee_fields );

		// Register post meta
		apply_filters( 'gigneo_profile_post_meta_container', Container::make( 'post_meta', 'gigneo-profile-options', esc_html__( 'Gigneo Option', 'gigneo' ) ) )
			->where( 'post_type', 'IN', array( 'gigneo_freelancer', 'gigneo_employer' ) )
			->set_context( 'normal' )
			->set_layout( 'tabbed-vertical' )
			->set_classes( 'gigneo-post-meta-container' )
			->add_tab( esc_html__( 'Information', 'gigneo' ), $gigneo_basic_info_fields )
			->add_tab( esc_html__( 'Bio description', 'gigneo' ), $gigneo_bio_field )
			->add_tab( esc_html__( 'Media', 'gigneo' ), $gigneo_media_fields )
			->add_tab( esc_html__( 'Languages', 'gigneo' ), $gigneo_language_fields )
			->add_tab( esc_html__( 'Address and location', 'gigneo' ), $gigneo_location_fields )
			->add_tab( esc_html__( 'Education and experience', 'gigneo' ), $gigneo_qualification_fields )
			->add_tab( esc_html__( 'Certificate and award', 'gigneo' ), $gigneo_achievement_fields )
			->add_tab( esc_html__( 'Portfolio and FAQ', 'gigneo' ), $gigneo_showcase_fields )
			->add_tab( esc_html__( 'License and payout', 'gigneo' ), $gigneo_accreditation_fields )
			->add_tab( esc_html__( 'Socials', 'gigneo' ), $gigneo_social_fields )
			->add_tab( esc_html__( 'Commission fee', 'gigneo' ), $gigneo_fee_fields );
	}

	/**
	 * Register service post meta fields.
	 *
	 * @since 0.0.1
	 */
	public function register_service_post_meta() {

		$platform_type = gigneo_platform_type();

		if ( 'jobs' === $platform_type ) {
			return;
		}

		//General
		$gigneo_general_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/service/general.php';
		$gigneo_general_fields = apply_filters( 'gigneo_pm_service_general_tab_fields', $gigneo_general_fields );

		//Media
		$gigneo_media_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/service/media.php';
		$gigneo_media_fields = apply_filters( 'gigneo_pm_service_media_tab_fields', $gigneo_media_fields );

		//Plan 1
		$gigneo_plan_one_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/service/plan-one.php';

		//Plan 2
		$gigneo_plan_two_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/service/plan-two.php';

		//Plan 3
		$gigneo_plan_three_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/service/plan-three.php';

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only parameters no nonce required.
		$current_post_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : 0;

		// Category plan feature options
		if ( ! empty( $current_post_id ) ) {
			$facet_query = new WP_Term_Query(
				array(
					'taxonomy'   => 'gigneo_facet',
					'object_ids' => $current_post_id,
					'hide_empty' => false,
				)
			);

			if ( ! empty( $facet_query->terms ) ) {

				foreach ( $facet_query->terms as $f => $facet ) {
					$features = gigneo_term_meta( $facet->term_id, '_gigneo_tm_service_features' );
					if ( ! empty( $features ) ) {
						foreach ( $features as $key => $feature ) {
							if ( empty( $feature['title'] ) ) {
								continue;
							}
							$title = $feature['title'];
							foreach ( array( 'one', 'two', 'three' ) as $index ) {
								$field = Field::make( 'checkbox', "gigneo_pm_price_plan_{$index}_feature_{$facet->slug}_$key", $title )
												->set_classes( 'gn-plan-feature' );

								${"gigneo_plan_{$index}_fields"}[] = $field;
							}
						}
					}
				}
			}
		}

		// Plan 1, 2 & 3 fields
		$gigneo_plan_one_fields   = apply_filters( 'gigneo_pm_service_plan_one_tab_fields', $gigneo_plan_one_fields );
		$gigneo_plan_two_fields   = apply_filters( 'gigneo_pm_service_plan_two_tab_fields', $gigneo_plan_two_fields );
		$gigneo_plan_three_fields = apply_filters( 'gigneo_pm_service_plan_three_tab_fields', $gigneo_plan_three_fields );

		//Features
		$gigneo_feature_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/service/feature.php';

		//Additional
		$gigneo_additional_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/service/additional.php';
		$gigneo_additional_fields = apply_filters( 'gigneo_pm_service_additional_tab_fields', $gigneo_additional_fields );

		//Requirements
		$gigneo_requirement_fields = array();
		$gigneo_requirement_fields = apply_filters( 'gigneo_pm_service_requirement_tab_fields', $gigneo_requirement_fields );

		// Register post meta
		$gigneo_service_container = apply_filters( 'gigneo_service_post_meta_container', Container::make( 'post_meta', 'gigneo-service-options', esc_html__( 'Gigneo Option', 'gigneo' ) ) )->where( 'post_type', '=', 'gigneo_service' )
			->set_context( 'normal' )
			->set_layout( 'tabbed-vertical' )
			->set_classes( 'gigneo-post-meta-container' )
			->add_tab( esc_html__( 'General', 'gigneo' ), $gigneo_general_fields )
			->add_tab( esc_html__( 'Media', 'gigneo' ), $gigneo_media_fields )
			->add_tab( esc_html__( 'Plan 1', 'gigneo' ), $gigneo_plan_one_fields )
			->add_tab( esc_html__( 'Plan 2', 'gigneo' ), $gigneo_plan_two_fields )
			->add_tab( esc_html__( 'Plan 3', 'gigneo' ), $gigneo_plan_three_fields );

		if ( ! empty( $gigneo_requirement_fields ) ) {
			$gigneo_service_container->add_tab( esc_html__( 'Requirements', 'gigneo' ), $gigneo_requirement_fields );
		}

		$gigneo_service_container->add_tab( esc_html__( 'Feature', 'gigneo' ), $gigneo_feature_fields )
			->add_tab( esc_html__( 'Additional', 'gigneo' ), $gigneo_additional_fields );
	}

	/**
	 * Register job post meta fields.
	 *
	 * @since 0.0.1
	 */
	public function register_job_post_meta() {

		$platform_type = gigneo_platform_type();
		if ( 'services' === $platform_type ) {
			return;
		}

		//Pricing
		$gigneo_pricing_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/job/pricing.php';
		$gigneo_pricing_fields = apply_filters( 'gigneo_pm_job_pricing_fields', $gigneo_pricing_fields );

		//Details
		$gigneo_detail_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/job/detail.php';
		$gigneo_detail_fields = apply_filters( 'gigneo_pm_job_detail_fields', $gigneo_detail_fields );

		//Media
		$gigneo_media_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/job/media.php';
		$gigneo_media_fields = apply_filters( 'gigneo_pm_job_media_tab_fields', $gigneo_media_fields );

		//Location
		$gigneo_location_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/job/location.php';

		$gigneo_screening_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/job/screening.php';
		$gigneo_screening_fields = apply_filters( 'gigneo_pm_job_screening_fields', $gigneo_screening_fields );

		// Register post meta
		$gigneo_job_container = apply_filters( 'gigneo_job_post_meta_container', Container::make( 'post_meta', 'gigneo-job-options', esc_html__( 'Gigneo Option', 'gigneo' ) ) )
			->where( 'post_type', '=', 'gigneo_job' )
			->set_context( 'normal' )
			->set_layout( 'tabbed-vertical' )
			->set_classes( 'gigneo-post-meta-container' )
			->add_tab( esc_html__( 'Pricing', 'gigneo' ), $gigneo_pricing_fields )
			->add_tab( esc_html__( 'Details', 'gigneo' ), $gigneo_detail_fields )
			->add_tab( esc_html__( 'Location', 'gigneo' ), $gigneo_location_fields )
			->add_tab( esc_html__( 'Media', 'gigneo' ), $gigneo_media_fields )
			->add_tab( esc_html__( 'Screening', 'gigneo' ), $gigneo_screening_fields );
	}

	/** Save service post meta fields.
	 *
	 * @param int $post_id The post ID.
	 *
	 * @since 0.0.1
	 */
	public function save_service_post_meta( $post_id ) {
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
			return;
		}
		if ( wp_is_post_revision( $post_id ) ) {
			return;
		}

		// phpcs:ignore WordPress.Security.NonceVerification.Missing -- This is called during save_post where WordPress already verifies the nonce.
		$carbon_compact_fields_data = isset( $_POST['carbon_fields_compact_input'] ) ? map_deep( wp_unslash( $_POST['carbon_fields_compact_input'] ), 'sanitize_text_field' ) : array();
		if ( ! empty( $carbon_compact_fields_data ) && is_array( $carbon_compact_fields_data ) ) {
			foreach ( $carbon_compact_fields_data as $field_key => $field_value ) {
				if ( str_starts_with( $field_key, '_gigneo_price_plan_one_feature_' ) || str_starts_with( $field_key, '_gigneo_price_plan_two_feature_' ) || str_starts_with( $field_key, '_gigneo_price_plan_three_feature_' ) ) {
					update_post_meta( $post_id, $field_key, $field_value );
				}
			}
		}
	}

	/**
	 * Register proposal meta fields.
	 *
	 * @since 0.0.1
	 */
	public function register_proposal_post_meta() {

		$platform_type = gigneo_platform_type();
		if ( 'services' === $platform_type ) {
			return;
		}

		// Terms
		$gigneo_proposal_fields = array();
		include_once GIGNEO_DIR_PATH . 'includes/post-meta/fields/proposal/proposal-fields.php';

		$gigneo_proposal_fields = apply_filters(
			'gigneo_pm_proposal_fields',
			$gigneo_proposal_fields
		);

		// Register post meta
		apply_filters(
			'gigneo_job_post_meta_container',
			Container::make(
				'post_meta',
				'gigneo-proposl-options',
				esc_html__( 'Gigneo Option', 'gigneo' )
			)
		)
			->where( 'post_type', '=', 'gigneo_proposal' )
			->set_context( 'normal' )
			->set_classes( 'gigneo-post-meta-container' )
			->add_fields( $gigneo_proposal_fields );
	}
}
