{% extends "integrations/wizard_base.html" %}

{% block wizard_steps %}
    <!-- Step 1: Instance Configuration -->
    <div class="wizard-step" data-step="1">
        <h2 class="text-xl font-semibold mb-4">{{ _('Step 1: Instance Configuration') }}</h2>
        <div class="space-y-4">
            {% if current_user.is_admin %}
            <div>
                <label for="gitlab_instance_url" class="block text-sm font-medium mb-2">
                    {{ _('GitLab Instance URL') }}
                </label>
                <input type="url" id="gitlab_instance_url" name="gitlab_instance_url"
                       value="{{ current_config.get('instance_url', 'https://gitlab.com') }}"
                       class="w-full px-3 py-2 border border-border-light dark:border-border-dark rounded-lg bg-card-light dark:bg-card-dark"
                       placeholder="https://gitlab.com">
                <p class="mt-1 text-xs text-text-muted-light dark:text-text-muted-dark">
                    {{ _('URL of your GitLab instance. Use "https://gitlab.com" for GitLab.com or your self-hosted GitLab URL.') }}
                </p>
            </div>
            {% endif %}
            
            <div class="p-4 bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800 rounded-lg">
                <p class="text-sm text-blue-800 dark:text-blue-200">
                    <i class="fas fa-info-circle mr-2"></i>
                    {{ _('You can use GitLab.com or your self-hosted GitLab instance.') }}
                </p>
            </div>
        </div>
    </div>

    <!-- Step 2: OAuth Setup -->
    <div class="wizard-step hidden" data-step="2">
        <h2 class="text-xl font-semibold mb-4">{{ _('Step 2: OAuth Setup') }}</h2>
        <div class="space-y-4">
            {% if current_user.is_admin %}
            <div class="p-4 bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800 rounded-lg mb-4">
                <p class="text-sm text-blue-800 dark:text-blue-200">
                    <i class="fas fa-info-circle mr-2"></i>
                    {{ _('Configure OAuth credentials. Create an application in GitLab Settings → Applications.') }}
                </p>
            </div>
            
            <div>
                <label for="gitlab_client_id" class="block text-sm font-medium mb-2">
                    {{ _('GitLab OAuth Client ID') }} <span class="text-red-500">*</span>
                </label>
                <input type="text" id="gitlab_client_id" name="gitlab_client_id"
                       class="w-full px-3 py-2 border border-border-light dark:border-border-dark rounded-lg bg-card-light dark:bg-card-dark"
                       placeholder="{{ _('Enter OAuth Client ID') }}" required>
            </div>
            
            <div>
                <label for="gitlab_client_secret" class="block text-sm font-medium mb-2">
                    {{ _('GitLab OAuth Client Secret') }} <span class="text-red-500">*</span>
                </label>
                <input type="password" id="gitlab_client_secret" name="gitlab_client_secret"
                       class="w-full px-3 py-2 border border-border-light dark:border-border-dark rounded-lg bg-card-light dark:bg-card-dark"
                       placeholder="{{ _('Enter OAuth Client Secret') }}" required>
            </div>
            {% endif %}
            
            <div class="p-4 bg-green-50 dark:bg-green-900/20 border border-green-200 dark:border-green-800 rounded-lg">
                <h3 class="font-semibold mb-2">{{ _('OAuth Redirect URI') }}</h3>
                <p class="text-sm text-text-muted-light dark:text-text-muted-dark mb-2">
                    {{ _('Add this URL as an authorized redirect URI in your GitLab application settings:') }}
                </p>
                <code class="block p-2 bg-gray-100 dark:bg-gray-800 rounded text-xs break-all">
                    {{ url_for('integrations.oauth_callback', provider='gitlab', _external=True) }}
                </code>
            </div>
        </div>
    </div>

    <!-- Step 3: Repository Selection -->
    <div class="wizard-step hidden" data-step="3">
        <h2 class="text-xl font-semibold mb-4">{{ _('Step 3: Repository Selection') }}</h2>
        <div class="space-y-4">
            <div>
                <label for="repository_ids" class="block text-sm font-medium mb-2">
                    {{ _('Repository IDs') }} <span class="text-text-muted-light dark:text-text-muted-dark">({{ _('optional') }})</span>
                </label>
                <input type="text" id="repository_ids" name="repository_ids"
                       value="{{ current_config.get('repository_ids', '') }}"
                       class="w-full px-3 py-2 border border-border-light dark:border-border-dark rounded-lg bg-card-light dark:bg-card-dark"
                       placeholder="123456, 789012">
                <p class="mt-1 text-xs text-text-muted-light dark:text-text-muted-dark">
                    {{ _('Comma-separated list of GitLab project IDs to sync. Leave empty to sync all accessible projects.') }}
                </p>
            </div>
            
            <div class="flex items-center">
                <input type="checkbox" name="create_projects" id="create_projects" value="1"
                       {% if current_config.get('create_projects', True) %}checked{% endif %}
                       class="h-4 w-4 rounded border-gray-300 text-primary focus:ring-primary">
                <label for="create_projects" class="ml-2 text-sm text-text-light dark:text-text-dark">
                    {{ _('Create Projects') }}
                </label>
            </div>
            <p class="text-xs text-text-muted-light dark:text-text-muted-dark ml-6">
                {{ _('Automatically create projects in TimeTracker from GitLab projects') }}
            </p>
        </div>
    </div>

    <!-- Step 4: Sync Settings -->
    <div class="wizard-step hidden" data-step="4">
        <h2 class="text-xl font-semibold mb-4">{{ _('Step 4: Sync Settings') }}</h2>
        <div class="space-y-4">
            <div>
                <label for="sync_direction" class="block text-sm font-medium mb-2">
                    {{ _('Sync Direction') }}
                </label>
                <select id="sync_direction" name="sync_direction"
                        class="w-full px-3 py-2 border border-border-light dark:border-border-dark rounded-lg bg-card-light dark:bg-card-dark">
                    <option value="gitlab_to_timetracker" {% if current_config.get('sync_direction', 'gitlab_to_timetracker') == 'gitlab_to_timetracker' %}selected{% endif %}>
                        {{ _('GitLab → TimeTracker (Import only)') }}
                    </option>
                    <option value="timetracker_to_gitlab" {% if current_config.get('sync_direction') == 'timetracker_to_gitlab' %}selected{% endif %}>
                        {{ _('TimeTracker → GitLab (Export only)') }}
                    </option>
                    <option value="bidirectional" {% if current_config.get('sync_direction') == 'bidirectional' %}selected{% endif %}>
                        {{ _('Bidirectional (Two-way sync)') }}
                    </option>
                </select>
            </div>
            
            <div>
                <label class="block text-sm font-medium mb-2">{{ _('Items to Sync') }}</label>
                <div class="space-y-2">
                    {% set sync_items = current_config.get('sync_items', ['issues']) %}
                    <div class="flex items-center">
                        <input type="checkbox" name="sync_items" value="issues" id="sync_issues"
                               {% if 'issues' in sync_items %}checked{% endif %}
                               class="h-4 w-4 rounded border-gray-300 text-primary focus:ring-primary">
                        <label for="sync_issues" class="ml-2 text-sm text-text-light dark:text-text-dark">{{ _('Issues') }}</label>
                    </div>
                    <div class="flex items-center">
                        <input type="checkbox" name="sync_items" value="merge_requests" id="sync_merge_requests"
                               {% if 'merge_requests' in sync_items %}checked{% endif %}
                               class="h-4 w-4 rounded border-gray-300 text-primary focus:ring-primary">
                        <label for="sync_merge_requests" class="ml-2 text-sm text-text-light dark:text-text-dark">{{ _('Merge Requests') }}</label>
                    </div>
                    <div class="flex items-center">
                        <input type="checkbox" name="sync_items" value="projects" id="sync_projects"
                               {% if 'projects' in sync_items %}checked{% endif %}
                               class="h-4 w-4 rounded border-gray-300 text-primary focus:ring-primary">
                        <label for="sync_projects" class="ml-2 text-sm text-text-light dark:text-text-dark">{{ _('Projects') }}</label>
                    </div>
                </div>
            </div>
            
            <div>
                <label class="block text-sm font-medium mb-2">{{ _('Issue States to Sync') }}</label>
                <div class="space-y-2">
                    {% set issue_states = current_config.get('issue_states', ['opened']) %}
                    <div class="flex items-center">
                        <input type="checkbox" name="issue_states" value="opened" id="state_opened"
                               {% if 'opened' in issue_states %}checked{% endif %}
                               class="h-4 w-4 rounded border-gray-300 text-primary focus:ring-primary">
                        <label for="state_opened" class="ml-2 text-sm text-text-light dark:text-text-dark">{{ _('Open Issues') }}</label>
                    </div>
                    <div class="flex items-center">
                        <input type="checkbox" name="issue_states" value="closed" id="state_closed"
                               {% if 'closed' in issue_states %}checked{% endif %}
                               class="h-4 w-4 rounded border-gray-300 text-primary focus:ring-primary">
                        <label for="state_closed" class="ml-2 text-sm text-text-light dark:text-text-dark">{{ _('Closed Issues') }}</label>
                    </div>
                </div>
            </div>
            
            <div>
                <label for="sync_interval" class="block text-sm font-medium mb-2">
                    {{ _('Sync Schedule') }}
                </label>
                <select id="sync_interval" name="sync_interval"
                        class="w-full px-3 py-2 border border-border-light dark:border-border-dark rounded-lg bg-card-light dark:bg-card-dark">
                    <option value="manual" {% if current_config.get('sync_interval', 'manual') == 'manual' %}selected{% endif %}>{{ _('Manual only') }}</option>
                    <option value="hourly" {% if current_config.get('sync_interval') == 'hourly' %}selected{% endif %}>{{ _('Every hour') }}</option>
                    <option value="daily" {% if current_config.get('sync_interval') == 'daily' %}selected{% endif %}>{{ _('Daily') }}</option>
                    <option value="weekly" {% if current_config.get('sync_interval') == 'weekly' %}selected{% endif %}>{{ _('Weekly') }}</option>
                </select>
            </div>
            
            <div class="flex items-center">
                <input type="checkbox" name="auto_sync" id="auto_sync" value="1"
                       {% if current_config.get('auto_sync', False) %}checked{% endif %}
                       class="h-4 w-4 rounded border-gray-300 text-primary focus:ring-primary">
                <label for="auto_sync" class="ml-2 text-sm text-text-light dark:text-text-dark">
                    {{ _('Auto Sync') }}
                </label>
            </div>
            <p class="text-xs text-text-muted-light dark:text-text-muted-dark ml-6">
                {{ _('Automatically sync when webhooks are received from GitLab') }}
            </p>
            
            <div>
                <label for="webhook_secret" class="block text-sm font-medium mb-2">
                    {{ _('Webhook Secret') }} <span class="text-text-muted-light dark:text-text-muted-dark">({{ _('optional') }})</span>
                </label>
                <input type="password" id="webhook_secret" name="webhook_secret"
                       class="w-full px-3 py-2 border border-border-light dark:border-border-dark rounded-lg bg-card-light dark:bg-card-dark"
                       placeholder="{{ _('Enter webhook secret') }}">
                <p class="mt-1 text-xs text-text-muted-light dark:text-text-muted-dark">
                    {{ _('Secret token for verifying webhook signatures') }}
                </p>
            </div>
        </div>
    </div>

    <!-- Step 5: Review -->
    <div class="wizard-step hidden" data-step="5">
        <h2 class="text-xl font-semibold mb-4">{{ _('Step 5: Review & Save') }}</h2>
        <div class="p-4 bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800 rounded-lg">
            <p class="text-sm text-blue-800 dark:text-blue-200 mb-4">
                <i class="fas fa-info-circle mr-2"></i>
                {{ _('Review your configuration and click "Finish" to save.') }}
            </p>
            <div class="space-y-2 text-sm">
                <div class="flex justify-between">
                    <span class="font-medium">{{ _('Instance URL') }}:</span>
                    <span id="review-instance-url" class="text-text-muted-light dark:text-text-muted-dark"></span>
                </div>
                <div class="flex justify-between">
                    <span class="font-medium">{{ _('Sync Direction') }}:</span>
                    <span id="review-sync-direction" class="text-text-muted-light dark:text-text-muted-dark"></span>
                </div>
                <div class="flex justify-between">
                    <span class="font-medium">{{ _('Items to Sync') }}:</span>
                    <span id="review-sync-items" class="text-text-muted-light dark:text-text-muted-dark"></span>
                </div>
            </div>
        </div>
        <input type="hidden" name="wizard_final_step" value="true">
    </div>
{% endblock %}

{% block wizard_js %}
<script>
document.addEventListener('DOMContentLoaded', function() {
    if (typeof IntegrationWizard !== 'undefined') {
        const wizard = new IntegrationWizard({
            totalSteps: 5,
            provider: 'gitlab',
            saveUrl: '{{ wizard_save_url }}',
            finishText: '{{ _("Finish") }}',
            onStepChange: function(step) {
                if (step === 5) {
                    const instanceUrl = document.getElementById('gitlab_instance_url')?.value || 'https://gitlab.com';
                    const syncDirection = document.getElementById('sync_direction')?.selectedOptions[0]?.text || '';
                    const syncItems = Array.from(document.querySelectorAll('input[name="sync_items"]:checked'))
                        .map(cb => cb.nextElementSibling.textContent).join(', ') || '{{ _("None") }}';
                    
                    document.getElementById('review-instance-url').textContent = instanceUrl;
                    document.getElementById('review-sync-direction').textContent = syncDirection;
                    document.getElementById('review-sync-items').textContent = syncItems;
                }
            }
        });
        wizard.init();
    }
});
</script>
{% endblock %}
