<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only parameters no nonce required.
$proposal_id   = ! empty( $_GET['p_id'] ) ? sanitize_text_field( wp_unslash( $_GET['p_id'] ) ) : '';
$profile_id    = gigneo_active_profile_id();
$employer_id   = absint( gigneo_post_meta( $proposal_id, '_gigneo_pm_employer_profile_id' ) ?? 0 );
$freelancer_id = absint( gigneo_post_meta( $proposal_id, '_gigneo_pm_profile_id' ) ?? 0 );

if ( $profile_id !== $freelancer_id && $profile_id !== $employer_id ) {
	$args = array(
		'title' => __( 'No proposal found', 'gigneo' ),
	);
	gigneo_not_found( 'proposal_detail', $args );
	return;
}
$job_id = gigneo_post_meta( $proposal_id, '_gigneo_pm_job_id' );

if ( empty( $job_id ) ) {
	$not_found_args = array(
		'title'       => esc_html__( 'Proposal not found', 'gigneo' ),
		'description' => esc_html__( 'The proposal you are looking for does not exist or has been deleted.', 'gigneo' ),
	);
	gigneo_not_found( 'proposal_detail', $not_found_args );
	return;
}

$current_user_id = get_current_user_id();
$job_author_id   = (int) get_post_field( 'post_author', $job_id );
$proposal_status = get_post_status( $proposal_id );

if ( $current_user_id === $job_author_id && 'publish' === $proposal_status ) {
	wp_update_post(
		array(
			'ID'          => $proposal_id,
			'post_status' => 'viewed',
		)
	);
	update_post_meta( $proposal_id, '_gigneo_pm_viewed', current_time( 'mysql' ) );
}

$author_id     = get_post_field( 'post_author', $proposal_id );
$order_id      = gigneo_post_meta( $proposal_id, '_gigneo_pm_order_id' );
$price         = gigneo_post_meta( $proposal_id, '_gigneo_pm_budget' );
$duration      = gigneo_post_meta( $proposal_id, '_gigneo_pm_duration' );
$duration_type = gigneo_post_meta( $proposal_id, '_gigneo_pm_duration_type' );

$template_args = array(
	'p_id'            => $proposal_id,
	'job_id'          => $job_id,
	'author_id'       => $author_id,
	'order_id'        => $order_id,
	'price'           => $price,
	'duration'        => $duration,
	'duration_type'   => $duration_type,
	'proposal_status' => $proposal_status,
);
?>
<div class="row gy-4 gn-proposal-detail">
	<div class="col-12">
		<?php
			//job info
			gigneo_load_template( 'dashboard/proposal/parts/job-info', true, $template_args );
		?>
	</div>
	<div class="col-lg-8">
		<div class="gn-dashboard-section gn-proposal-details-section">
			<?php
			//Cover Letter
			gigneo_load_template( 'dashboard/proposal/parts/cover-letter', true, $template_args );
			if ( in_array( 'intake_form', gigneo_active_modules(), true ) ) {
				//Questions
				gigneo_load_template( 'dashboard/proposal/parts/questions', true, $template_args );
			}
			// Attachments
			gigneo_load_template( 'dashboard/proposal/parts/attachments', true, $template_args );
			?>
		</div>
	</div>
	<div class="col-lg-4">
		<div class="gn-aside gn-dashboard-section">
			<?php
			//Overview
			gigneo_load_template( 'dashboard/proposal/parts/overview', true, $template_args );
			gigneo_load_template( 'dashboard/proposal/parts/hire-proposal', true, $template_args );
			?>
		</div>
	</div>
</div>
