<?php
/**
 * Renders a fillable form from a handover schema snapshot.
 *
 * Expected variables:
 *   $schemaFields  — list of field definitions from schema_snapshot['fields']
 *   $fieldValues   — associative array of current field values (key => value)
 *   $readonly      — bool, if true all inputs are disabled
 *   $fieldDiff     — (optional) associative array of diffs: key => ['type' => added|removed|changed, 'old' => ..., 'new' => ...]
 */
$schemaFields = is_array($schemaFields ?? null) ? $schemaFields : [];
$fieldValues = is_array($fieldValues ?? null) ? $fieldValues : [];
$fieldDiff = is_array($fieldDiff ?? null) ? $fieldDiff : [];
$readonly = !empty($readonly);
$hasDiff = $fieldDiff !== [];
?>
<?php if ($schemaFields === []): ?>
  <p class="app-muted"><?php e(t('No protocol fields defined.')); ?></p>
<?php else: ?>
  <?php foreach ($schemaFields as $index => $field):
      $type = (string) ($field['type'] ?? '');
      $label = (string) ($field['label'] ?? '');
      $key = (string) ($field['key'] ?? '');
      $required = !empty($field['required']);
      $value = $key !== '' ? (string) ($fieldValues[$key] ?? '') : '';
      $inputId = 'handover-field-' . $index;
      $diff = ($key !== '' && isset($fieldDiff[$key])) ? $fieldDiff[$key] : null;
      $diffClass = $diff !== null ? ' handover-diff-' . ($diff['type'] ?? '') : '';
  ?>
    <?php if ($type === 'heading'): ?>
      <h3 class="handover-form-heading"><?php e($label); ?></h3>

    <?php elseif ($type === 'paragraph'): ?>
      <p class="app-muted"><?php e($label); ?></p>

    <?php elseif ($type === 'checkbox'): ?>
      <div class="handover-diff-field<?php e($diffClass); ?>">
        <label class="handover-form-checkbox-label">
          <input
            type="checkbox"
            id="<?php e($inputId); ?>"
            name="field_<?php e($key); ?>"
            value="1"
            <?php if ($value === '1'): ?> checked<?php endif; ?>
            <?php if ($readonly): ?> disabled<?php endif; ?>
          >
          <?php e($label); ?>
          <?php if ($required): ?><span class="handover-form-required"> *</span><?php endif; ?>
        </label>
        <?php if ($diff !== null): ?>
          <span class="handover-diff-indicator" aria-label="<?php e(t('Changed')); ?>">
            <?php if ($diff['type'] === 'changed'): ?>
              <small class="app-muted"><?php e(t('was: %s', ($diff['old'] ?? '') === '1' ? t('Yes') : t('No'))); ?></small>
            <?php endif; ?>
          </span>
        <?php endif; ?>
      </div>

    <?php elseif ($type === 'select'): ?>
      <div class="handover-diff-field<?php e($diffClass); ?>">
        <label for="<?php e($inputId); ?>">
          <?php e($label); ?>
          <?php if ($required): ?><span class="handover-form-required"> *</span><?php endif; ?>
        </label>
        <select
          id="<?php e($inputId); ?>"
          name="field_<?php e($key); ?>"
          <?php if ($required): ?> aria-required="true"<?php endif; ?>
          <?php if ($readonly): ?> disabled<?php endif; ?>
        >
          <option value=""><?php e(t('Please select...')); ?></option>
          <?php foreach (($field['options'] ?? []) as $opt): ?>
            <option value="<?php e((string) ($opt['value'] ?? '')); ?>"<?php if ($value === (string) ($opt['value'] ?? '')): ?> selected<?php endif; ?>>
              <?php e((string) ($opt['label'] ?? '')); ?>
            </option>
          <?php endforeach; ?>
        </select>
        <?php if ($diff !== null && $diff['type'] === 'changed'): ?>
          <small class="app-muted handover-diff-indicator" aria-label="<?php e(t('Changed')); ?>"><?php e(t('was: %s', (string) ($diff['old'] ?? ''))); ?></small>
        <?php endif; ?>
      </div>

    <?php elseif ($type === 'textarea'): ?>
      <div class="handover-diff-field<?php e($diffClass); ?>">
        <label for="<?php e($inputId); ?>">
          <?php e($label); ?>
          <?php if ($required): ?><span class="handover-form-required"> *</span><?php endif; ?>
        </label>
        <textarea
          id="<?php e($inputId); ?>"
          name="field_<?php e($key); ?>"
          rows="4"
          <?php if ($required): ?> aria-required="true"<?php endif; ?>
          <?php if ($readonly): ?> disabled<?php endif; ?>
        ><?php e($value); ?></textarea>
        <?php if ($diff !== null && $diff['type'] === 'changed'): ?>
          <small class="app-muted handover-diff-indicator" aria-label="<?php e(t('Changed')); ?>"><?php e(t('was: %s', (string) ($diff['old'] ?? ''))); ?></small>
        <?php endif; ?>
      </div>

    <?php else: ?>
      <?php
        $inputType = match ($type) {
            'number' => 'number',
            'date' => 'date',
            default => 'text',
        };
      ?>
      <div class="handover-diff-field<?php e($diffClass); ?>">
        <label for="<?php e($inputId); ?>">
          <?php e($label); ?>
          <?php if ($required): ?><span class="handover-form-required"> *</span><?php endif; ?>
        </label>
        <input
          type="<?php e($inputType); ?>"
          id="<?php e($inputId); ?>"
          name="field_<?php e($key); ?>"
          value="<?php e($value); ?>"
          <?php if ($required): ?> aria-required="true"<?php endif; ?>
          <?php if ($readonly): ?> disabled<?php endif; ?>
        >
        <?php if ($diff !== null && $diff['type'] === 'changed'): ?>
          <small class="app-muted handover-diff-indicator" aria-label="<?php e(t('Changed')); ?>"><?php e(t('was: %s', (string) ($diff['old'] ?? ''))); ?></small>
        <?php endif; ?>
      </div>
    <?php endif; ?>
  <?php endforeach; ?>
<?php endif; ?>
