<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

if ( class_exists( 'CSF' ) ) {

	// Set a unique slug-like ID for meta options
	$meta_prefix = 'jobus_meta_options';

	CSF::createMetabox( $meta_prefix, array(
		'title'        => esc_html__( 'Job Options', 'jobus' ),
		'post_type'    => 'jobus_job',
		'theme'        => 'dark',
		'output_css'   => true,
		'show_restore' => true,
	) );

	// Retrieve global job options
	$jobus_opt         = get_option( 'jobus_opt', [] );
	$enable_job_expiry = ! isset( $jobus_opt['enable_job_expiry'] ) || $jobus_opt['enable_job_expiry'];

	// Construct General Meta Fields
	$general_fields = array(
		// Single Post Layout
		array(
			'type'    => 'subheading',
			'content' => esc_html__( 'Single Post Layout', 'jobus' ),
		),

		array(
			'id'       => 'job_details_layout',
			'type'     => 'image_select',
			'title'    => esc_html__( 'Choose Layout', 'jobus' ),
			'subtitle' => esc_html__( 'Select the preferred layout for your job post for this page.', 'jobus' ),
			'options'  => array(
				'1' => esc_url( JOBUS_IMG . '/layout/job/single-layout-1.png' ),
				'2' => esc_url( JOBUS_IMG . '/layout/job/single-layout-2.png' ),
			),
			'default'  => '1',
			'class'    => trim( $pro_access_class . $active_theme_class ),
		),

		// Company Information
		array(
			'type'    => 'subheading',
			'content' => esc_html__( 'Company Info', 'jobus' ),
		),

		array(
			'id'      => 'select_company',
			'type'    => 'select',
			'title'   => esc_html__( 'Select Company', 'jobus' ),
			'options' => jobus_company_post_list(),
			'chosen'  => true,
			'default' => '',
			'class'   => trim( $pro_access_class . $active_theme_class ),
		),

		array(
			'id'      => 'is_company_website',
			'type'    => 'button_set',
			'title'   => esc_html__( 'Company Website', 'jobus' ),
			'options' => array(
				'default' => esc_html__( 'Default', 'jobus' ),
				'custom'  => esc_html__( 'Custom', 'jobus' ),
			),
			'default' => 'default',
			'class'   => trim( $pro_access_class . $active_theme_class ),
		),

		array(
			'id'         => 'company_website',
			'type'       => 'link',
			'title'      => esc_html__( 'Website Button', 'jobus' ),
			'default'    => array(
				'url' => '#',
			),
			'dependency' => array( 'is_company_website', '==', 'custom' ),
		), // End company information

		//====================== Job Information ======================//
		array(
			'type'    => 'subheading',
			'content' => esc_html__( 'Job Information', 'jobus' ),
		),
	);

	// Condition based show hide for boost performance
	if ( $enable_job_expiry ) {
		$general_fields[] = array(
			'id'       => '_jobus_expiration_date',
			'type'     => 'date',
			'title'    => esc_html__( 'Expiration Date', 'jobus' ),
			'subtitle' => esc_html__( 'Date when this job will automatically expire and be marked as Draft. If empty, the global default is used.', 'jobus' ),
			'settings' => array(
				'dateFormat' => 'Y-m-d H:i:s',
				'enableTime' => true,
				'time_24hr'  => true,
			),
		);
	}

	$general_fields[] = array(
		'id'      => 'is_apply_btn',
		'type'    => 'button_set',
		'title'   => esc_html__( 'Application Method', 'jobus' ),
		'desc'    => esc_html__( 'Select the primary method candidates will use to submit their application.', 'jobus' ),
		'options' => array(
			'default'  => esc_html__( 'Internal Form (Default)', 'jobus' ),
			'external' => esc_html__( 'External Apply Link', 'jobus' ),
		),
		'default' => 'default',
	);

	$general_fields[] = array(
		'id'         => 'apply_form_url',
		'type'       => 'text',
		'title'      => esc_html__( 'External Apply Link', 'jobus' ),
		'desc'       => esc_html__( 'Provide the complete URL (including https://) where candidates should be directed to complete their application. Ideal for third-party Applicant Tracking Systems (ATS) or corporate portals.', 'jobus' ),
		'default'    => '#',
		'dependency' => array( 'is_apply_btn', '==', 'external' ),
	);

	// Company Info Meta Options
	CSF::createSection( $meta_prefix, array(
		'title'  => esc_html__( 'General', 'jobus' ),
		'id'     => 'jobus_meta_general',
		'icon'   => 'fas fa-home',
		'fields' => $general_fields,
	) );


	// Retrieve the repeater field configurations from settings options
	$specifications = jobus_opt( 'job_specifications' );
	$fields = [];
	
	if ( is_array( $specifications ) && ! empty( $specifications ) ) {
		foreach ( $specifications as $field ) {

			$meta_value = $field['meta_values_group'] ?? [];
			$opt_values = [];
			$opt_val    = '';

			// Determine meta options based on the value of the switcher
			$is_meta_icon = isset( $field['is_meta_icon'] ) ? $field['is_meta_icon'] : '';
			$meta_options = '';

			if ( $is_meta_icon == 'meta_icon' && ! empty( $field['meta_icon'] ) ) {
				$meta_options = '<i class="' . esc_attr( $field['meta_icon'] ) . '"></i>';
			} elseif ( $is_meta_icon == 'meta_image' && ! empty( $field['meta_image'] ) ) {
				$meta_options = '<img src="' . esc_url( $field['meta_image']['url'] ) . '" alt="' . esc_attr__( 'icon', 'jobus' )
				                . '" class="lazy-img m-auto icon">';
			}

			foreach ( $meta_value as $value ) {
				$modifiedString         = preg_replace( '/[,\s]+/', '@space@', $value['meta_values'] ?? '' );
				$opt_val                = strtolower( $modifiedString );
				$opt_values[ $opt_val ] = $value['meta_values'] ?? '';
			}

			if ( ! empty( $field['meta_key'] ) ) {
				$fields[] = [
					'id'       => $field['meta_key'] ?? '',
					'type'     => 'select',
					'title'    => esc_html( $field['meta_name'] ) ?? '',
					'options'  => $opt_values,
					'multiple' => true,
					'chosen'   => true,
					'after'    => $meta_options,
					'class'    => 'job_specifications'
				];
			}
		}
	}

	if ( ! empty( $fields ) ) {
		CSF::createSection( $meta_prefix, array(
			'title'  => esc_html__( 'Specifications', 'jobus' ),
			'fields' => $fields,
			'icon'   => 'fas fa-cogs',
			'id'     => 'jobus_meta_specifications',
		) );
	} else {
		// Display message when no specifications are configured
		$settings_url = admin_url( 'edit.php?post_type=jobus_job&page=jobus-settings#tab=job-options' );
		CSF::createSection( $meta_prefix, array(
			'title'  => esc_html__( 'Specifications', 'jobus' ),
			'fields' => array(
				array(
					'type'    => 'content',
					'content' => '<div style="padding: 20px; background-color: #f8f9fa; border: 1px solid #dee2e6; border-radius: 4px; color: #666;">' .
					             '<p style="margin: 0;">' . esc_html__( 'No Job Specifications configured yet.', 'jobus' ) . '</p>' .
					             '<p style="margin: 8px 0 0 0;">' .
					             sprintf(
						             /* translators: %s: settings page link */
						             esc_html__( 'Please add the specifications from %s.', 'jobus' ),
						             '<a href="' . esc_url( $settings_url ) . '" target="_blank" style="color: #0073aa; text-decoration: none; font-weight: 500;">Settings > Job Options > Specifications</a>'
					             ) .
					             '</p>' .
					             '</div>',
				),
			),
			'icon'   => 'fas fa-cogs',
			'id'     => 'jobus_meta_specifications',
		) );
	}

	}