<?php
/**
 * Template: Data Processing Records (GDPR Art. 30)
 *
 * Renders the data processing records management page with a records table,
 * inline creation form, and delete functionality.
 *
 * @var array $data JSON data from Cloud API (get-data-processing-records response).
 *                  Expected keys: records (array of record objects).
 * @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 = __( 'Data Processing Records', 'biscotti-cmp' );
include __DIR__ . '/partials/page-header.php';

// Extract records with safe default.
$records = isset( $data['records'] ) && is_array( $data['records'] ) ? $data['records'] : array();

// Legal basis options for the create form.
$legal_basis_options = array(
	'consent'              => __( 'Consent', 'biscotti-cmp' ),
	'contract'             => __( 'Contract', 'biscotti-cmp' ),
	'legal_obligation'     => __( 'Legal Obligation', 'biscotti-cmp' ),
	'vital_interests'      => __( 'Vital Interests', 'biscotti-cmp' ),
	'public_task'          => __( 'Public Task', 'biscotti-cmp' ),
	'legitimate_interests' => __( 'Legitimate Interests', 'biscotti-cmp' ),
);
?>

<div class="biscotti-page biscotti-page-data-processing">

	<p class="biscotti-page-description">
		<?php esc_html_e( 'Manage your GDPR Article 30 data processing records. Each record documents a processing activity including its purpose, legal basis, and data categories.', 'biscotti-cmp' ); ?>
	</p>

	<!-- Create Record Button -->
	<p>
		<button type="button" id="biscotti-toggle-create-form" class="button button-primary">
			<?php esc_html_e( 'Create New Record', 'biscotti-cmp' ); ?>
		</button>
	</p>

	<!-- Inline Create Form (hidden by default) -->
	<div id="biscotti-create-record-form-wrap" style="display:none;">
		<h2><?php esc_html_e( 'New Data Processing Record', 'biscotti-cmp' ); ?></h2>
		<form id="biscotti-create-record-form" class="biscotti-form">
			<table class="form-table" role="presentation">
				<?php
				Biscotti_Template_Renderer::form_field(
					'text',
					'name',
					'',
					array(
						'label'       => __( 'Name', 'biscotti-cmp' ),
						'placeholder' => __( 'e.g. Newsletter Subscription', 'biscotti-cmp' ),
					)
				);

				Biscotti_Template_Renderer::form_field(
					'textarea',
					'purpose',
					'',
					array(
						'label'       => __( 'Purpose', 'biscotti-cmp' ),
						'placeholder' => __( 'Describe the purpose of this data processing activity.', 'biscotti-cmp' ),
					)
				);

				Biscotti_Template_Renderer::form_field(
					'select',
					'legalBasis',
					'consent',
					array(
						'label'   => __( 'Legal Basis', 'biscotti-cmp' ),
						'choices' => $legal_basis_options,
					)
				);

				Biscotti_Template_Renderer::form_field(
					'text',
					'dataCategories',
					'',
					array(
						'label'       => __( 'Data Categories', 'biscotti-cmp' ),
						'placeholder' => __( 'e.g. Email, Name, IP Address (comma-separated)', 'biscotti-cmp' ),
						'description' => __( 'Enter data categories separated by commas.', 'biscotti-cmp' ),
					)
				);
				?>
			</table>

			<p class="submit">
				<button
					type="button"
					id="biscotti-save-record"
					class="button button-primary"
					data-label="<?php esc_attr_e( 'Create Record', 'biscotti-cmp' ); ?>"
					data-biscotti-save="create-data-processing-record"
					data-biscotti-form="#biscotti-create-record-form"
				>
					<?php esc_html_e( 'Create Record', 'biscotti-cmp' ); ?>
				</button>
				<button type="button" id="biscotti-cancel-create" class="button">
					<?php esc_html_e( 'Cancel', 'biscotti-cmp' ); ?>
				</button>
			</p>
		</form>
	</div>

	<!-- Records Table -->
	<?php if ( empty( $records ) ) : ?>
		<div class="biscotti-empty-state">
			<p><?php esc_html_e( 'No data processing records found. Create your first record to document your processing activities.', 'biscotti-cmp' ); ?></p>
		</div>
	<?php else : ?>
		<table class="widefat biscotti-data-table biscotti-data-processing-table">
			<thead>
				<tr>
					<th scope="col"><?php esc_html_e( 'Name', 'biscotti-cmp' ); ?></th>
					<th scope="col"><?php esc_html_e( 'Purpose', 'biscotti-cmp' ); ?></th>
					<th scope="col"><?php esc_html_e( 'Legal Basis', 'biscotti-cmp' ); ?></th>
					<th scope="col"><?php esc_html_e( 'Data Categories', 'biscotti-cmp' ); ?></th>
					<th scope="col"><?php esc_html_e( 'Updated', 'biscotti-cmp' ); ?></th>
					<th scope="col"><?php esc_html_e( 'Actions', 'biscotti-cmp' ); ?></th>
				</tr>
			</thead>
			<tbody>
				<?php foreach ( $records as $record ) :
					$record_id       = isset( $record['id'] ) ? $record['id'] : '';
					$record_name     = isset( $record['name'] ) ? $record['name'] : '';
					$record_purpose  = isset( $record['purpose'] ) ? $record['purpose'] : '';
					$record_basis    = isset( $record['legalBasis'] ) ? $record['legalBasis'] : '';
					$record_cats     = isset( $record['dataCategories'] ) && is_array( $record['dataCategories'] ) ? $record['dataCategories'] : array();
					$record_updated  = isset( $record['updatedAt'] ) ? $record['updatedAt'] : '';

					// Format legal basis for display.
					$basis_label = isset( $legal_basis_options[ $record_basis ] ) ? $legal_basis_options[ $record_basis ] : $record_basis;

					// Format date for display.
					$formatted_date = '';
					if ( ! empty( $record_updated ) ) {
						$timestamp      = strtotime( $record_updated );
						$formatted_date = $timestamp ? date_i18n( get_option( 'date_format' ), $timestamp ) : $record_updated;
					}
				?>
					<tr>
						<td><?php echo esc_html( $record_name ); ?></td>
						<td><?php echo esc_html( $record_purpose ); ?></td>
						<td><?php echo esc_html( $basis_label ); ?></td>
						<td><?php echo esc_html( implode( ', ', array_map( 'sanitize_text_field', $record_cats ) ) ); ?></td>
						<td><?php echo esc_html( $formatted_date ); ?></td>
						<td>
							<button
								type="button"
								class="button button-small button-link-delete biscotti-delete-record"
								data-record-id="<?php echo esc_attr( $record_id ); ?>"
								data-record-name="<?php echo esc_attr( $record_name ); ?>"
							>
								<?php esc_html_e( 'Delete', 'biscotti-cmp' ); ?>
							</button>
						</td>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>
	<?php endif; ?>

</div>
