<template>
  <div>
    <AppHeader />
    
    <div class="container-fluid py-4">
      <div class="d-flex justify-content-between align-items-center mb-4">
        <h4 class="mb-0">
          <NuxtLink to="/admin" class="text-decoration-none text-muted me-2"><i class="bi bi-arrow-left"></i></NuxtLink>
          <i class="bi bi-git me-2"></i>VCS 서버 관리
        </h4>
        <button class="btn btn-primary" @click="openModal()">
          <i class="bi bi-plus-lg me-1"></i>서버 추가
        </button>
      </div>

      <div class="card">
        <div class="table-responsive">
          <table class="table table-hover mb-0">
            <thead class="table-light">
              <tr>
                <th style="width: 50px" class="text-center">No</th>
                <th>서버명</th>
                <th style="width: 80px">유형</th>
                <th>URL</th>
                <th style="width: 80px" class="text-center">상태</th>
                <th style="width: 100px">등록일</th>
                <th style="width: 100px" class="text-center">관리</th>
              </tr>
            </thead>
            <tbody>
              <tr v-if="isLoading">
                <td colspan="7" class="text-center py-4"><span class="spinner-border spinner-border-sm"></span></td>
              </tr>
              <tr v-else-if="servers.length === 0">
                <td colspan="7" class="text-center py-5 text-muted">
                  <i class="bi bi-inbox display-4"></i>
                  <p class="mt-2 mb-0">등록된 VCS 서버가 없습니다.</p>
                </td>
              </tr>
              <tr v-else v-for="(s, idx) in servers" :key="s.serverId">
                <td class="text-center">{{ idx + 1 }}</td>
                <td>
                  <div class="fw-bold">{{ s.serverName }}</div>
                  <small class="text-muted">{{ s.description || '' }}</small>
                </td>
                <td>
                  <span v-if="s.serverType === 'git'" class="badge bg-success">Git</span>
                  <span v-else class="badge bg-info">SVN</span>
                </td>
                <td><code class="small">{{ s.serverUrl }}</code></td>
                <td class="text-center">
                  <span v-if="s.isActive" class="badge bg-success">활성</span>
                  <span v-else class="badge bg-secondary">비활성</span>
                </td>
                <td>{{ formatDate(s.createdAt) }}</td>
                <td class="text-center">
                  <button class="btn btn-sm btn-outline-primary me-1" @click="openModal(s)">
                    <i class="bi bi-pencil"></i>
                  </button>
                  <button class="btn btn-sm btn-outline-danger" @click="deleteServer(s)">
                    <i class="bi bi-trash"></i>
                  </button>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>

    <!-- 생성/수정 모달 -->
    <div class="modal fade" :class="{ show: showModal }" :style="{ display: showModal ? 'block' : 'none' }">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title">{{ isEdit ? 'VCS 서버 수정' : 'VCS 서버 추가' }}</h5>
            <button type="button" class="btn-close" @click="showModal = false"></button>
          </div>
          <div class="modal-body">
            <div class="mb-3">
              <label class="form-label">서버명 <span class="text-danger">*</span></label>
              <input type="text" class="form-control" v-model="form.serverName" placeholder="예: GitHub, 사내 GitLab" />
            </div>
            <div class="mb-3">
              <label class="form-label">유형 <span class="text-danger">*</span></label>
              <div class="btn-group w-100">
                <input type="radio" class="btn-check" name="serverType" id="typeGit" value="git" v-model="form.serverType">
                <label class="btn btn-outline-success" for="typeGit"><i class="bi bi-git me-1"></i>Git</label>
                <input type="radio" class="btn-check" name="serverType" id="typeSvn" value="svn" v-model="form.serverType">
                <label class="btn btn-outline-info" for="typeSvn"><i class="bi bi-code-slash me-1"></i>SVN</label>
              </div>
            </div>
            <div class="mb-3">
              <label class="form-label">서버 URL <span class="text-danger">*</span></label>
              <input type="text" class="form-control" v-model="form.serverUrl" placeholder="https://github.com" />
            </div>
            <div class="mb-3">
              <label class="form-label">설명</label>
              <textarea class="form-control" v-model="form.description" rows="2"></textarea>
            </div>
            <div v-if="isEdit" class="mb-3">
              <div class="form-check">
                <input class="form-check-input" type="checkbox" id="isActive" v-model="form.isActive">
                <label class="form-check-label" for="isActive">활성화</label>
              </div>
            </div>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" @click="showModal = false">취소</button>
            <button type="button" class="btn btn-primary" @click="saveServer" :disabled="isSaving">
              {{ isSaving ? '저장 중...' : '저장' }}
            </button>
          </div>
        </div>
      </div>
    </div>
    <div class="modal-backdrop fade show" v-if="showModal"></div>
  </div>
</template>

<script setup lang="ts">
const { fetchCurrentUser } = useAuth()
const router = useRouter()

interface VcsServer {
  serverId: number
  serverName: string
  serverType: string
  serverUrl: string
  description: string
  isActive: boolean
  createdAt: string
}

const servers = ref<VcsServer[]>([])
const isLoading = ref(false)
const isSaving = ref(false)
const showModal = ref(false)
const isEdit = ref(false)
const editServerId = ref<number | null>(null)

const form = ref({
  serverName: '',
  serverType: 'git',
  serverUrl: '',
  description: '',
  isActive: true
})

onMounted(async () => {
  const user = await fetchCurrentUser()
  if (!user) { router.push('/login'); return }
  await loadServers()
})

async function loadServers() {
  isLoading.value = true
  try {
    const res = await $fetch<{ servers: VcsServer[] }>('/api/vcs-server/list', { query: { includeInactive: 'true' } })
    servers.value = res.servers || []
  } catch (e) { console.error(e) }
  finally { isLoading.value = false }
}

function openModal(server?: VcsServer) {
  if (server) {
    isEdit.value = true
    editServerId.value = server.serverId
    form.value = {
      serverName: server.serverName,
      serverType: server.serverType,
      serverUrl: server.serverUrl,
      description: server.description || '',
      isActive: server.isActive
    }
  } else {
    isEdit.value = false
    editServerId.value = null
    form.value = { serverName: '', serverType: 'git', serverUrl: '', description: '', isActive: true }
  }
  showModal.value = true
}

async function saveServer() {
  if (!form.value.serverName.trim()) { alert('서버명을 입력해주세요.'); return }
  if (!form.value.serverUrl.trim()) { alert('서버 URL을 입력해주세요.'); return }
  
  isSaving.value = true
  try {
    if (isEdit.value && editServerId.value) {
      await $fetch(`/api/vcs-server/${editServerId.value}/update`, { method: 'PUT', body: form.value })
    } else {
      await $fetch('/api/vcs-server/create', { method: 'POST', body: form.value })
    }
    showModal.value = false
    await loadServers()
  } catch (e: any) {
    alert(e.data?.message || '저장에 실패했습니다.')
  } finally {
    isSaving.value = false
  }
}

async function deleteServer(server: VcsServer) {
  if (!confirm(`"${server.serverName}" 서버를 삭제하시겠습니까?`)) return
  try {
    await $fetch(`/api/vcs-server/${server.serverId}/delete`, { method: 'DELETE' })
    await loadServers()
  } catch (e: any) {
    alert(e.data?.message || '삭제에 실패했습니다.')
  }
}

function formatDate(d: string | null) {
  if (!d) return '-'
  return d.split('T')[0]
}
</script>

<style scoped>
.modal.show { background: rgba(0, 0, 0, 0.5); }
</style>
