<!DOCTYPE html>
<html lang="en" data-bs-theme="light">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Azure Scout</title>
    <link rel="icon" type="image/svg+xml" href="{{ url_for('static', path='img/favicon.svg') }}">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet"
          integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/simple-datatables@9/dist/style.css" rel="stylesheet">
    <link rel="stylesheet" href="{{ url_for('static', path='css/style.css') }}">
    <script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body class="d-flex flex-column min-vh-100">

<!-- ===== Navbar ===== -->
<nav class="navbar navbar-expand-sm bg-body-tertiary border-bottom">
    <div class="container-fluid">
        <a class="navbar-brand d-flex align-items-center gap-2" href="#">
            <img src="{{ url_for('static', path='img/favicon.svg') }}" width="26" height="26" alt="">
            <span>Azure Scout</span>
        </a>
        <div class="d-flex align-items-center gap-2">
            <button type="button" class="btn btn-sm btn-outline-secondary" id="theme-toggle" onclick="toggleTheme()" title="Toggle dark/light mode">
                <i class="bi bi-moon-fill" id="icon-dark"></i>
                <i class="bi bi-sun-fill d-none" id="icon-light"></i>
            </button>
            <a href="https://github.com/lrivallain/az-scout" target="_blank" rel="noopener" class="btn btn-sm btn-outline-secondary" title="GitHub">
                <i class="bi bi-github"></i>
            </a>
        </div>
    </div>
</nav>

<!-- ===== Main content ===== -->
<div class="container-fluid mt-3 flex-grow-1 overflow-auto pb-4">

    <!-- Shared selectors: Tenant + Region -->
    <div class="row mb-2 g-2 align-items-center" id="selector-bar">
        <div class="col-auto" id="tenant-section">
            <div class="input-group input-group-sm">
                <span class="input-group-text"><i class="bi bi-shield-lock"></i> Tenant</span>
                <select class="form-select form-select-sm" id="tenant-select" style="max-width:320px;">
                    <option value="">Loading tenants…</option>
                </select>
            </div>
        </div>
        <div class="col-auto">
            <div class="position-relative" id="region-combobox">
                <div class="input-group input-group-sm">
                    <span class="input-group-text"><i class="bi bi-geo-alt"></i> Region</span>
                    <input type="search" class="form-control form-control-sm" id="region-search"
                           placeholder="Loading regions…" autocomplete="off" spellcheck="false" disabled
                           style="min-width:240px;">
                </div>
                <input type="hidden" id="region-select" value="">
                <ul class="dropdown-menu w-100" id="region-dropdown"></ul>
            </div>
        </div>
    </div>

    <!-- Tabs -->
    <ul class="nav nav-tabs" id="mainTabs" role="tablist">
        <li class="nav-item" role="presentation">
            <button class="nav-link active" id="topology-tab" data-bs-toggle="tab" data-bs-target="#tab-topology"
                    type="button" role="tab" aria-controls="tab-topology" aria-selected="true">
                <i class="bi bi-diagram-3"></i> AZ Topology
            </button>
        </li>
        <li class="nav-item" role="presentation">
            <button class="nav-link" id="planner-tab" data-bs-toggle="tab" data-bs-target="#tab-planner"
                    type="button" role="tab" aria-controls="tab-planner" aria-selected="false">
                <i class="bi bi-grid-3x3-gap"></i> Deployment Planner
            </button>
        </li>
    </ul>

    <div class="tab-content" id="mainTabContent">

        <!-- ====== AZ Topology Tab ====== -->
        <div class="tab-pane fade show active" id="tab-topology" role="tabpanel" aria-labelledby="topology-tab">
            <div class="card border-top-0 rounded-top-0">
                <div class="card-body">
                    <div class="row g-3">
                        <!-- Left sidebar: subscriptions -->
                        <div class="col-md-3 col-xl-2">
                            <div class="topo-sidebar">
                                <label class="form-label form-label-sm mb-1">
                                    <i class="bi bi-collection"></i> Subscriptions
                                </label>
                                <div class="input-group input-group-sm mb-1">
                                    <input type="search" class="form-control" id="topo-sub-filter"
                                           placeholder="Filter…" autocomplete="off" spellcheck="false">
                                    <button type="button" class="btn btn-outline-secondary" onclick="topoSelectAllVisible()" title="Select visible">
                                        <i class="bi bi-check-all"></i>
                                    </button>
                                    <button type="button" class="btn btn-outline-secondary" onclick="topoDeselectAll()" title="Clear all">
                                        <i class="bi bi-x-lg"></i>
                                    </button>
                                </div>
                                <div id="topo-sub-list" class="subscription-checklist"></div>
                                <div class="d-flex justify-content-between align-items-center mt-2">
                                    <small class="text-body-secondary" id="topo-sub-count">0 selected</small>
                                    <button type="button" class="btn btn-primary btn-sm" id="topo-load-btn" onclick="loadMappings()" disabled>
                                        <i class="bi bi-arrow-repeat"></i> Load
                                    </button>
                                </div>
                            </div>
                        </div>

                        <!-- Right content: results -->
                        <div class="col-md-9 col-xl-10">
                            <!-- Error -->
                            <div id="topo-error" class="alert alert-danger alert-sm d-none" role="alert"></div>

                            <!-- Empty state -->
                            <div id="topo-empty" class="text-center text-body-secondary py-5">
                                <i class="bi bi-diagram-3 fs-1 d-block mb-2"></i>
                                <p>Select subscriptions and click <strong>Load</strong>.</p>
                            </div>

                            <!-- Loading -->
                            <div id="topo-loading" class="text-center py-5 d-none">
                                <div class="spinner-border text-primary" role="status"></div>
                                <p class="mt-2 text-body-secondary">Fetching zone mappings…</p>
                            </div>

                            <!-- Results -->
                            <div id="topo-results" class="d-none">
                                <!-- Graph -->
                                <div class="card mb-3">
                                    <div class="card-header d-flex justify-content-between align-items-center">
                                        <span><i class="bi bi-diagram-3"></i> Zone Mapping Graph</span>
                                        <button type="button" class="btn btn-sm btn-outline-secondary" onclick="exportGraphPNG()" title="Export as PNG">
                                            <i class="bi bi-download"></i> PNG
                                        </button>
                                    </div>
                                    <div class="card-body">
                                        <div id="graph-container" class="graph-container mx-auto"></div>
                                        <div id="graph-legend" class="d-flex flex-wrap gap-3 mt-2 justify-content-center"></div>
                                    </div>
                                </div>

                                <!-- Table -->
                                <div class="card">
                                    <div class="card-header d-flex justify-content-between align-items-center">
                                        <span><i class="bi bi-table"></i> Zone Mapping Table</span>
                                        <button type="button" class="btn btn-sm btn-outline-secondary" onclick="exportTableCSV()" title="Export as CSV">
                                            <i class="bi bi-download"></i> CSV
                                        </button>
                                    </div>
                                    <div class="card-body">
                                        <div id="table-container" class="table-responsive"></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- ====== Deployment Planner Tab ====== -->
        <div class="tab-pane fade" id="tab-planner" role="tabpanel" aria-labelledby="planner-tab">
            <div class="card border-top-0 rounded-top-0">
                <div class="card-body">
                    <!-- Controls row -->
                    <div class="row g-2 align-items-end mb-3">
                        <div class="col-md-3 col-lg-3">
                            <label for="planner-sub-search" class="form-label form-label-sm mb-1">
                                <i class="bi bi-collection"></i> Subscription
                            </label>
                            <div class="position-relative" id="planner-sub-combobox">
                                <input type="search" class="form-control form-control-sm" id="planner-sub-search"
                                       placeholder="Loading subscriptions…" autocomplete="off" spellcheck="false" disabled>
                                <input type="hidden" id="planner-sub-select" value="">
                                <ul class="dropdown-menu w-100" id="planner-sub-dropdown"></ul>
                            </div>
                        </div>
                        <div class="col-auto">
                            <select class="form-select form-select-sm" id="planner-currency" title="Currency" style="width:80px;">
                                <option value="USD" selected>USD</option>
                                <option value="EUR">EUR</option>
                                <option value="GBP">GBP</option>
                                <option value="JPY">JPY</option>
                                <option value="AUD">AUD</option>
                                <option value="CAD">CAD</option>
                                <option value="CHF">CHF</option>
                                <option value="SEK">SEK</option>
                                <option value="BRL">BRL</option>
                                <option value="INR">INR</option>
                            </select>
                        </div>
                        <div class="col-auto">
                            <button type="button" class="btn btn-primary btn-sm" id="planner-load-btn" onclick="loadSkus()" disabled>
                                <i class="bi bi-arrow-repeat"></i> Load SKUs
                            </button>
                        </div>
                        <div class="col d-none" id="planner-csv-btn">
                            <div class="d-flex justify-content-end gap-2 align-items-center">
                                <div class="form-check form-switch mb-0">
                                    <input class="form-check-input" type="checkbox" id="planner-show-spot" checked onchange="toggleTableColumns()">
                                    <label class="form-check-label small" for="planner-show-spot">Spot</label>
                                </div>
                                <div class="form-check form-switch mb-0">
                                    <input class="form-check-input" type="checkbox" id="planner-show-prices" checked onchange="toggleTableColumns()">
                                    <label class="form-check-label small" for="planner-show-prices">Prices</label>
                                </div>
                                <button type="button" class="btn btn-sm btn-outline-secondary" onclick="exportSkuCSV()" title="Export as CSV">
                                    <i class="bi bi-download"></i> CSV
                                </button>
                            </div>
                        </div>
                    </div>

                    <!-- Error -->
                    <div id="planner-error" class="alert alert-danger alert-sm d-none" role="alert"></div>

                    <!-- Empty state -->
                    <div id="planner-empty" class="text-center text-body-secondary py-5">
                        <i class="bi bi-grid-3x3-gap fs-1 d-block mb-2"></i>
                        <p>Select a subscription, then click <strong>Load SKUs</strong>.</p>
                    </div>

                    <!-- Loading -->
                    <div id="planner-loading" class="text-center py-5 d-none">
                        <div class="spinner-border text-primary" role="status"></div>
                        <p class="mt-2 text-body-secondary">Fetching SKU data…</p>
                    </div>

                    <!-- SKU results -->
                    <div id="planner-results" class="d-none">
                        <div id="region-summary" class="d-none"></div>
                        <div id="sku-table-container"></div>
                    </div>
                </div>
            </div>
        </div>

    </div><!-- /tab-content -->
</div><!-- /container -->

<!-- ===== Footer ===== -->
<footer class="border-top py-2 text-center small text-body-secondary mt-auto">
    <span>v{{ version }}</span> ·
    <span>MIT License</span> ·
    <span>Independent Azure analysis tool — not affiliated with Microsoft.</span>
</footer>

<!-- ===== Spot Score Modal ===== -->
<div class="modal fade" id="spotModal" tabindex="-1" aria-labelledby="spotModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="spotModalLabel">Spot Placement Score</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <p class="fw-semibold mb-2" id="spot-modal-sku"></p>
                <label for="spot-modal-instances" class="form-label small">Number of instances:</label>
                <input type="number" class="form-control form-control-sm mb-3" id="spot-modal-instances" value="1" min="1" max="1000">
                <div id="spot-modal-loading" class="text-center d-none">
                    <div class="spinner-border spinner-border-sm text-primary" role="status"></div>
                    <span class="ms-2 small">Fetching score…</span>
                </div>
                <div id="spot-modal-result" class="d-none"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary btn-sm" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary btn-sm" onclick="confirmSpotScore()">Get Score</button>
            </div>
        </div>
    </div>
</div>

<!-- ===== Pricing Detail Modal ===== -->
<div class="modal fade" id="pricingModal" tabindex="-1" aria-labelledby="pricingModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-xl modal-fullscreen-md-down modal-dialog-centered modal-dialog-scrollable">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="pricingModalLabel">SKU Detail — <span id="pricing-modal-sku"></span></h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div id="pricing-modal-loading" class="text-center d-none">
                    <div class="spinner-border spinner-border-sm text-primary" role="status"></div>
                    <span class="ms-2 small">Fetching prices…</span>
                </div>
                <div id="pricing-modal-content" class="d-none"></div>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/simple-datatables@9/dist/umd/simple-datatables.js"></script>
<script src="{{ url_for('static', path='js/app.js') }}"></script>
</body>
</html>
