<?php

declare(strict_types=1);

require_once dirname(__DIR__, 3) . '/system/bootstrap.php';
require_once dirname(__DIR__, 3) . '/modules/files/AssetReplacementView.php';

$repository = myronFileRepository();
$videoAssets = myronVideoAssetRepository();
$mediaAccessibilityAttachments = myronMediaAccessibilityAttachmentRepository();
$documentTemplates = myronDocumentTemplateRepository();
$csrfToken = myronCsrfToken();
$requestPath = myronResolveAppRequestPath((string) ($_SERVER['REQUEST_URI'] ?? myronBuildAdminUrl('content.imageViewer')));
$mode = str_ends_with($requestPath, '/videos') ? 'video' : (str_ends_with($requestPath, '/audio') ? 'audio' : (str_ends_with($requestPath, '/documents') ? 'document' : 'image'));
$modeMap = [
    'image' => [
        'title' => 'Image Viewer',
        'activeItem' => 'content-image-viewer',
        'route' => 'content.imageViewer',
    ],
    'video' => [
        'title' => 'Video Viewer',
        'activeItem' => 'content-video-viewer',
        'route' => 'content.videoViewer',
    ],
    'audio' => [
        'title' => 'Audio Viewer',
        'activeItem' => 'content-audio-viewer',
        'route' => 'content.audioViewer',
    ],
    'document' => [
        'title' => 'Document Manager',
        'activeItem' => 'content-document-manager',
        'route' => 'content.documentManager',
    ],
];
$config = $modeMap[$mode];
$currentRoute = (string) $config['route'];

if (($_SERVER['REQUEST_METHOD'] ?? 'GET') === 'POST') {
    $currentUser = myronRequireContentAccess(true);
    try {
        myronRequireCsrf($_POST['csrf_token'] ?? null);
        $action = trim((string) ($_POST['action'] ?? ''));

        if ($action === 'upload-document' && $mode === 'document') {
            $file = $repository->uploadFile(
                $_FILES['upload_file'] ?? [],
                (string) $currentUser['id'],
                array_merge($_POST, ['asset_class' => 'document']),
                true
            );
            myronRedirectWithFlash(
                myronBuildAdminUrl('content.documentManager', ['tab' => 'upload', 'preview' => (string) $file['id']]),
                'Document uploaded.',
                'success'
            );
        }

        if ($action === 'replace-file') {
            if (!myronUserCanManageContent($currentUser)) {
                throw new RuntimeException('You do not have permission to replace this asset.');
            }

            $fileId = trim((string) ($_POST['file_id'] ?? ''));
            myronFilesActionService()->replaceFromMultipart(
                $fileId,
                $_FILES['replacement_file'] ?? [],
                $_POST,
                myronActionCallerContextForUser($currentUser)
            );

            $videoAssetId = trim((string) ($_POST['video_asset_id'] ?? ''));
            if ($mode === 'video' && $videoAssetId !== '') {
                $query = ['preview_video' => $videoAssetId];
            } elseif ($mode === 'document') {
                $query = ['tab' => 'upload', 'preview' => $fileId];
            } else {
                $query = ['preview' => $fileId];
            }

            myronRedirectWithFlash(
                myronBuildAdminUrl($currentRoute, $query),
                ucfirst($mode) . ' asset replaced. Existing references now use the new asset bytes.',
                'success'
            );
        }

        throw new RuntimeException('Unknown asset manager action.');
    } catch (Throwable $exception) {
        $query = [];
        $previewId = trim((string) ($_POST['file_id'] ?? $_GET['preview'] ?? ''));
        $previewVideoId = trim((string) ($_POST['video_asset_id'] ?? $_GET['preview_video'] ?? ''));
        if ($mode === 'video' && $previewVideoId !== '') {
            $query['preview_video'] = $previewVideoId;
        } elseif ($previewId !== '') {
            if ($mode === 'document') {
                $query['tab'] = 'upload';
            }
            $query['preview'] = $previewId;
        } elseif ($mode === 'document') {
            $query['tab'] = 'upload';
        }
        myronRedirectWithFlash(myronBuildAdminUrl($currentRoute, $query), $exception->getMessage(), 'error');
    }
}

$currentUser = myronRequireContentAccess();
$manageId = trim((string) ($_GET['preview'] ?? ''));
$manageVideoId = trim((string) ($_GET['preview_video'] ?? ''));
$activeTab = $mode === 'document' && (string) ($_GET['tab'] ?? '') === 'upload' ? 'upload' : 'templates';
$files = $mode === 'video' ? [] : $repository->listFilesByMediaClass($mode);
$videos = $mode === 'video' ? $videoAssets->listVideoAssets() : [];
$standaloneVideos = $mode === 'video' ? $repository->listFilesByMediaClass('video') : [];
$availableTemplates = $mode === 'document' ? $documentTemplates->listActiveTemplates() : [];
$documentUploadPolicy = $mode === 'document' ? $repository->uploadPolicyForMediaClass('document') : [];
$previewFile = $manageId !== '' ? $repository->getFileById($manageId) : null;
$previewAudioTranscript = is_array($previewFile) && ($previewFile['is_audio'] ?? false) === true
    ? $mediaAccessibilityAttachments->getAttachment('audio_file', (string) ($previewFile['id'] ?? ''), 'transcript')
    : null;
$previewVideo = $mode === 'video' && $manageVideoId !== '' ? $videoAssets->getVideoAssetById($manageVideoId) : null;
$canManageContent = myronUserCanManageContent($currentUser);

myronRenderAdminShellStart($currentUser, [
    'title' => (string) $config['title'],
    'titleContext' => 'Asset Manager',
    'activeItem' => (string) $config['activeItem'],
    'headScripts' => [myronBuildAssetUrl('runtime/js/files.js'), myronBuildAssetUrl('runtime/js/media-player.js')],
]);
?>
<div class="mc-workspace__header">
  <?php myronRenderAdminBreadcrumbs([
      ['label' => 'Content', 'href' => myronBuildAdminUrl('content.pages')],
      ['label' => 'Asset Manager', 'href' => myronBuildAdminUrl('content.imageViewer')],
      ['label' => (string) $config['title']],
  ]); ?>
  <div class="mc-workspace__title-row">
    <h1 class="mc-workspace__title"><?= myronEscape((string) $config['title']) ?></h1>
  </div>
  <p class="mc-workspace__subtitle">Preview uploaded assets and copy neutral public URLs for content workflows.</p>
</div>

<div class="mc-page-canvas mc-page-canvas--site">
  <div class="sg-main">
    <div class="mc-files-layout<?= (is_array($previewFile) || is_array($previewVideo)) ? ' mc-files-layout--sheet-open' : '' ?>">
      <section class="sg-section" data-myron-content-asset-manager="<?= myronEscape($mode) ?>">
        <div class="sg-section__header">
          <h2 class="sg-section__title"><?= myronEscape((string) $config['title']) ?></h2>
          <p class="sg-section__subtitle"><?= myronEscape((string) ($mode === 'video' ? count($videos) + count($standaloneVideos) : count($files))) ?> matching assets</p>
        </div>

        <?php if ($mode === 'video'): ?>
        <div class="mc-page-grid mc-page-grid--files" role="table" aria-label="Video Viewer">
          <div class="mc-page-grid__row mc-page-grid__row--head" role="row">
            <span role="columnheader">Video</span>
            <span role="columnheader">Renditions</span>
            <span role="columnheader">Updated</span>
            <span role="columnheader">Actions</span>
          </div>
          <?php if ($videos === [] && $standaloneVideos === []): ?>
          <div class="mc-page-grid__row" role="row">
            <span class="mc-page-grid__cell" role="cell">No matching assets have been uploaded yet.</span>
            <span class="mc-page-grid__cell" role="cell">-</span>
            <span class="mc-page-grid__cell" role="cell">-</span>
            <span class="mc-page-grid__cell" role="cell">-</span>
          </div>
          <?php endif; ?>
          <?php foreach ($videos as $video): ?>
          <?php $primary = is_array($video['primary_rendition'] ?? null) ? $video['primary_rendition'] : null; ?>
          <div class="mc-page-grid__row" role="row">
            <span class="mc-page-grid__cell mc-page-grid__cell--name" role="cell">
              <span class="mc-page-grid__name"><?= myronEscape((string) ($video['title'] ?? 'Video asset')) ?></span>
              <span class="mc-page-grid__subtle"><?= myronEscape((string) ($video['description'] ?? '')) ?></span>
            </span>
            <span class="mc-page-grid__cell" role="cell"><?= myronEscape((string) ($video['rendition_count'] ?? 0)) ?></span>
            <span class="mc-page-grid__cell" role="cell"><?= myronEscape(myronFormatAdminTimestamp((string) ($video['updated_at'] ?? ''))) ?></span>
            <span class="mc-page-grid__cell mc-page-grid__cell--actions" role="cell">
              <a class="sg-button sg-button--secondary" href="<?= myronEscape(myronBuildAdminUrl('content.videoViewer', ['preview_video' => (string) $video['id']])) ?>">Preview</a>
            </span>
          </div>
          <?php endforeach; ?>
          <?php foreach ($standaloneVideos as $file): ?>
          <div class="mc-page-grid__row" role="row">
            <span class="mc-page-grid__cell mc-page-grid__cell--name" role="cell">
              <span class="mc-page-grid__name"><?= myronEscape((string) ($file['original_filename'] ?? 'Video file')) ?></span>
              <span class="mc-page-grid__subtle">Standalone video file</span>
            </span>
            <span class="mc-page-grid__cell" role="cell">1</span>
            <span class="mc-page-grid__cell" role="cell"><?= myronEscape(myronFormatAdminTimestamp((string) ($file['created_at'] ?? ''))) ?></span>
            <span class="mc-page-grid__cell mc-page-grid__cell--actions" role="cell">
              <a class="sg-button sg-button--secondary" href="<?= myronEscape(myronBuildAdminUrl('content.videoViewer', ['preview' => (string) $file['id']])) ?>">Preview</a>
            </span>
          </div>
          <?php endforeach; ?>
        </div>
        <?php elseif ($mode === 'document'): ?>
        <nav class="mc-workspace__tabs mc-asset-manager-tabs" aria-label="Document Manager sections">
          <a class="mc-workspace__tab<?= $activeTab === 'templates' ? ' mc-workspace__tab--active' : '' ?>" href="<?= myronEscape(myronBuildAdminUrl('content.documentManager', ['tab' => 'templates'])) ?>">View Templates</a>
          <a class="mc-workspace__tab<?= $activeTab === 'upload' ? ' mc-workspace__tab--active' : '' ?>" href="<?= myronEscape(myronBuildAdminUrl('content.documentManager', ['tab' => 'upload'])) ?>">Upload Document</a>
        </nav>

        <?php if ($activeTab === 'templates'): ?>
        <div class="mc-page-grid mc-page-grid--files" role="table" aria-label="Document Templates">
          <div class="mc-page-grid__row mc-page-grid__row--head" role="row">
            <span role="columnheader">Template</span>
            <span role="columnheader">Type</span>
            <span role="columnheader">Updated</span>
            <span role="columnheader">Size</span>
            <span role="columnheader">Actions</span>
          </div>
          <?php if ($availableTemplates === []): ?>
          <div class="mc-page-grid__row" role="row">
            <span class="mc-page-grid__cell" role="cell">No document templates are available yet.</span>
            <span class="mc-page-grid__cell" role="cell">-</span>
            <span class="mc-page-grid__cell" role="cell">-</span>
            <span class="mc-page-grid__cell" role="cell">-</span>
            <span class="mc-page-grid__cell" role="cell">-</span>
          </div>
          <?php endif; ?>
          <?php foreach ($availableTemplates as $template): ?>
          <div class="mc-page-grid__row" role="row">
            <span class="mc-page-grid__cell mc-page-grid__cell--name" role="cell">
              <span class="mc-page-grid__name"><?= myronEscape((string) ($template['title'] ?? 'Document template')) ?></span>
              <span class="mc-page-grid__subtle"><?= myronEscape((string) ($template['description'] ?? '')) ?></span>
            </span>
            <span class="mc-page-grid__cell" role="cell"><?= myronEscape(strtoupper((string) ($template['file_extension'] ?? ''))) ?></span>
            <span class="mc-page-grid__cell" role="cell"><?= myronEscape(myronFormatAdminTimestamp((string) ($template['updated_at'] ?? ''))) ?></span>
            <span class="mc-page-grid__cell" role="cell"><?= myronEscape((string) ($template['file_size_label'] ?? '')) ?></span>
            <span class="mc-page-grid__cell mc-page-grid__cell--actions" role="cell">
              <button class="sg-button" type="button" data-mc-copy-url="<?= myronEscape((string) ($template['file_url'] ?? '')) ?>">Copy URL</button>
              <a class="sg-button sg-button--secondary" href="<?= myronEscape((string) ($template['download_url'] ?? '')) ?>">Download</a>
            </span>
          </div>
          <?php endforeach; ?>
        </div>
        <?php else: ?>
        <?php if ($canManageContent): ?>
        <?= MyronAssetUploadView::renderCompact([
            'id' => 'document-upload',
            'csrfToken' => $csrfToken,
            'actionValue' => 'upload-document',
            'assetClass' => 'document',
            'titleLabel' => 'Document Title',
            'fileLabel' => 'Add Document',
            'submitLabel' => 'Upload Document',
            'accept' => $repository->acceptAttributeForMediaClass('document'),
            'policyHint' => 'Maximum upload size on this server: ' . (string) ($documentUploadPolicy['effectiveLabel'] ?? '') . '. MyronCMS document policy limit: ' . (string) ($documentUploadPolicy['policyLabel'] ?? '') . '.',
        ]) ?>
        <?php endif; ?>
        <div class="mc-page-grid mc-page-grid--files" role="table" aria-label="<?= myronEscape((string) $config['title']) ?>">
          <div class="mc-page-grid__row mc-page-grid__row--head" role="row">
            <span role="columnheader">Filename</span>
            <span role="columnheader">Type</span>
            <span role="columnheader">Uploaded</span>
            <span role="columnheader">Size</span>
            <span role="columnheader">Actions</span>
          </div>
          <?php if ($files === []): ?>
          <div class="mc-page-grid__row" role="row">
            <span class="mc-page-grid__cell" role="cell">No matching assets have been uploaded yet.</span>
            <span class="mc-page-grid__cell" role="cell">-</span>
            <span class="mc-page-grid__cell" role="cell">-</span>
            <span class="mc-page-grid__cell" role="cell">-</span>
            <span class="mc-page-grid__cell" role="cell">-</span>
          </div>
          <?php endif; ?>
          <?php foreach ($files as $file): ?>
          <div class="mc-page-grid__row" role="row">
            <span class="mc-page-grid__cell mc-page-grid__cell--name" role="cell">
              <span class="mc-page-grid__name"><?= myronEscape((string) ($file['original_filename'] ?? 'asset')) ?></span>
              <span class="mc-page-grid__subtle"><?= myronEscape((string) ($file['description'] ?? '')) ?></span>
            </span>
            <span class="mc-page-grid__cell" role="cell"><?= myronEscape(strtoupper((string) ($file['file_extension'] ?? ''))) ?></span>
            <span class="mc-page-grid__cell" role="cell"><?= myronEscape(myronFormatAdminTimestamp((string) ($file['created_at'] ?? ''))) ?></span>
            <span class="mc-page-grid__cell" role="cell"><?= myronEscape((string) ($file['file_size_label'] ?? '')) ?></span>
            <span class="mc-page-grid__cell mc-page-grid__cell--actions" role="cell">
              <a class="sg-button sg-button--secondary" href="<?= myronEscape(myronBuildAdminUrl((string) $config['route'], ['preview' => (string) $file['id']])) ?>">Preview</a>
            </span>
          </div>
          <?php endforeach; ?>
        </div>
        <?php endif; ?>
        <?php else: ?>
        <div class="mc-page-grid mc-page-grid--files" role="table" aria-label="<?= myronEscape((string) $config['title']) ?>">
          <div class="mc-page-grid__row mc-page-grid__row--head" role="row">
            <span role="columnheader">Filename</span>
            <span role="columnheader">Type</span>
            <span role="columnheader">Uploaded</span>
            <span role="columnheader">Size</span>
            <span role="columnheader">Actions</span>
          </div>
          <?php if ($files === []): ?>
          <div class="mc-page-grid__row" role="row">
            <span class="mc-page-grid__cell" role="cell">No matching assets have been uploaded yet.</span>
            <span class="mc-page-grid__cell" role="cell">-</span>
            <span class="mc-page-grid__cell" role="cell">-</span>
            <span class="mc-page-grid__cell" role="cell">-</span>
            <span class="mc-page-grid__cell" role="cell">-</span>
          </div>
          <?php endif; ?>
          <?php foreach ($files as $file): ?>
          <div class="mc-page-grid__row" role="row">
            <span class="mc-page-grid__cell mc-page-grid__cell--name" role="cell">
              <span class="mc-page-grid__name"><?= myronEscape((string) ($file['original_filename'] ?? 'asset')) ?></span>
              <span class="mc-page-grid__subtle"><?= myronEscape((string) ($file['description'] ?? '')) ?></span>
            </span>
            <span class="mc-page-grid__cell" role="cell"><?= myronEscape(strtoupper((string) ($file['file_extension'] ?? ''))) ?></span>
            <span class="mc-page-grid__cell" role="cell"><?= myronEscape(myronFormatAdminTimestamp((string) ($file['created_at'] ?? ''))) ?></span>
            <span class="mc-page-grid__cell" role="cell"><?= myronEscape((string) ($file['file_size_label'] ?? '')) ?></span>
            <span class="mc-page-grid__cell mc-page-grid__cell--actions" role="cell">
              <a class="sg-button sg-button--secondary" href="<?= myronEscape(myronBuildAdminUrl((string) $config['route'], ['preview' => (string) $file['id']])) ?>">Preview</a>
            </span>
          </div>
          <?php endforeach; ?>
        </div>
        <?php endif; ?>
      </section>

      <?php if (is_array($previewFile)): ?>
      <aside class="mc-side-sheet" aria-label="Preview asset">
        <div class="mc-side-sheet__header">
          <div>
            <h2 class="mc-side-sheet__title">Preview Asset</h2>
            <div class="mc-side-sheet__meta">
              <span><?= myronEscape((string) $previewFile['original_filename']) ?></span>
              <span><?= myronEscape((string) $previewFile['mime_type']) ?> · <?= myronEscape((string) $previewFile['file_size_label']) ?></span>
            </div>
          </div>
          <a class="sg-button sg-button--secondary" href="<?= myronEscape(myronBuildAdminUrl((string) $config['route'])) ?>">Close</a>
        </div>
        <div class="mc-side-sheet__preview">
          <?php if (($previewFile['is_image'] ?? false) === true): ?>
          <img class="mc-side-sheet__preview-image" src="<?= myronEscape((string) $previewFile['file_url']) ?>" alt="<?= myronEscape((string) ($previewFile['alt_text'] ?? $previewFile['title'] ?? '')) ?>">
          <?php elseif (($previewFile['is_video'] ?? false) === true): ?>
          <video class="mc-side-sheet__preview-media" controls preload="metadata" src="<?= myronEscape((string) $previewFile['file_url']) ?>"></video>
          <?php elseif (($previewFile['is_audio'] ?? false) === true): ?>
          <audio class="mc-side-sheet__preview-media" controls preload="metadata" src="<?= myronEscape((string) $previewFile['file_url']) ?>"></audio>
          <?php if (is_array($previewAudioTranscript)): ?>
          <p class="sg-field__hint">Transcript available: <a href="<?= myronEscape((string) ($previewAudioTranscript['file_url'] ?? '')) ?>" target="_blank" rel="noreferrer"><?= myronEscape((string) ($previewAudioTranscript['original_filename'] ?? 'Transcript')) ?></a></p>
          <?php endif; ?>
          <?php elseif (($previewFile['is_pdf'] ?? false) === true): ?>
          <iframe class="mc-side-sheet__preview-frame" src="<?= myronEscape((string) $previewFile['file_url']) ?>" title="File preview"></iframe>
          <?php else: ?>
          <div class="mc-side-sheet__preview-generic">Open or download the file to inspect it.</div>
          <?php endif; ?>
        </div>
        <div class="mc-side-sheet__url-row">
          <input class="sg-field__control mc-side-sheet__url-control" type="text" value="<?= myronEscape((string) $previewFile['file_url']) ?>" readonly>
          <button class="sg-button" type="button" data-mc-copy-url="<?= myronEscape((string) $previewFile['file_url']) ?>">Copy URL</button>
        </div>
        <?php if ($canManageContent): ?>
        <?= MyronAssetReplacementView::render([
            'csrfToken' => $csrfToken,
            'fileId' => (string) $previewFile['id'],
            'accept' => $repository->acceptAttributeForMediaClass((string) ($previewFile['media_class'] ?? $mode)),
            'label' => 'Replace ' . ucfirst((string) ($previewFile['media_class'] ?? $mode)),
            'buttonLabel' => 'Replace Asset',
        ]) ?>
        <?php endif; ?>
        <?php if (($previewFile['is_audio'] ?? false) === true && is_array($previewAudioTranscript)): ?>
        <section class="mc-admin-form__section" aria-label="Audio transcript">
          <div class="mc-admin-form__section-header">
            <h3 class="mc-admin-form__section-title">Transcript</h3>
            <p class="mc-admin-form__section-description">Use the transcript file when this audio needs an accessible text alternative.</p>
          </div>
          <div class="mc-side-sheet__url-row">
            <input class="sg-field__control mc-side-sheet__url-control" type="text" value="<?= myronEscape((string) ($previewAudioTranscript['original_filename'] ?? 'Transcript')) ?>" readonly>
            <a class="sg-button sg-button--secondary" href="<?= myronEscape((string) ($previewAudioTranscript['file_url'] ?? '')) ?>" target="_blank" rel="noreferrer">View</a>
            <a class="sg-button sg-button--secondary" href="<?= myronEscape((string) ($previewAudioTranscript['download_url'] ?? '')) ?>">Download</a>
          </div>
        </section>
        <?php endif; ?>
      </aside>
      <?php endif; ?>

      <?php if (is_array($previewVideo)): ?>
      <aside class="mc-side-sheet" aria-label="Preview video">
        <div class="mc-side-sheet__header">
          <div>
            <h2 class="mc-side-sheet__title">Preview Video</h2>
            <div class="mc-side-sheet__meta">
              <span><?= myronEscape((string) ($previewVideo['title'] ?? 'Video asset')) ?></span>
              <span><?= myronEscape((string) ($previewVideo['rendition_count'] ?? 0)) ?> renditions</span>
            </div>
          </div>
          <a class="sg-button sg-button--secondary" href="<?= myronEscape(myronBuildAdminUrl('content.videoViewer')) ?>">Close</a>
        </div>
        <?php $renditions = is_array($previewVideo['renditions'] ?? null) ? $previewVideo['renditions'] : []; ?>
        <?php $previewCaption = is_array($previewVideo['caption_attachment'] ?? null) ? $previewVideo['caption_attachment'] : null; ?>
        <?php if ($renditions !== []): ?>
        <div class="mc-side-sheet__preview">
          <video class="mc-side-sheet__preview-media" controls preload="metadata" data-myron-media-player>
            <?php foreach ($renditions as $rendition): ?>
            <source src="<?= myronEscape((string) ($rendition['file_url'] ?? '')) ?>" type="video/mp4" data-quality="<?= myronEscape((string) ($rendition['quality_label'] ?? '')) ?>">
            <?php endforeach; ?>
            <?php if ((string) ($previewVideo['captions_url'] ?? '') !== ''): ?>
            <track kind="subtitles" src="<?= myronEscape((string) $previewVideo['captions_url']) ?>" srcLang="<?= myronEscape((string) ($previewCaption['language'] ?? 'en')) ?>" label="<?= myronEscape((string) ($previewCaption['label'] ?? 'English')) ?>">
            <?php endif; ?>
          </video>
          <?php if (count($renditions) > 1): ?>
          <div class="mc-media-quality-bar" data-myron-media-quality>
            <?php foreach ($renditions as $rendition): ?>
            <button class="sg-button" type="button" data-quality="<?= myronEscape((string) ($rendition['quality_label'] ?? '')) ?>" data-src="<?= myronEscape((string) ($rendition['file_url'] ?? '')) ?>"><?= myronEscape((string) ($rendition['quality_label'] ?? '')) ?></button>
            <?php endforeach; ?>
          </div>
          <?php endif; ?>
        </div>
        <?php endif; ?>
        <?php foreach ($renditions as $rendition): ?>
        <?php $renditionFileId = (string) ($rendition['file_id'] ?? ''); ?>
        <div class="mc-side-sheet__url-row">
          <input class="sg-field__control mc-side-sheet__url-control" type="text" value="<?= myronEscape((string) ($rendition['file_url'] ?? '')) ?>" readonly>
          <button class="sg-button" type="button" data-mc-copy-url="<?= myronEscape((string) ($rendition['file_url'] ?? '')) ?>">Copy <?= myronEscape((string) ($rendition['quality_label'] ?? 'URL')) ?></button>
        </div>
        <?php if ($canManageContent && $renditionFileId !== ''): ?>
        <?= MyronAssetReplacementView::render([
            'csrfToken' => $csrfToken,
            'fileId' => $renditionFileId,
            'accept' => $repository->acceptAttributeForMediaClass('video'),
            'label' => 'Replace ' . (string) ($rendition['quality_label'] ?? 'Video') . ' Rendition',
            'buttonLabel' => 'Replace Rendition',
            'hidden' => ['video_asset_id' => (string) $previewVideo['id']],
        ]) ?>
        <?php endif; ?>
        <?php endforeach; ?>
      </aside>
      <?php endif; ?>
    </div>
  </div>
</div>
<?php myronRenderAdminShellEnd(); ?>
