<?php
/**
 * Template: Services / Provider Editor (native, full parity)
 *
 * Mirrors the Cloud Services view: a table of detected services (name +
 * manually-edited badge, category, provider type, vendor) with a per-service
 * provider editor modal. Editing loads merged provider data via the get-provider
 * proxy; saving persists overrides via save-provider; reset clears them via
 * delete-provider. "Use operator data" pre-fills vendor fields with the website
 * operator details (returned inline by get-provider).
 *
 * @var array $data get-services response ({ services:[{id,providerType,isManuallyEdited,overrideCategory,service:{name,category,vendor}}] }).
 * @package Biscotti_CMP
 */

defined( 'ABSPATH' ) || exit;

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template is included inside Biscotti_Admin::render_dynamic_page(); these are function-scoped locals, not globals.

$page_title = __( 'Services & Providers', 'biscotti-cmp' );
include __DIR__ . '/partials/page-header.php';

if ( null === $data ) :
	include __DIR__ . '/partials/loading-state.php';
	return;
endif;

$services = ( isset( $data['services'] ) && is_array( $data['services'] ) ) ? $data['services'] : array();

$category_icons = array(
	'ESSENTIAL'       => '🔒',
	'FUNCTIONAL'      => '⚙️',
	'ANALYTICS'       => '📊',
	'MARKETING'       => '📢',
	'PERSONALIZATION' => '🎯',
);

$provider_type_labels = array(
	'FIRST_PARTY' => __( 'First party', 'biscotti-cmp' ),
	'SELF_HOSTED' => __( 'Self-hosted', 'biscotti-cmp' ),
	'THIRD_PARTY' => __( 'Third party', 'biscotti-cmp' ),
);

$category_options = array( 'ESSENTIAL', 'FUNCTIONAL', 'ANALYTICS', 'MARKETING', 'PERSONALIZATION' );
?>

<div class="biscotti-page biscotti-page-services">

	<div class="biscotti-card">
		<h3>🔧 <?php esc_html_e( 'Services & Providers', 'biscotti-cmp' ); ?></h3>
		<p class="description"><?php esc_html_e( 'Review the services detected on your site and edit provider details used in your legal documents and consent notice.', 'biscotti-cmp' ); ?></p>

		<?php if ( empty( $services ) ) : ?>
			<div class="biscotti-empty-state">
				<span class="dashicons dashicons-admin-plugins biscotti-empty-icon"></span>
				<h3><?php esc_html_e( 'No services detected yet', 'biscotti-cmp' ); ?></h3>
				<p><?php esc_html_e( 'Run a website scan to detect services and cookies on your site.', 'biscotti-cmp' ); ?></p>
			</div>
		<?php else : ?>
			<table class="widefat biscotti-data-table biscotti-services-table">
				<thead>
					<tr>
						<th scope="col"><?php esc_html_e( 'Service', 'biscotti-cmp' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Category', 'biscotti-cmp' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Provider Type', 'biscotti-cmp' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Vendor', 'biscotti-cmp' ); ?></th>
						<th scope="col"></th>
					</tr>
				</thead>
				<tbody>
					<?php
					foreach ( $services as $ws ) :
						$ws_id        = isset( $ws['id'] ) ? (string) $ws['id'] : '';
						$svc          = ( isset( $ws['service'] ) && is_array( $ws['service'] ) ) ? $ws['service'] : array();
						$svc_name     = isset( $svc['name'] ) ? sanitize_text_field( (string) $svc['name'] ) : __( 'Unknown', 'biscotti-cmp' );
						$category     = ! empty( $ws['overrideCategory'] ) ? (string) $ws['overrideCategory'] : ( isset( $svc['category'] ) ? (string) $svc['category'] : '' );
						$category     = strtoupper( $category );
						$cat_icon     = isset( $category_icons[ $category ] ) ? $category_icons[ $category ] : '📋';
						$provider_t   = isset( $ws['providerType'] ) ? (string) $ws['providerType'] : 'THIRD_PARTY';
						$type_label   = isset( $provider_type_labels[ $provider_t ] ) ? $provider_type_labels[ $provider_t ] : $provider_t;
						$vendor       = isset( $svc['vendor'] ) && '' !== $svc['vendor'] ? sanitize_text_field( (string) $svc['vendor'] ) : '—';
						$is_edited    = ! empty( $ws['isManuallyEdited'] );
						?>
						<tr data-ws-id="<?php echo esc_attr( $ws_id ); ?>">
							<td>
								<strong><?php echo esc_html( $svc_name ); ?></strong>
								<?php if ( $is_edited ) : ?>
									<span class="biscotti-pill biscotti-pill-amber" style="margin-left:6px;">✏️ <?php esc_html_e( 'Edited', 'biscotti-cmp' ); ?></span>
								<?php endif; ?>
							</td>
							<td><?php echo esc_html( $cat_icon . ' ' . ( '' !== $category ? $category : '—' ) ); ?></td>
							<td><span class="biscotti-pill biscotti-pill-gray"><?php echo esc_html( $type_label ); ?></span></td>
							<td><?php echo esc_html( $vendor ); ?></td>
							<td>
								<button type="button" class="button button-small biscotti-svc-edit" data-ws-id="<?php echo esc_attr( $ws_id ); ?>">✏️ <?php esc_html_e( 'Edit', 'biscotti-cmp' ); ?></button>
							</td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		<?php endif; ?>
	</div>

</div>

<!-- Provider Editor Modal -->
<div id="biscotti-svc-modal" class="biscotti-doc-overlay" role="dialog" aria-modal="true" aria-labelledby="biscotti-svc-modal-title">
	<div class="biscotti-doc-modal biscotti-doc-modal-sm">
		<div class="biscotti-doc-modal-header">
			<h3 id="biscotti-svc-modal-title">✏️ <?php esc_html_e( 'Edit Provider', 'biscotti-cmp' ); ?></h3>
			<button type="button" class="biscotti-doc-modal-close" id="biscotti-svc-close" aria-label="<?php esc_attr_e( 'Close', 'biscotti-cmp' ); ?>">&times;</button>
		</div>
		<div class="biscotti-doc-modal-body">
			<div id="biscotti-svc-hint" class="biscotti-svc-hint" style="display:none;">
				⚠️ <?php esc_html_e( 'This is a first-party or self-hosted service. Use your operator data as the provider, or enter custom details.', 'biscotti-cmp' ); ?>
			</div>
			<input type="hidden" id="biscotti-svc-ws-id" value="" />
			<p class="biscotti-form-row"><label for="biscotti-svc-vendor"><?php esc_html_e( 'Vendor', 'biscotti-cmp' ); ?></label><input type="text" id="biscotti-svc-vendor" class="biscotti-svc-input" /></p>
			<p class="biscotti-form-row"><label for="biscotti-svc-location"><?php esc_html_e( 'Vendor Location', 'biscotti-cmp' ); ?></label><textarea id="biscotti-svc-location" class="biscotti-svc-input" rows="2"></textarea></p>
			<p class="biscotti-form-row"><label for="biscotti-svc-privacy"><?php esc_html_e( 'Privacy Policy URL', 'biscotti-cmp' ); ?></label><input type="url" id="biscotti-svc-privacy" class="biscotti-svc-input" placeholder="https://..." /></p>
			<p class="biscotti-form-row"><label for="biscotti-svc-legal"><?php esc_html_e( 'Legal Basis', 'biscotti-cmp' ); ?></label><input type="text" id="biscotti-svc-legal" class="biscotti-svc-input" /></p>
			<p class="biscotti-form-row"><label for="biscotti-svc-duration"><?php esc_html_e( 'Storage Duration', 'biscotti-cmp' ); ?></label><input type="text" id="biscotti-svc-duration" class="biscotti-svc-input" /></p>
			<p class="biscotti-form-row">
				<label for="biscotti-svc-category"><?php esc_html_e( 'Category', 'biscotti-cmp' ); ?></label>
				<select id="biscotti-svc-category" class="biscotti-svc-input">
					<option value="">—</option>
					<?php foreach ( $category_options as $c ) : ?>
						<option value="<?php echo esc_attr( $c ); ?>"><?php echo esc_html( ( isset( $category_icons[ $c ] ) ? $category_icons[ $c ] . ' ' : '' ) . $c ); ?></option>
					<?php endforeach; ?>
				</select>
			</p>
			<p class="biscotti-form-row"><label for="biscotti-svc-desc"><?php esc_html_e( 'Description', 'biscotti-cmp' ); ?></label><textarea id="biscotti-svc-desc" class="biscotti-svc-input" rows="3"></textarea></p>
		</div>
		<div class="biscotti-doc-modal-footer">
			<button type="button" class="button button-primary" id="biscotti-svc-save">💾 <?php esc_html_e( 'Save', 'biscotti-cmp' ); ?></button>
			<button type="button" class="button" id="biscotti-svc-use-operator">🏢 <?php esc_html_e( 'Use Operator Data', 'biscotti-cmp' ); ?></button>
			<button type="button" class="button biscotti-btn-danger" id="biscotti-svc-reset" style="display:none;">↩️ <?php esc_html_e( 'Reset', 'biscotti-cmp' ); ?></button>
			<span id="biscotti-svc-status" class="biscotti-save-status" role="status"></span>
		</div>
	</div>
</div>
