<?php
declare(strict_types=1);
$values = is_array($values ?? null) ? $values : [];
$categories = is_array($categories ?? null) ? $categories : [];
$tags = is_array($tags ?? null) ? $tags : [];
$postingRules = is_array($postingRules ?? null) ? array_replace(\App\Community\PostingPolicyService::defaults(), $postingRules) : \App\Community\PostingPolicyService::defaults();
$titleMin = max(0, (int) ($postingRules['question_title_min'] ?? 10)); $titleMax = max(0, (int) ($postingRules['question_title_max'] ?? 180));
$bodyMin = max(0, (int) ($postingRules['question_body_min'] ?? 20)); $bodyMax = max(0, (int) ($postingRules['question_body_max'] ?? 20000));
$tagMin = max(0, (int) ($postingRules['tags_min'] ?? 0)); $tagMax = max(1, (int) ($postingRules['tags_max'] ?? 5));
?>
<section class="content-section form-section">
    <div class="container narrow-container">
        <p class="eyebrow"><?= quaeriva_t($translator, 'questions.ask') ?></p>
        <h1><?= quaeriva_t($translator, 'questions.ask') ?></h1>
        <p class="section-lede"><?= quaeriva_t($translator, 'questions.ask_intro') ?></p>
        <?php if (($error ?? '') !== ''): ?><div class="notice notice-error" role="alert"><?= quaeriva_escape($error) ?></div><?php endif; ?>
        <?php if (($currentUser ?? null) === null): ?>
            <div class="notice notice-muted"><?= quaeriva_t($translator, 'questions.login_to_ask') ?> <a class="text-link" href="<?= quaeriva_escape(quaeriva_url('login', ['redirect' => quaeriva_url('ask')])) ?>"><?= quaeriva_t($translator, 'auth.login') ?></a></div>
        <?php endif; ?>
        <form class="question-form" method="post" action="<?= quaeriva_escape(quaeriva_url('ask')) ?>" aria-describedby="question-form-note">
            <input type="hidden" name="_csrf" value="<?= quaeriva_escape($csrfToken ?? '') ?>">
            <?= quaeriva_bot_guard_fields() ?>
            <p class="form-hint"><?= quaeriva_t($translator, 'form.required_hint') ?></p>
            <p class="notice notice-muted" id="question-form-note"><?= quaeriva_t($translator, 'questions.moderation_notice') ?></p>
            <div class="field-group">
                <label for="question-title"><?= quaeriva_t($translator, 'questions.title') ?> <span aria-hidden="true">*</span></label>
                <input id="question-title" name="title" type="text" dir="auto" required<?= $titleMin > 0 ? ' minlength="' . $titleMin . '"' : '' ?><?= $titleMax > 0 ? ' maxlength="' . $titleMax . '"' : '' ?> placeholder="<?= quaeriva_t($translator, 'questions.title_placeholder') ?>" value="<?= quaeriva_escape($values['title'] ?? '') ?>"<?= (($site['duplicate_suggestions_enabled'] ?? true) === true) ? ' data-suggest-url="' . quaeriva_escape(quaeriva_url('suggest/questions')) . '"' : '' ?>>
                <?php if (($site['duplicate_suggestions_enabled'] ?? true) === true): ?>
                    <div class="duplicate-suggestions" id="duplicate-suggestions" hidden aria-live="polite">
                        <strong><?= quaeriva_t($translator, 'questions.similar_questions') ?></strong>
                        <p><?= quaeriva_t($translator, 'questions.similar_questions_hint') ?></p>
                        <ul></ul>
                    </div>
                <?php endif; ?>
            </div>
            <div class="field-group">
                <label for="question-body"><?= quaeriva_t($translator, 'questions.body') ?> <span aria-hidden="true">*</span></label>
                <textarea id="question-body" name="body" dir="auto" required rows="8"<?= quaeriva_rich_editor_attributes($translator, 'full', (string) ($values['body'] ?? '')) ?><?= $bodyMin > 0 ? ' minlength="' . $bodyMin . '"' : '' ?><?= $bodyMax > 0 ? ' maxlength="' . $bodyMax . '"' : '' ?> placeholder="<?= quaeriva_t($translator, 'questions.body_placeholder') ?>"><?= quaeriva_escape($values['body'] ?? '') ?></textarea>
            </div>
            <?php
            // A one-option language select is noise: when the site has a single
            // enabled language the choice is already made, so the field is
            // replaced by a hidden input and the row collapses to one column.
            $askLanguages = [];
            foreach ((array) ($site['languages'] ?? []) as $language) {
                $code = (string) ($language['code'] ?? '');
                if ($code === '') continue;
                $askLanguages[] = $language;
            }
            $askShowLanguage = count($askLanguages) > 1;
            $askOnlyLocale = $askShowLanguage ? '' : (string) ($askLanguages[0]['code'] ?? $translator->locale());
            ?>
            <div class="form-grid-2">
                <div class="field-group">
                    <label for="question-category"><?= quaeriva_t($translator, 'questions.category') ?></label>
                    <select id="question-category" name="category_id">
                        <option value="0"><?= quaeriva_t($translator, 'questions.no_category') ?></option>
                        <?php foreach ($categories as $category): $id=(int)($category['id']??0); if($id<=0) continue; $depth=max(0,(int)($category['depth']??0)); ?>
                            <option value="<?= $id ?>" data-category-depth="<?= $depth ?>" data-content-locale="<?= quaeriva_escape((string)($category['content_locale'] ?? '')) ?>"<?= (int)($values['category_id'] ?? 0) === $id ? ' selected' : '' ?>><?= quaeriva_escape(str_repeat('— ', $depth) . (string)($category['name'] ?? '')) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <?php if ($askShowLanguage): ?>
                <div class="field-group">
                    <label for="question-language"><?= quaeriva_t($translator, 'questions.content_language') ?></label>
                    <select id="question-language" name="content_locale">
                        <?php foreach ($askLanguages as $language): $code = (string) ($language['code'] ?? ''); ?>
                            <option value="<?= quaeriva_escape($code) ?>"<?= (($values['content_locale'] ?? $translator->locale()) === $code) ? ' selected' : '' ?>><?= quaeriva_escape((string) ($language['native_name'] ?? $language['name'] ?? $code)) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <?php else: ?>
                <input type="hidden" name="content_locale" value="<?= quaeriva_escape($askOnlyLocale) ?>">
                <?php endif; ?>
            </div>
            <div class="field-group">
                <label for="question-tag-search"><?= quaeriva_t($translator, 'questions.tags') ?></label>
                <div class="tag-picker" data-tag-picker data-max-tags="<?= $tagMax ?>" data-tag-create-url="<?= quaeriva_escape(quaeriva_url('tags/create')) ?>" data-csrf-token="<?= quaeriva_escape($csrfToken ?? '') ?>" data-create-failed="<?= quaeriva_escape(quaeriva_t($translator, 'questions.tags_create_failed')) ?>">
                    <div class="tag-picker-chips" data-tag-chips aria-live="polite"></div>
                    <input id="question-tag-search" class="tag-picker-search" type="search" autocomplete="off" placeholder="<?= quaeriva_t($translator, 'questions.tags_search_placeholder') ?>" data-tag-search>
                    <div class="tag-picker-options" data-tag-options hidden></div>
                    <select id="question-tags" class="tag-picker-source" name="tag_ids[]" multiple size="<?= min(6, max(3, count($tags))) ?>" aria-describedby="question-tags-hint">
                        <?php $selectedTags=array_map('intval',(array)($values['tag_ids']??[])); foreach($tags as $tag): $tagId=(int)($tag['id']??0); if($tagId<=0)continue; ?>
                        <option value="<?= $tagId ?>" data-content-locale="<?= quaeriva_escape((string)($tag['content_locale'] ?? '')) ?>"<?= in_array($tagId,$selectedTags,true)?' selected':'' ?>><?= quaeriva_escape((string)($tag['name']??'')) ?></option>
                        <?php endforeach; ?>
                    </select>
                    <p class="form-hint tag-picker-status" data-tag-status></p>
                </div>
                <p class="form-hint" id="question-tags-hint"><?= quaeriva_t($translator, 'questions.tags_hint') ?> <span><bdi><?= $tagMin ?></bdi>–<bdi><?= $tagMax ?></bdi></span></p>
            </div>
            <label class="check-row"><input type="checkbox" name="is_anonymous" value="1"<?= !empty($values['is_anonymous']) ? ' checked' : '' ?>> <span><?= quaeriva_t($translator, 'questions.ask_anonymously') ?></span></label>
            <button class="button" type="submit"<?= ($currentUser ?? null) === null ? ' disabled' : '' ?>><?= quaeriva_t($translator, 'common.submit') ?></button>
        </form>
    </div>
</section>
