{% extends "base.html" %}
{% block title %}New Service Principal — Shoreguard{% endblock %}

{% block breadcrumbs %}
<ol class="breadcrumb mb-0">
    <li class="breadcrumb-item"><a href="/users">Users</a></li>
    <li class="breadcrumb-item active">New Service Principal</li>
</ol>
{% endblock %}

{% block content %}
<div class="row justify-content-center">
    <div class="col-md-6 col-lg-5" x-data="spNewForm()">
        <h5 class="mb-3"><i class="bi bi-key me-2"></i>New Service Principal</h5>

        <div class="card sg-card-themed">
            <div class="card-body">
                <form @submit.prevent="submit">
                    <div class="mb-3">
                        <label class="form-label">Name</label>
                        <input type="text" class="form-control" x-model="name" placeholder="terraform-ci" autofocus required>
                        <div class="form-text">A label to identify this service principal.</div>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Role</label>
                        <select class="form-select" x-model="role" required>
                            <option value="viewer">Viewer — read-only access</option>
                            <option value="operator">Operator — sandbox, policy, provider management</option>
                            <option value="admin">Admin — full access</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Expires</label>
                        <input type="date" class="form-control" x-model="expiresDate">
                        <div class="form-text">Leave empty for a non-expiring key.</div>
                    </div>

                    <!-- Error -->
                    <div x-show="error" x-cloak class="text-danger small mb-3">
                        <i class="bi bi-x-circle me-1"></i><span x-text="error"></span>
                    </div>

                    <!-- Success -->
                    <div x-show="spKey" x-cloak class="alert alert-success small py-2 mb-2">
                        <p class="mb-1"><i class="bi bi-check-circle me-1"></i>Key created!</p>
                        <span class="badge text-bg-warning mb-2"><i class="bi bi-exclamation-triangle me-1"></i>Show once — save this key now</span>
                        <div class="input-group input-group-sm">
                            <input type="text" class="form-control font-monospace" :value="spKey" readonly>
                            <button type="button" class="btn btn-outline-secondary" @click="navigator.clipboard.writeText(spKey); showToast('Copied!', 'success')">
                                <i class="bi bi-clipboard"></i>
                            </button>
                        </div>
                    </div>

                    <div class="d-flex gap-2">
                        <button x-show="!spKey" type="submit" class="btn btn-success" :disabled="loading">
                            <i class="bi bi-plus me-1"></i>Create
                        </button>
                        <a x-show="spKey" href="/users" class="btn btn-outline-secondary btn-sm">
                            <i class="bi bi-arrow-left me-1"></i>Back to Users
                        </a>
                        <a x-show="!spKey" href="/users" class="btn btn-outline-secondary">Cancel</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script src="/static/js/users.js"></script>
{% endblock %}
