<?php

use humhub\helpers\Html;
use humhub\widgets\bootstrap\Button;
use humhub\widgets\form\ActiveForm;
use yii\web\View;

/** @var $this View */
/** @var $model \humhub\modules\cgyshare\models\ConfigureForm */
/** @var $spaceOptions array */

$module = $this->context->module;
$this->title = 'CGY-Share Settings';
$adminCssUrl = $module->getPublishedUrl('/css/admin.css');
$adminJsUrl = $module->getPublishedUrl('/js/admin.js');
$iconUrl = $module->getPublishedUrl('/icon-128.png') ?: $module->getImage();

if ($adminCssUrl) {
    $this->registerCssFile($adminCssUrl);
}

if ($adminJsUrl) {
    $this->registerJsFile($adminJsUrl, ['position' => View::POS_END]);
}

$selectedSpace = $model->targetSpaceGuid && isset($spaceOptions[$model->targetSpaceGuid])
    ? $spaceOptions[$model->targetSpaceGuid]
    : 'Not selected';
?>

<div class="cgy-vibez" data-cgy-tabs data-cgy-default-tab="automation">
    <section class="cgy-vibez__hero">
        <div class="cgy-vibez__orb cgy-vibez__orb--one"></div>
        <div class="cgy-vibez__orb cgy-vibez__orb--two"></div>

        <div class="cgy-vibez__brand">
            <img src="<?= Html::encode($iconUrl) ?>"
                 alt="CGY-Share"
                 class="cgy-vibez__logo">
            <div>
                <span class="cgy-vibez__eyebrow">CGY SHARE DASHBOARD</span>
                <h1>CGY-Share</h1>
                <p>A focused publishing pipeline from member profiles to your Main Space.</p>
            </div>
        </div>

        <div class="cgy-vibez__release">
            <span class="cgy-vibez__version">v19.69.06</span>
            <small>Designed &amp; developed by <strong>YCP</strong></small>
        </div>
    </section>

    <div class="cgy-vibez__status-grid">
        <article class="cgy-status-card <?= $model->autoShareEnabled ? 'is-live' : 'is-muted' ?>">
            <span class="cgy-status-card__icon">⚡</span>
            <div>
                <small>Automation</small>
                <strong><?= $model->autoShareEnabled ? 'Live' : 'Paused' ?></strong>
            </div>
        </article>

        <article class="cgy-status-card <?= $model->publicOnly ? 'is-live' : 'is-warning' ?>">
            <span class="cgy-status-card__icon">◎</span>
            <div>
                <small>Visibility rule</small>
                <strong><?= $model->publicOnly ? 'Public only' : 'All posts' ?></strong>
            </div>
        </article>

        <article class="cgy-status-card <?= $model->targetSpaceGuid ? 'is-live' : 'is-warning' ?>">
            <span class="cgy-status-card__icon">◆</span>
            <div>
                <small>Main Space</small>
                <strong><?= Html::encode($selectedSpace) ?></strong>
            </div>
        </article>
    </div>

    <div class="cgy-vibez__workspace">
        <nav class="cgy-tabs" aria-label="CGY-Share settings">
            <button type="button"
                    class="cgy-tab is-active"
                    data-cgy-tab-target="automation"
                    aria-selected="true">
                <span>⚡</span> Automation
            </button>
            <button type="button"
                    class="cgy-tab"
                    data-cgy-tab-target="guide"
                    aria-selected="false">
                <span>✦</span> How It Works
            </button>
        </nav>

        <?php $form = ActiveForm::begin(['options' => ['class' => 'cgy-vibez__form']]); ?>

        <section class="cgy-tab-panel is-active" data-cgy-tab-panel="automation">
            <div class="cgy-section-heading">
                <span class="cgy-section-heading__icon">⚡</span>
                <div>
                    <h2>Automatic sharing</h2>
                    <p>Choose where profile posts land and which posts qualify.</p>
                </div>
            </div>

            <div class="cgy-field-card">
                <?= $form->field($model, 'targetSpaceGuid')->dropDownList(
                    $spaceOptions,
                    ['prompt' => 'Choose your Main Space']
                ) ?>
                <p class="cgy-field-help">New eligible profile posts are shared into this Space using HumHub’s native Share Content service.</p>
            </div>

            <div class="cgy-toggle-grid">
                <div class="cgy-field-card cgy-field-card--toggle">
                    <?= $form->field($model, 'autoShareEnabled')->checkbox() ?>
                    <p class="cgy-field-help">Turn the complete automation on or off without disabling the module.</p>
                </div>

                <div class="cgy-field-card cgy-field-card--toggle">
                    <?= $form->field($model, 'publicOnly')->checkbox() ?>
                    <p class="cgy-field-help">Recommended. Private profile content remains private and is never shared.</p>
                </div>
            </div>

            <div class="cgy-callout">
                <span class="cgy-callout__icon">i</span>
                <div>
                    <strong>Share Content is required</strong>
                    <p>Keep HumHub’s official <em>Share content</em> module installed and enabled.</p>
                </div>
            </div>
        </section>

        <section class="cgy-tab-panel" data-cgy-tab-panel="guide" hidden>
            <div class="cgy-section-heading">
                <span class="cgy-section-heading__icon">✦</span>
                <div>
                    <h2>How CGY-Share works</h2>
                    <p>The complete path from a profile post to the Main Space.</p>
                </div>
            </div>

            <div class="cgy-field-card cgy-guide-step">
                <span>1</span>
                <div>
                    <strong>A member publishes on their profile</strong>
                    <p class="cgy-field-help">The post must be newly created after CGY-Share is enabled.</p>
                </div>
            </div>

            <div class="cgy-field-card cgy-guide-step">
                <span>2</span>
                <div>
                    <strong>CGY-Share checks eligibility</strong>
                    <p class="cgy-field-help">Publication state, visibility, destination, permissions, and duplicate status are verified.</p>
                </div>
            </div>

            <div class="cgy-field-card cgy-guide-step">
                <span>3</span>
                <div>
                    <strong>HumHub shares it into the Main Space</strong>
                    <p class="cgy-field-help">The native Share Content service keeps the original profile post as the source.</p>
                </div>
            </div>

            <div class="cgy-guide-results">
                <article>
                    <span class="cgy-guide-results__badge">SHARED</span>
                    <h3>Eligible content</h3>
                    <p>New, published profile posts that meet visibility and permission rules.</p>
                </article>
                <article>
                    <span class="cgy-guide-results__badge cgy-guide-results__badge--muted">SKIPPED</span>
                    <h3>Protected content</h3>
                    <p>Private, unpublished, duplicate, ineligible, or Space-originated posts.</p>
                </article>
                <article>
                    <span class="cgy-guide-results__badge">PRESERVED</span>
                    <h3>Original source</h3>
                    <p>The profile post stays in place; HumHub creates a native share reference.</p>
                </article>
            </div>

            <div class="cgy-callout">
                <span class="cgy-callout__icon">!</span>
                <div>
                    <strong>Permissions still apply</strong>
                    <p>The author must be allowed to share into the selected Main Space.</p>
                </div>
            </div>
        </section>

        <div class="cgy-actions">
            <div class="cgy-actions__note">
                <span class="cgy-actions__pulse"></span>
                Settings apply to newly created posts
            </div>
            <?= Button::save('Save CGY-Share settings')->submit() ?>
        </div>

        <?php ActiveForm::end(); ?>
    </div>

    <footer class="cgy-vibez__footer">
        <span>CGY-Share 19.69.06</span>
        <span>Blue Saucer Dashboard &middot; Developer <strong>YCP</strong></span>
    </footer>
</div>
