<template>
  <div class="app-layout">
    <!-- HEADER -->
    <header class="app-header">
      <div class="header-title">
        <span class="logo-icon">&#9889;</span>
        <h1>Traffic Generator</h1>
      </div>
      <StatusBar :health="health" :api-error="apiError" />
    </header>

    <!-- ERROR BANNER -->
    <div v-if="apiError" class="error-banner">
      <span class="error-icon">&#9888;</span>
      API unreachable: {{ apiError }} &mdash; retrying every 5s
    </div>

    <!-- MAIN CONTENT -->
    <div class="main-content">
      <!-- LEFT COLUMN: Flow Builder -->
      <aside class="left-col">
        <FlowBuilder @created="fetchFlows" @updated="fetchFlows" />
      </aside>

      <!-- RIGHT COLUMN: Tabs -->
      <main class="right-col">
        <div class="tabs">
          <button
            v-for="tab in tabs"
            :key="tab.id"
            class="tab-btn"
            :class="{ active: activeTab === tab.id }"
            @click="activeTab = tab.id"
          >
            {{ tab.label }}
          </button>
        </div>

        <div class="tab-content">
          <FlowTable v-if="activeTab === 'flows'" :flows="flows" @refresh="fetchFlows" />
          <TestBuilder v-else-if="activeTab === 'tests'" :flows="flows" @created="fetchTests" @refresh="fetchAll" />
          <TestRunner v-else-if="activeTab === 'runner'" :tests="tests" @refresh="fetchTests" />
          <ResultsPanel v-else-if="activeTab === 'results'" :tests="tests" />
          <StatsMonitor v-else-if="activeTab === 'monitor'" :flows="flows" />
        </div>
      </main>
    </div>

    <!-- FOOTER -->
    <footer class="app-footer">
      <span>Refreshing every 5s (health) / 3s (flows)</span>
      <span class="footer-sep">|</span>
      <a href="http://localhost:3000" target="_blank" class="footer-link">Grafana: :3000</a>
      <span class="footer-sep">|</span>
      <a href="http://localhost:5001" target="_blank" class="footer-link">Route Injector: :5001</a>
    </footer>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { api } from './api.js'
import StatusBar from './components/StatusBar.vue'
import FlowBuilder from './components/FlowBuilder.vue'
import FlowTable from './components/FlowTable.vue'
import TestBuilder from './components/TestBuilder.vue'
import TestRunner from './components/TestRunner.vue'
import ResultsPanel from './components/ResultsPanel.vue'
import StatsMonitor from './components/StatsMonitor.vue'

const health = ref(null)
const flows = ref([])
const tests = ref([])
const apiError = ref(null)
const activeTab = ref('flows')

const tabs = [
  { id: 'flows', label: 'Flows' },
  { id: 'tests', label: 'Tests' },
  { id: 'runner', label: 'Runner' },
  { id: 'results', label: 'Results' },
  { id: 'monitor', label: 'Monitor' },
]

async function fetchHealth() {
  try {
    health.value = await api.health()
    apiError.value = null
  } catch (e) {
    apiError.value = e.message
    health.value = null
  }
}

async function fetchFlows() {
  try {
    const data = await api.flows()
    flows.value = data.flows || []
  } catch (_) {}
}

async function fetchTests() {
  try {
    const data = await api.tests()
    tests.value = data.tests || []
  } catch (_) {}
}

async function fetchAll() {
  await Promise.all([fetchFlows(), fetchTests()])
}

let healthTimer = null
let dataTimer = null

onMounted(() => {
  fetchHealth()
  fetchAll()
  healthTimer = setInterval(fetchHealth, 5000)
  dataTimer = setInterval(fetchAll, 3000)
})

onUnmounted(() => {
  clearInterval(healthTimer)
  clearInterval(dataTimer)
})
</script>

<style>
:root {
  --bg: #0f1117;
  --card-bg: #1a1f2e;
  --border: #2d3748;
  --accent: #4f9cf9;
  --success: #48bb78;
  --danger: #fc8181;
  --warning: #f6ad55;
  --text: #e2e8f0;
  --muted: #718096;
  --radius: 8px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

button {
  cursor: pointer;
  font-family: inherit;
  font-size: 13px;
  border: none;
  border-radius: var(--radius);
  transition: opacity 0.15s, background 0.15s;
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

input, select {
  font-family: inherit;
  font-size: 13px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 10px;
  outline: none;
}

input:focus {
  border-color: var(--accent);
}
</style>

<style scoped>
.app-layout {
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  min-height: 100vh;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
  gap: 16px;
  flex-wrap: wrap;
}

.header-title {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo-icon {
  font-size: 22px;
  color: var(--warning);
}

.app-header h1 {
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
  letter-spacing: 0.02em;
}

.error-banner {
  background: rgba(252, 129, 129, 0.12);
  border-bottom: 1px solid var(--danger);
  color: var(--danger);
  padding: 8px 20px;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.error-icon {
  font-size: 16px;
}

.main-content {
  display: grid;
  grid-template-columns: 340px 1fr;
  overflow: hidden;
  height: calc(100vh - 110px);
}

.left-col {
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 16px;
}

.right-col {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.tabs {
  display: flex;
  gap: 2px;
  padding: 12px 16px 0;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
}

.tab-btn {
  background: transparent;
  color: var(--muted);
  padding: 8px 18px;
  border-radius: var(--radius) var(--radius) 0 0;
  border: 1px solid transparent;
  border-bottom: none;
  font-weight: 500;
}

.tab-btn:hover {
  color: var(--text);
  background: rgba(79, 156, 249, 0.08);
}

.tab-btn.active {
  color: var(--accent);
  background: var(--bg);
  border-color: var(--border);
  border-bottom: 1px solid var(--bg);
  margin-bottom: -1px;
}

.tab-content {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}

.app-footer {
  padding: 8px 20px;
  background: var(--card-bg);
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.footer-sep {
  color: var(--border);
}

.footer-link {
  color: var(--accent);
  text-decoration: none;
}

.footer-link:hover {
  text-decoration: underline;
}
</style>
