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

{% block wizard_steps %}
    <!-- Step 1: OAuth Connection -->
    <div class="wizard-step" data-step="1">
        <h2 class="text-xl font-semibold mb-4">{{ _('Step 1: OAuth Connection') }}</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 from Xero Developer Portal.') }}
                </p>
            </div>
            
            <div>
                <label for="xero_client_id" class="block text-sm font-medium mb-2">
                    {{ _('Xero OAuth Client ID') }} <span class="text-red-500">*</span>
                </label>
                <input type="text" id="xero_client_id" name="xero_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="xero_client_secret" class="block text-sm font-medium mb-2">
                    {{ _('Xero OAuth Client Secret') }} <span class="text-red-500">*</span>
                </label>
                <input type="password" id="xero_client_secret" name="xero_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>
    </div>

    <!-- Step 2: Tenant Selection -->
    <div class="wizard-step hidden" data-step="2">
        <h2 class="text-xl font-semibold mb-4">{{ _('Step 2: Tenant Selection') }}</h2>
        <div class="space-y-4">
            <div>
                <label for="tenant_id" class="block text-sm font-medium mb-2">
                    {{ _('Tenant ID') }} <span class="text-red-500">*</span>
                </label>
                <input type="text" id="tenant_id" name="tenant_id"
                       value="{{ current_config.get('tenant_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="tenant-uuid-123" required>
                <p class="mt-1 text-xs text-text-muted-light dark:text-text-muted-dark">
                    {{ _('Find your tenant ID (organisation ID) in Xero after connecting via OAuth.') }}
                </p>
            </div>
        </div>
    </div>

    <!-- Step 3: Sync Configuration -->
    <div class="wizard-step hidden" data-step="3">
        <h2 class="text-xl font-semibold mb-4">{{ _('Step 3: Sync Configuration') }}</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="xero_to_timetracker" {% if current_config.get('sync_direction', 'timetracker_to_xero') == 'xero_to_timetracker' %}selected{% endif %}>
                        {{ _('Xero → TimeTracker (Import only)') }}
                    </option>
                    <option value="timetracker_to_xero" {% if current_config.get('sync_direction', 'timetracker_to_xero') == 'timetracker_to_xero' %}selected{% endif %}>
                        {{ _('TimeTracker → Xero (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', ['invoices', 'expenses']) %}
                    <div class="flex items-center">
                        <input type="checkbox" name="sync_items" value="invoices" id="sync_invoices"
                               {% if 'invoices' in sync_items %}checked{% endif %}
                               class="h-4 w-4 rounded border-gray-300 text-primary focus:ring-primary">
                        <label for="sync_invoices" class="ml-2 text-sm">{{ _('Invoices') }}</label>
                    </div>
                    <div class="flex items-center">
                        <input type="checkbox" name="sync_items" value="expenses" id="sync_expenses"
                               {% if 'expenses' in sync_items %}checked{% endif %}
                               class="h-4 w-4 rounded border-gray-300 text-primary focus:ring-primary">
                        <label for="sync_expenses" class="ml-2 text-sm">{{ _('Expenses') }}</label>
                    </div>
                    <div class="flex items-center">
                        <input type="checkbox" name="sync_items" value="payments" id="sync_payments"
                               {% if 'payments' in sync_items %}checked{% endif %}
                               class="h-4 w-4 rounded border-gray-300 text-primary focus:ring-primary">
                        <label for="sync_payments" class="ml-2 text-sm">{{ _('Payments') }}</label>
                    </div>
                    <div class="flex items-center">
                        <input type="checkbox" name="sync_items" value="contacts" id="sync_contacts"
                               {% if 'contacts' in sync_items %}checked{% endif %}
                               class="h-4 w-4 rounded border-gray-300 text-primary focus:ring-primary">
                        <label for="sync_contacts" class="ml-2 text-sm">{{ _('Contacts') }}</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>
                </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">{{ _('Auto Sync') }}</label>
            </div>
        </div>
    </div>

    <!-- Step 4: Mappings -->
    <div class="wizard-step hidden" data-step="4">
        <h2 class="text-xl font-semibold mb-4">{{ _('Step 4: Account Mappings') }} <span class="text-text-muted-light dark:text-text-muted-dark text-sm font-normal">({{ _('optional') }})</span></h2>
        <div class="space-y-4">
            <div>
                <label for="default_expense_account_code" class="block text-sm font-medium mb-2">
                    {{ _('Default Expense Account Code') }}
                </label>
                <input type="text" id="default_expense_account_code" name="default_expense_account_code"
                       value="{{ current_config.get('default_expense_account_code', '200') }}"
                       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="200">
                <p class="mt-1 text-xs text-text-muted-light dark:text-text-muted-dark">
                    {{ _('Xero account code to use for expenses when no mapping is configured') }}
                </p>
            </div>
            
            <div>
                <label for="contact_mappings" class="block text-sm font-medium mb-2">
                    {{ _('Contact Mappings') }} <span class="text-text-muted-light dark:text-text-muted-dark text-xs font-normal">({{ _('JSON') }})</span>
                </label>
                <textarea id="contact_mappings" name="contact_mappings" rows="4"
                          class="w-full px-3 py-2 border border-border-light dark:border-border-dark rounded-lg bg-card-light dark:bg-card-dark font-mono text-sm"
                          placeholder='{"1": "contact-uuid-123", "2": "contact-uuid-456"}'>{{ current_config.get('contact_mappings', '')|tojson|safe if current_config.get('contact_mappings') else '' }}</textarea>
                <p class="mt-1 text-xs text-text-muted-light dark:text-text-muted-dark">
                    {{ _('Map TimeTracker client IDs to Xero Contact IDs (JSON format)') }}
                </p>
            </div>
            
            <div>
                <label for="account_mappings" class="block text-sm font-medium mb-2">
                    {{ _('Account Mappings') }} <span class="text-text-muted-light dark:text-text-muted-dark text-xs font-normal">({{ _('JSON') }})</span>
                </label>
                <textarea id="account_mappings" name="account_mappings" rows="4"
                          class="w-full px-3 py-2 border border-border-light dark:border-border-dark rounded-lg bg-card-light dark:bg-card-dark font-mono text-sm"
                          placeholder='{"expense_category_1": "account_code_123"}'>{{ current_config.get('account_mappings', '')|tojson|safe if current_config.get('account_mappings') else '' }}</textarea>
                <p class="mt-1 text-xs text-text-muted-light dark:text-text-muted-dark">
                    {{ _('Map TimeTracker expense category IDs to Xero account codes (JSON format)') }}
                </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">{{ _('Tenant ID') }}:</span>
                    <span id="review-tenant-id" 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: 'xero',
            saveUrl: '{{ wizard_save_url }}',
            finishText: '{{ _("Finish") }}',
            onStepChange: function(step) {
                if (step === 5) {
                    const tenantId = document.getElementById('tenant_id')?.value || '{{ _("Not set") }}';
                    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-tenant-id').textContent = tenantId;
                    document.getElementById('review-sync-direction').textContent = syncDirection;
                    document.getElementById('review-sync-items').textContent = syncItems;
                }
            }
        });
        wizard.init();
    }
});
</script>
{% endblock %}
