<div class="p-4 w-[54rem]">
  <p class="mb-4 text-gray-300">
    This will restore the embedded metadata and cover (if available) from a saved backup for the selected book.
  </p>

  @if (book) {
    <div class="mb-4">
      <p class="font-semibold text-gray-300 pb-2">Book to Restore:</p>
      <div class="border p-3 rounded space-y-1 custom-table">
        <div><strong>ID:</strong> {{ book.id }}</div>
        <div><strong>Current Title:</strong> {{ book.metadata?.title }}</div>
      </div>
    </div>
  }

  @if (backupMetadata) {
    <div class="mb-4">
      <p class="font-semibold text-gray-300 pb-2">Backup Metadata Preview:</p>
      <div class="p-6 rounded custom-table text-sm text-gray-400 flex flex-col gap-6">

        <div class="flex">
          <div class="flex-1 space-y-1">
            @if (backupMetadata.title) {
              <div><strong>Title:</strong> {{ backupMetadata.title }}</div>
            }
            @if (backupMetadata.subtitle) {
              <div><strong>Subtitle:</strong> {{ backupMetadata.subtitle }}</div>
            }
            @if (backupMetadata.authors!.length) {
              <div><strong>Authors:</strong> {{ backupMetadata.authors!.join(', ') }}</div>
            }
            @if (backupMetadata.publisher) {
              <div><strong>Publisher:</strong> {{ backupMetadata.publisher }}</div>
            }
            @if (backupMetadata.publishedDate) {
              <div><strong>Published Date:</strong> {{ backupMetadata.publishedDate }}</div>
            }
            @if (backupMetadata.seriesName) {
              <div><strong>Series Name:</strong> {{ backupMetadata.seriesName }}</div>
            }
            @if (backupMetadata.seriesNumber !== null) {
              <div><strong>Series Number:</strong> {{ backupMetadata.seriesNumber }}</div>
            }
            @if (backupMetadata.seriesTotal !== null) {
              <div><strong>Series Total:</strong> {{ backupMetadata.seriesTotal }}</div>
            }
            @if (backupMetadata.isbn13) {
              <div><strong>ISBN-13:</strong> {{ backupMetadata.isbn13 }}</div>
            }
            @if (backupMetadata.isbn10) {
              <div><strong>ISBN-10:</strong> {{ backupMetadata.isbn10 }}</div>
            }
            @if (backupMetadata.pageCount !== null) {
              <div><strong>Page Count:</strong> {{ backupMetadata.pageCount }}</div>
            }
            @if (backupMetadata.language) {
              <div><strong>Language:</strong> {{ backupMetadata.language }}</div>
            }
            @if (backupMetadata.asin) {
              <div><strong>ASIN:</strong> {{ backupMetadata.asin }}</div>
            }
            @if (backupMetadata.amazonRating !== null) {
              <div><strong>Amazon Rating:</strong> {{ backupMetadata.amazonRating }}</div>
            }
            @if (backupMetadata.amazonReviewCount !== null) {
              <div><strong>Amazon Review Count:</strong> {{ backupMetadata.amazonReviewCount }}</div>
            }
            @if (backupMetadata.comicvineId) {
              <div><strong>Comicvine ID:</strong> {{ backupMetadata.comicvineId }}</div>
            }
            @if (backupMetadata.goodreadsId) {
              <div><strong>Goodreads ID:</strong> {{ backupMetadata.goodreadsId }}</div>
            }
            @if (backupMetadata.goodreadsRating !== null) {
              <div><strong>Goodreads Rating:</strong> {{ backupMetadata.goodreadsRating }}</div>
            }
            @if (backupMetadata.goodreadsReviewCount !== null) {
              <div><strong>Goodreads Review Count:</strong> {{ backupMetadata.goodreadsReviewCount }}</div>
            }
            @if (backupMetadata.hardcoverId) {
              <div><strong>Hardcover ID:</strong> {{ backupMetadata.hardcoverId }}</div>
            }
            @if (backupMetadata.hardcoverRating !== null) {
              <div><strong>Hardcover Rating:</strong> {{ backupMetadata.hardcoverRating }}</div>
            }
            @if (backupMetadata.hardcoverReviewCount !== null) {
              <div><strong>Hardcover Review Count:</strong> {{ backupMetadata.hardcoverReviewCount }}</div>
            }
            @if (backupMetadata.googleId) {
              <div><strong>Google Books ID:</strong> {{ backupMetadata.googleId }}</div>
            }
            @if (backupMetadata.categories && backupMetadata.categories.length) {
              <div><strong>Categories:</strong> {{ backupMetadata.categories.join(', ') }}</div>
            }
          </div>
          <div class="w-48 flex-shrink-0">
            <div class="font-semibold text-gray-300 pb-2">Backup Cover Preview:</div>
            <div class="bg-gray-900 border p-2 rounded flex justify-center">
              <img
                [src]="urlHelperService.getBackupCoverUrl(book?.id!)"
                alt="Backup Cover"
                class="max-h-80 object-contain rounded shadow"
                onerror="this.src='assets/images/missing-cover.jpg'"/>
            </div>
          </div>
        </div>

        @if (backupMetadata.description) {
          <div class="text-sm text-gray-400">
            <div class="font-semibold text-gray-300 pb-1">Description:</div>
            {{ backupMetadata.description }}
          </div>
        }

      </div>
    </div>
  }

  @if (!backupMetadata) {
    <div class="mb-4">
      <div class="p-4 border border-yellow-700 rounded bg-yellow-900 text-yellow-300 text-sm shadow-sm">
        <p class="font-medium mb-1 text-yellow-200">No backup available</p>
        <p>We couldn't find a saved backup for this book. Metadata and cover cannot be restored.</p>
      </div>
    </div>
  }

  <div class="flex justify-end gap-3 mt-6">
    <p-button
      severity="info"
      outlined
      label="Cancel"
      (onClick)="dynamicDialogRef.close()"
      icon="pi pi-times">
    </p-button>

    <p-button
      outlined
      severity="danger"
      label="Restore"
      (onClick)="onRestore()"
      icon="pi pi-refresh"
      [disabled]="!backupMetadata">
    </p-button>
  </div>
</div>
