<?php
/**
 * Admin-View: Einzelansicht einer Rechnung
 *
 * @var \stdClass         $invoice
 * @var \stdClass|null    $file
 * @var \stdClass[]       $auditTrail
 * @var array|null        $reportData
 * @var string            $downloadUrl
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

use Itdatex\Rechnungseingang\Plugin;

$fmt = static fn( ?float $v ): string => $v === null ? '—' : number_format( (float) $v, 2, ',', '.' );
$dl  = static fn( ?string $d ): string => $d ? mysql2date( 'd.m.Y', (string) $d ) : '—';

$status_label = [
    'valid'     => __( 'gueltig',                  'itdatex-e-invoice-intake' ),
    'warnings'  => __( 'mit Warnungen',            'itdatex-e-invoice-intake' ),
    'invalid'   => __( 'Fehler in der Validierung', 'itdatex-e-invoice-intake' ),
    'unchecked' => __( 'nicht geprueft',            'itdatex-e-invoice-intake' ),
];

$inbox_url = admin_url( 'admin.php?page=itdatex-e-invoice-intake' );
?>
<div class="wrap reing-wrap reing-detail">
  <h1>
    <?php echo esc_html( $invoice->invoice_number ?: __( 'Rechnung', 'itdatex-e-invoice-intake' ) ); ?>
    <a href="<?php echo esc_url( $inbox_url ); ?>" class="page-title-action"><?php esc_html_e( '← Eingang', 'itdatex-e-invoice-intake' ); ?></a>
    <?php if ( $downloadUrl ) : ?>
      <a href="<?php echo esc_url( $downloadUrl ); ?>" class="page-title-action"><?php esc_html_e( 'Datei herunterladen', 'itdatex-e-invoice-intake' ); ?></a>
    <?php endif; ?>
  </h1>

  <div class="reing-grid">

    <div class="reing-card">
      <h2><?php esc_html_e( 'Kopfdaten', 'itdatex-e-invoice-intake' ); ?></h2>
      <table class="reing-kv">
        <tr><th><?php esc_html_e( 'Rechnungsnummer', 'itdatex-e-invoice-intake' ); ?></th><td><?php echo esc_html( $invoice->invoice_number ?: '—' ); ?></td></tr>
        <tr><th><?php esc_html_e( 'Format',         'itdatex-e-invoice-intake' ); ?></th><td><code><?php echo esc_html( $invoice->format ); ?></code></td></tr>
        <tr><th><?php esc_html_e( 'Rechnungsdatum', 'itdatex-e-invoice-intake' ); ?></th><td><?php echo esc_html( $dl( $invoice->issue_date ) ); ?></td></tr>
        <tr><th><?php esc_html_e( 'Faelligkeit',    'itdatex-e-invoice-intake' ); ?></th><td><?php echo esc_html( $dl( $invoice->due_date ) ); ?></td></tr>
        <tr><th><?php esc_html_e( 'Leitweg-ID',     'itdatex-e-invoice-intake' ); ?></th><td><?php echo esc_html( $invoice->buyer_reference ?: '—' ); ?></td></tr>
        <tr><th><?php esc_html_e( 'Positionen',     'itdatex-e-invoice-intake' ); ?></th><td><?php echo (int) $invoice->line_count; ?></td></tr>
      </table>
    </div>

    <div class="reing-card">
      <h2><?php esc_html_e( 'Parteien', 'itdatex-e-invoice-intake' ); ?></h2>
      <table class="reing-kv">
        <tr><th><?php esc_html_e( 'Verkaeufer',         'itdatex-e-invoice-intake' ); ?></th><td><?php echo esc_html( $invoice->seller_name ?: '—' ); ?></td></tr>
        <tr><th><?php esc_html_e( 'USt-IdNr.',           'itdatex-e-invoice-intake' ); ?></th><td><?php echo esc_html( $invoice->seller_vat_id ?: '—' ); ?></td></tr>
        <tr><th><?php esc_html_e( 'Kaeufer',             'itdatex-e-invoice-intake' ); ?></th><td><?php echo esc_html( $invoice->buyer_name ?: '—' ); ?></td></tr>
      </table>
    </div>

    <div class="reing-card">
      <h2><?php esc_html_e( 'Betraege', 'itdatex-e-invoice-intake' ); ?></h2>
      <table class="reing-kv reing-amounts">
        <tr><th><?php esc_html_e( 'Netto',  'itdatex-e-invoice-intake' ); ?></th><td><?php echo esc_html( $fmt( $invoice->total_net !== null ? (float) $invoice->total_net : null ) . ' ' . $invoice->currency ); ?></td></tr>
        <tr><th><?php esc_html_e( 'USt',    'itdatex-e-invoice-intake' ); ?></th><td><?php echo esc_html( $fmt( $invoice->total_tax !== null ? (float) $invoice->total_tax : null ) . ' ' . $invoice->currency ); ?></td></tr>
        <tr><th><strong><?php esc_html_e( 'Brutto', 'itdatex-e-invoice-intake' ); ?></strong></th><td><strong><?php echo esc_html( $fmt( $invoice->total_gross !== null ? (float) $invoice->total_gross : null ) . ' ' . $invoice->currency ); ?></strong></td></tr>
      </table>
    </div>

    <div class="reing-card">
      <h2><?php esc_html_e( 'Validierung', 'itdatex-e-invoice-intake' ); ?></h2>
      <p>
        <span class="reing-badge reing-badge-<?php echo esc_attr( $invoice->validation_status ); ?>">
          <?php echo esc_html( $status_label[ $invoice->validation_status ] ?? $invoice->validation_status ); ?>
        </span>
        — <?php
        echo esc_html( sprintf(
            /* translators: 1: errors, 2: warnings */
            __( '%1$d Fehler · %2$d Warnungen', 'itdatex-e-invoice-intake' ),
            (int) $invoice->validation_errors,
            (int) $invoice->validation_warnings
        ) );
        ?>
      </p>

      <?php if ( $reportData && ! empty( $reportData['issues'] ) ) : ?>
        <details>
          <summary><?php esc_html_e( 'Details anzeigen', 'itdatex-e-invoice-intake' ); ?></summary>
          <ul class="reing-issues">
            <?php foreach ( $reportData['issues'] as $issue ) : ?>
              <li class="reing-issue reing-issue-<?php echo esc_attr( $issue['severity'] ?? 'info' ); ?>">
                <code><?php echo esc_html( $issue['source'] ?? '?' ); ?></code>
                <strong><?php echo esc_html( $issue['severity'] ?? '?' ); ?></strong>
                — <?php echo esc_html( $issue['message'] ?? '' ); ?>
              </li>
            <?php endforeach; ?>
          </ul>
        </details>
      <?php endif; ?>
    </div>

    <?php
    /**
     * Fires after the validation card on the invoice detail page.
     * Add-ons can render their own cards here (e.g. extended validation
     * findings or AI-generated explanations).
     *
     * @param object     $invoice    Invoice DB row.
     * @param array|null $reportData Decoded Free-Plugin validation report.
     */
    do_action( 'reing_invoice_view_after_validation', $invoice, $reportData );
    ?>

    <div class="reing-card">
      <h2><?php esc_html_e( 'Datei', 'itdatex-e-invoice-intake' ); ?></h2>
      <?php if ( $file ) : ?>
        <table class="reing-kv">
          <tr><th><?php esc_html_e( 'Originalname', 'itdatex-e-invoice-intake' ); ?></th><td><?php echo esc_html( $file->original_name ); ?></td></tr>
          <tr><th>MIME</th><td><code><?php echo esc_html( $file->mime ); ?></code></td></tr>
          <tr><th><?php esc_html_e( 'Groesse',       'itdatex-e-invoice-intake' ); ?></th><td><?php echo esc_html( size_format( (int) $file->size_bytes ) ); ?></td></tr>
          <tr><th>SHA-256</th><td><code style="word-break:break-all;font-size:0.78em;"><?php echo esc_html( $file->sha256 ); ?></code></td></tr>
          <tr><th><?php esc_html_e( 'Kettenhash', 'itdatex-e-invoice-intake' ); ?></th><td><code style="word-break:break-all;font-size:0.78em;"><?php echo esc_html( $file->chain_hash ); ?></code></td></tr>
        </table>
      <?php endif; ?>
    </div>

    <div class="reing-card">
      <h2><?php esc_html_e( 'Audit-Spur', 'itdatex-e-invoice-intake' ); ?></h2>
      <ul class="reing-audit">
        <?php foreach ( $auditTrail as $entry ) : ?>
          <li>
            <time><?php echo esc_html( mysql2date( 'd.m.Y H:i', (string) $entry->created_at ) ); ?></time>
            <strong><?php echo esc_html( $entry->action ); ?></strong>
            <em><?php echo esc_html( $entry->actor ); ?></em>
            <?php if ( $entry->detail ) : ?> — <?php echo esc_html( $entry->detail ); ?><?php endif; ?>
          </li>
        <?php endforeach; ?>
      </ul>
    </div>

  </div>
</div>
