<?php

declare(strict_types=1);

final class MyronAssetUploadView
{
    /**
     * @param array<string, mixed> $options
     */
    public static function renderCompact(array $options): string
    {
        $id = (string) ($options['id'] ?? 'asset-upload');
        $actionValue = (string) ($options['actionValue'] ?? 'upload-file');
        $csrfToken = (string) ($options['csrfToken'] ?? '');
        $assetClass = (string) ($options['assetClass'] ?? 'image');
        $titleLabel = (string) ($options['titleLabel'] ?? 'File Title');
        $fileLabel = (string) ($options['fileLabel'] ?? 'Add File');
        $submitLabel = (string) ($options['submitLabel'] ?? 'Upload Asset');
        $accept = (string) ($options['accept'] ?? '');
        $policyHint = (string) ($options['policyHint'] ?? '');
        $includeQuality = (bool) ($options['includeQuality'] ?? false);
        $includeCaptionUpload = (bool) ($options['includeCaptionUpload'] ?? false);
        $captionFieldName = (string) ($options['captionFieldName'] ?? 'caption_file');
        $captionFieldLabel = (string) ($options['captionFieldLabel'] ?? 'Caption File');
        $captionAccept = (string) ($options['captionAccept'] ?? '.vtt,.srt,.txt');
        $captionPolicyHint = (string) ($options['captionPolicyHint'] ?? '');
        $descriptionRows = max(2, (int) ($options['descriptionRows'] ?? 2));
        $captionRows = max(2, (int) ($options['captionRows'] ?? 2));

        $html = [];
        $html[] = '<form id="' . myronEscape($id) . '" method="post" enctype="multipart/form-data" class="mc-admin-form mc-admin-form--wide mc-asset-upload mc-asset-upload--compact" data-myron-asset-upload="' . myronEscape($assetClass) . '">';
        $html[] = '  <input type="hidden" name="csrf_token" value="' . myronEscape($csrfToken) . '">';
        $html[] = '  <input type="hidden" name="action" value="' . myronEscape($actionValue) . '">';
        $html[] = '  <input type="hidden" name="asset_class" value="' . myronEscape($assetClass) . '">';
        $html[] = '  <section class="mc-admin-form__section mc-asset-upload__section" aria-label="' . myronEscape($fileLabel) . '">';
        $html[] = '    <div class="mc-admin-form__section-header">';
        $html[] = '      <h3 class="mc-admin-form__section-title">' . myronEscape($fileLabel) . '</h3>';
        $html[] = '      <p class="mc-admin-form__section-description">Choose the source file before adding reusable metadata.</p>';
        $html[] = '    </div>';
        $html[] = '    <div class="mc-admin-form__grid">';
        $html[] = '      <label class="sg-field">';
        $html[] = '        <span class="sg-field__label">File</span>';
        $html[] = '        <input class="sg-field__control" type="file" name="upload_file" required' . ($accept !== '' ? ' accept="' . myronEscape($accept) . '"' : '') . '>';
        if ($policyHint !== '') {
            $html[] = '        <span class="sg-field__hint">' . myronEscape($policyHint) . '</span>';
        }
        $html[] = '      </label>';

        if ($includeQuality) {
            $html[] = '      <label class="sg-field">';
            $html[] = '        <span class="sg-field__label">Initial Rendition</span>';
            $html[] = '        <select class="sg-field__control" name="quality_label">';
            foreach (['1080p', '720p', '480p'] as $quality) {
                $html[] = '          <option value="' . myronEscape($quality) . '">' . myronEscape($quality) . '</option>';
            }
            $html[] = '        </select>';
            $html[] = '      </label>';
        }

        if ($includeCaptionUpload) {
            $html[] = '      <label class="sg-field">';
            $html[] = '        <span class="sg-field__label">' . myronEscape($captionFieldLabel) . '</span>';
            $html[] = '        <input class="sg-field__control" type="file" name="' . myronEscape($captionFieldName) . '"' . ($captionAccept !== '' ? ' accept="' . myronEscape($captionAccept) . '"' : '') . '>';
            $html[] = '        <span class="sg-field__hint">' . myronEscape($captionPolicyHint !== '' ? $captionPolicyHint : 'Optional caption or transcript file.') . '</span>';
            $html[] = '      </label>';
        }

        $html[] = '    </div>';
        $html[] = '  </section>';
        $html[] = '  <section class="mc-admin-form__section mc-asset-upload__section" aria-label="Asset metadata">';
        $html[] = '    <div class="mc-admin-form__section-header">';
        $html[] = '      <h3 class="mc-admin-form__section-title">Metadata</h3>';
        $html[] = '      <p class="mc-admin-form__section-description">Name the asset and add accessible text for public render surfaces.</p>';
        $html[] = '    </div>';
        $html[] = '    <div class="mc-admin-form__grid mc-admin-form__grid--three">';
        $html[] = '      <label class="sg-field">';
        $html[] = '        <span class="sg-field__label">' . myronEscape($titleLabel) . '</span>';
        $html[] = '        <input class="sg-field__control" type="text" name="title" required maxlength="255">';
        $html[] = '      </label>';
        $html[] = self::renderMetadataFields([
            'descriptionRows' => $descriptionRows,
            'captionRows' => $captionRows,
            'fieldClass' => 'sg-field',
            'fullFieldClass' => 'sg-field mc-admin-form__span-full',
        ]);

        $html[] = '    </div>';
        $html[] = '  </section>';
        $html[] = '  <div class="mc-admin-form__actions mc-asset-upload__actions">';
        $html[] = '    <button class="sg-button sg-button--primary" type="submit">' . myronEscape($submitLabel) . '</button>';
        $html[] = '  </div>';
        $html[] = '</form>';

        return implode("\n", $html);
    }

    /**
     * @param array<string, mixed> $options
     */
    public static function renderMetadataFields(array $options = []): string
    {
        $disabled = (bool) ($options['disabled'] ?? false);
        $descriptionRows = max(2, (int) ($options['descriptionRows'] ?? 2));
        $captionRows = max(2, (int) ($options['captionRows'] ?? 2));
        $fieldClass = (string) ($options['fieldClass'] ?? 'sg-field');
        $fullFieldClass = (string) ($options['fullFieldClass'] ?? $fieldClass);
        $disabledAttribute = $disabled ? ' disabled' : '';

        $html = [];
        $html[] = '  <label class="' . myronEscape($fieldClass) . '">';
        $html[] = '    <span class="sg-field__label">Alt Text</span>';
        $html[] = '    <input class="sg-field__control" type="text" name="alt_text" required maxlength="500"' . $disabledAttribute . '>';
        $html[] = '  </label>';
        $html[] = '  <label class="' . myronEscape($fullFieldClass) . '">';
        $html[] = '    <span class="sg-field__label">Description</span>';
        $html[] = '    <textarea class="sg-field__control sg-field__control--textarea" name="description" required rows="' . myronEscape((string) $descriptionRows) . '"' . $disabledAttribute . '></textarea>';
        $html[] = '  </label>';
        $html[] = '  <label class="' . myronEscape($fullFieldClass) . '">';
        $html[] = '    <span class="sg-field__label">Caption</span>';
        $html[] = '    <textarea class="sg-field__control sg-field__control--textarea" name="caption" required rows="' . myronEscape((string) $captionRows) . '"' . $disabledAttribute . '></textarea>';
        $html[] = '  </label>';

        return implode("\n", $html);
    }
}
