<div x-data="{ channelType: 'telegram' }">
    <!-- The header is now in panel_settings.html, so it can be removed or simplified -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h4 class="mb-0">Notification Configuration</h4>
        <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addChannelModal">
            <i class="bi bi-plus-lg me-2"></i> Add Channel
        </button>
    </div>

    <div class="row g-4">
        <!-- Channels Management -->
        <div class="col-12">
            <div class="card">
                <div class="card-header"><h5 class="card-title mb-0">Notification Channels</h5></div>
                <div id="channels-list" 
                     class="table-responsive"
                     hx-get="/api/notifications/channels"
                     hx-trigger="load, channelsChanged from:body"
                     hx-swap="innerHTML">
                    <div class="p-4 text-center text-muted"><span class="spinner-border spinner-border-sm"></span> Loading channels...</div>
                </div>
            </div>
        </div>

        <!-- Notification Logs -->
        <div class="col-12">
            <div class="card">
                <div class="card-header"><h5 class="card-title mb-0">Recent Notification Logs</h5></div>
                 <div id="logs-list"
                     class="table-responsive"
                     hx-get="/api/notifications/logs"
                     hx-trigger="load, logsChanged from:body, channelsChanged from:body"
                     hx-swap="innerHTML">
                    <div class="p-4 text-center text-muted"><span class="spinner-border spinner-border-sm"></span> Loading logs...</div>
                </div>
            </div>
        </div>
    </div>

    <!-- Add Channel Modal -->
    <div class="modal fade" id="addChannelModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <form id="add-channel-form"
                      hx-post="/api/notifications/channels"
                      hx-on::after-request="if(event.detail.successful) { bootstrap.Modal.getInstance(this.closest('.modal')).hide(); this.reset(); }">
                    <div class="modal-header">
                        <h5 class="modal-title">Add New Notification Channel</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>
                    <div class="modal-body">
                        <div class="mb-3">
                            <label class="form-label">Channel Name</label>
                            <input type="text" class="form-control" name="name" placeholder="e.g., My Telegram Alert" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Channel Type</label>
                            <select class="form-select" name="channel_type" x-model="channelType">
                                <option value="telegram">Telegram</option>
                                <option value="webhook">Webhook</option>
                            </select>
                        </div>

                        <!-- Telegram Fields -->
                        <div x-show="channelType === 'telegram'" x-transition>
                            <div class="mb-3">
                                <label class="form-label">Bot Token</label>
                                <input type="password" class="form-control" name="bot_token" placeholder="Your bot token from BotFather">
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Chat ID</label>
                                <input type="text" class="form-control" name="chat_id" placeholder="Your personal or group chat ID">
                            </div>
                        </div>

                        <!-- Webhook Fields -->
                        <div x-show="channelType === 'webhook'" x-transition>
                             <div class="mb-3">
                                <label class="form-label">Webhook URL</label>
                                <input type="url" class="form-control" name="webhook_url" placeholder="e.g., Discord webhook URL">
                            </div>
                        </div>

                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" name="is_enabled" value="true" id="channel_enabled_check" checked>
                            <label class="form-check-label" for="channel_enabled_check">
                                Enable this channel
                            </label>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary">Save Channel</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>