<?php

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

use Gigneo\Modules;

$service_id       = ! empty( $args['p_id'] ) ? absint( $args['p_id'] ) : 0;
$current_user_obj = wp_get_current_user();
$profile_id       = gigneo_active_profile_id();
$profile_type     = gigneo_profile_type( $profile_id );
$freelancer_label = gigneo_freelancer_singular();
$employer_label   = gigneo_employer_singular();

if ( is_user_logged_in() && 'employer' !== $profile_type ) {
	$not_found_args = array(
		'title'       => sprintf(
			// translators: %s: Employer label.
			esc_html__( 'Only %1$s can hire!', 'gigneo' ),
			strtolower( $employer_label )
		),
		'description' => sprintf(
			// translators: %s: Freelancer label.
			esc_html__( 'Create or use a %1$s to place orders.', 'gigneo' ),
			strtolower( $freelancer_label )
		),
	);
	gigneo_not_found( 'hire', $not_found_args );
	return;
}

if ( 'gigneo_service' !== get_post_type( $service_id ) || 'publish' !== get_post_status( $service_id ) ) {
	$not_found_args = array(
		'title'       => esc_html__( 'Service not found.', 'gigneo' ),
		'description' => esc_html__( 'The requested service could not be found.', 'gigneo' ),
	);
	gigneo_not_found( 'hire', $not_found_args );
	return;
}

$service_author = (int) get_post_field( 'post_author', $service_id );

if ( $service_author === $current_user_obj->ID ) {
	$not_found_args = array(
		'title'       => esc_html__( 'You cannot hire yourself.', 'gigneo' ),
		'description' => esc_html__( 'You cannot place an order on your own service.', 'gigneo' ),
	);
	gigneo_not_found( 'hire', $not_found_args );
	return;
}

// phpcs:disable WordPress.Security.NonceVerification.Recommended -- Read-only parameters no nonce required.
$plan          = ! empty( $_GET['plan'] ) ? sanitize_text_field( wp_unslash( $_GET['plan'] ) ) : 'one';
$delivery_type = ! empty( $_GET['delivery_type'] ) ? sanitize_text_field( wp_unslash( $_GET['delivery_type'] ) ) : '';
$location_id   = ! empty( $_GET['location_id'] ) ? sanitize_text_field( wp_unslash( $_GET['location_id'] ) ) : '';
$location      = ! empty( $_GET['location'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_GET['location'] ) ) : array();
$address       = ! empty( $_GET['friendly_address'] ) ? sanitize_text_field( wp_unslash( $_GET['friendly_address'] ) ) : '';
// phpcs:enable WordPress.Security.NonceVerification.Recommended -- Read-only parameters no nonce required.

$template_args = array(
	'service_id'    => $service_id,
	'plan'          => $plan,
	'delivery_type' => $delivery_type,
	'location_id'   => $location_id,
	'location'      => $location,
	'address'       => $address,
);
?>
<div class="gn-hire gn-hire-service gn-hire-service-fixed">
	<div class="gn-hire-details">
		<form id="gn-hire-service-form" class="gn-hire-service-form">
			<div class="gn-main">
				<?php
				//Info
				gigneo_load_template( 'single/hire/service/parts/info', true, $template_args );

				//Addons
				if ( in_array( 'addons', gigneo_active_modules(), true ) ) {
					gigneo_load_template( 'single/hire/service/parts/addons', true, $template_args );
				}

				//Requirements
				if ( in_array( 'intake_form', gigneo_active_modules(), true ) ) {
					gigneo_load_template( 'single/hire/service/parts/requirements', true, $template_args );
				}
				?>
			</div>
			<div class="gn-aside">
				<?php
				//Vacation
				do_action( 'gigneo_single_hire_service_fixed_vacation_notice', $template_args );

				//Summary
				gigneo_load_template( 'single/hire/service/parts/summary', true, $template_args );
				?>
			</div>
		</form>
	</div>
</div>
