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

$order_id     = $args['order_id'] ?? 0;
$profile_id   = $args['profile_id'] ?? 0;
$profile_type = $args['profile_type'] ?? '';
$date_format  = get_option( 'date_format' );

$plan_data     = get_post_meta( $order_id, '_gigneo_pm_price_plan', true );
$service_price = ! empty( $plan_data['price'] ) ? gigneo_price_format( $plan_data['price'], true ) : 0;
$sub_total     = get_post_meta( $order_id, '_gigneo_pm_subtotal', true );
$total         = get_post_meta( $order_id, '_gigneo_pm_total', true );

//Commission
$commission_total = 0;
$commission_title = esc_html__( 'Platform Commission', 'gigneo' );
if ( 'employer' === $profile_type ) {
	$commission       = gigneo_post_meta( $order_id, '_gigneo_pm_commission' );
	$commission_title = gigneo_option( '_gigneo_ts_platform_fee_name', esc_html__( 'Platform fee', 'gigneo' ) );
	$commission_total = $commission['total'] ?? 0;
} else {
	$commission       = gigneo_post_meta( $order_id, '_gigneo_pm_freelancer_commission' );
	if ( empty( $commission ) ) {
		$order_location   = gigneo_post_meta( $order_id, '_gigneo_pm_location' );
		$travel_charges   = isset( $order_location['total_travel_charges'] ) ? floatval( $order_location['total_travel_charges'] ) : 0;
		$commission       = gigneo_commission( $sub_total - $travel_charges, 'service', $profile_id );
	}
	$commission_title = gigneo_option( '_gigneo_ts_platform_fee_name', esc_html__( 'Platform fee', 'gigneo' ) );
	$commission_total = $commission['total'] ?? 0;
	$total            = $total - $commission_total;
}
?>
<div class="gn-order-payments-wrapper">
	<div id="gn-order-payments" class="gn-order-section gn-order-payments">
		<div class="gn-head">
			<h3 class="gn-title"><?php echo esc_html__( 'Payment overview', 'gigneo' ); ?></h3>
		</div>
		<div class="gn-body">
			<div class="gn-stat-cards">
				<div class="gn-stat-card">
					<h6 class="gn-label"><?php echo esc_html__( 'Order amount', 'gigneo' ); ?></h6>
					<h3 class="gn-value"><?php echo esc_html( gigneo_price_format( $sub_total, true ) ); ?></h3>
				</div>
				<?php if ( ! empty( $commission_total ) ) { ?>
				<div class="gn-stat-card">
						<span class="gn-label"><?php echo esc_html( $commission_title ); ?></span>
						<span class="gn-value gn-danger"><?php echo esc_html( gigneo_price_format( $commission_total, true ) ); ?></span>
				</div>
				<?php } ?>
				<?php if ( 'employer' === $profile_type ) { ?>
					<div class="gn-stat-card">
						<h6 class="gn-label"><?php echo esc_html__( 'Total spent', 'gigneo' ); ?></h6>
						<h3 class="gn-value"><?php echo esc_html( gigneo_price_format( $total, true ) ); ?></h3>
					</div>
				<?php } else { ?>
					<div class="gn-stat-card">
						<h6 class="gn-label"><?php echo esc_html__( "You'll earn", 'gigneo' ); ?></h6>
						<h3 class="gn-value"><?php echo esc_html( gigneo_price_format( $total - $commission_total, true ) ); ?></h3>
					</div>
				<?php } ?>
			</div>
		</div>
	</div>
<?php
$invoice_args = array(
	'post_type'      => 'gigneo_invoice',
	'post_status'    => 'any',
	'posts_per_page' => - 1,
	'orderby'        => 'date',
	'order'          => 'DESC',
	// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
	'meta_query'     => array(
		'relation' => 'AND',
		array(
			'key'     => '_gigneo_pm_order_id',
			'value'   => $order_id,
			'compare' => '=',
		),
		array(
			'key'     => '_gigneo_pm_profile_id',
			'value'   => $profile_id,
			'compare' => '=',
		),
	),
);

$invoice_query = new WP_Query( $invoice_args );
?>
<div id="gn-order-payments" class="gn-order-section gn-order-payments">
	<div class="gn-head">
			<h3 class="gn-title"><?php echo esc_html__( 'Invoices', 'gigneo' ); ?></h3>
		</div>
		<div class="gn-body">
			<?php if ( $invoice_query->have_posts() ) { ?>
				<ul class="gn-invoice-list">
					<?php
					while ( $invoice_query->have_posts() ) {
						$invoice_query->the_post();
						$invoice_id            = get_the_ID();
						$invoice_status        = get_post_status( $invoice_id );
						$invoice_type          = gigneo_post_meta( $invoice_id, '_gigneo_pm_type' );
						$invoice_total         = gigneo_post_meta( $invoice_id, '_gigneo_pm_total' );
						$invoice_amount        = gigneo_post_meta( $invoice_id, '_gigneo_pm_credit' );
						$invoice_timestamp_utc = get_post_time( 'Y-m-d H:i:s', true );
						$invoice_publish_date  = gigneo_datetime( $invoice_timestamp_utc, $date_format );
						$invoice_status_label  = gigneo_status_label( $invoice_status );
						// Handle credit type
						if ( 'credit' === $invoice_type ) {
							$invoice_type = $invoice_amount > 0 ? 'credit_purchased' : 'credit_spent';
						}
						// If commission type, choose freelancer or employer version
						if ( 'service_commission' === $invoice_type ) {
							$invoice_type = ( 'freelancer' === $profile_type ) ? 'freelancer_fee' : 'employer_fee';
						}
						$invoice_label = gigneo_invoice_label( $invoice_type );
						?>
						<li class="gn-invoice">
						<span class="gn-icon">
							<i class="hi hi-document-text" aria-hidden="true"></i>
						</span>
							<div class="gn-info">
								<h5 class="gn-title">
									<?php echo esc_html( gigneo_format_id( $invoice_id, 'invoice', false ) ) . '.pdf'; ?>
									<span class="gn-status-tag <?php echo esc_attr( gigneo_status_class( $invoice_status ) ); ?>"><?php echo esc_html( $invoice_status_label ); ?></span>
								</h5>
								<ul class="gn-details">
									<li class="gn-date">
										<?php
										printf(
										// translators: %s is invoice date
											esc_html__( 'Generated %s', 'gigneo' ),
											esc_html( gigneo_datetime( $invoice_publish_date, $date_format ) )
										);
										?>
									</li>
									<?php if ( ! empty( $invoice_label ) ) { ?>
										<li class="gn-type"><?php echo esc_html( $invoice_label ); ?></li>
									<?php } ?>
								</ul>
							</div>
							<div class="gn-actions">
								<button type="button" data-gn-invoice-id="<?php echo esc_attr( $invoice_id ); ?>"
										class="gn-btn gn-invoice-download">
									<i class="hi hi-arrow-down-tray" aria-hidden="true"></i>
								</button>
							</div>
						</li>
					<?php } ?>
				</ul>
				<?php
			} else {
				$not_found_args = array(
					'title'       => esc_html__( 'No invoices', 'gigneo' ),
					'description' => esc_html__( 'All invoices for this order will appear here once available.', 'gigneo' ),
				);

				gigneo_not_found( 'service_order_invoices', $not_found_args );
			}
			?>
		</div>
	</div>
</div>
