<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import { ElMessage, ElMessageBox } from 'element-plus'
import api from '@/api/client'
import PageHeader from '@/components/PageHeader.vue'
import AppCard from '@/components/AppCard.vue'

const { t } = useI18n()

interface Subscription {
  id: number
  name: string
  url: string
  type: string
  interval_sec: number
  enabled: boolean
  last_fetch_at?: string
  last_error?: string
  node_count: number
}

const items = ref<Subscription[]>([])
const loading = ref(false)
const saving = ref(false)
const dialogVisible = ref(false)
const form = ref({ name: '', url: '', type: 'auto', interval_sec: 3600 })

async function load() {
  loading.value = true
  try {
    const { data } = await api.get('/subscriptions')
    items.value = data.items || []
  } finally {
    loading.value = false
  }
}

function openCreate() {
  form.value = { name: '', url: '', type: 'auto', interval_sec: 3600 }
  dialogVisible.value = true
}

async function save() {
  saving.value = true
  try {
    await api.post('/subscriptions', form.value)
    ElMessage.success(t('subscriptions.added'))
    dialogVisible.value = false
    load()
  } finally {
    saving.value = false
  }
}

async function refresh(row: Subscription) {
  await api.post(`/subscriptions/${row.id}/refresh`)
  ElMessage.success(t('subscriptions.refreshed'))
  load()
}

async function remove(row: Subscription) {
  await ElMessageBox.confirm(t('subscriptions.confirmDelete', { name: row.name }), t('common.confirm'))
  await api.delete(`/subscriptions/${row.id}`)
  ElMessage.success(t('subscriptions.deleted'))
  load()
}

onMounted(load)
</script>

<template>
  <div class="page">
    <PageHeader :title="t('subscriptions.title')" :description="t('subscriptions.desc')">
      <template #actions>
        <el-button type="primary" @click="openCreate">{{ t('subscriptions.add') }}</el-button>
      </template>
    </PageHeader>

    <AppCard>
      <el-table :data="items" v-loading="loading" size="small">
        <el-table-column prop="name" :label="t('common.name')" min-width="120" />
        <el-table-column prop="type" :label="t('common.type')" width="80" />
        <el-table-column prop="url" :label="t('subscriptions.url')" min-width="200" show-overflow-tooltip />
        <el-table-column prop="node_count" :label="t('subscriptions.nodeCount')" width="80" />
        <el-table-column :label="t('common.status')" width="100">
          <template #default="{ row }">
            <el-tag :type="row.last_error ? 'danger' : 'success'" size="small">
              {{ row.last_error ? t('subscriptions.error') : t('subscriptions.normal') }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="last_fetch_at" :label="t('subscriptions.lastFetch')" width="168" />
        <el-table-column :label="t('common.actions')" width="120" fixed="right">
          <template #default="{ row }">
            <el-button size="small" link @click="refresh(row)">{{ t('subscriptions.refresh') }}</el-button>
            <el-button size="small" type="danger" link @click="remove(row)">{{ t('common.delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </AppCard>

    <el-dialog v-model="dialogVisible" :title="t('subscriptions.dialogTitle')" width="520px" destroy-on-close>
      <el-form label-width="100px">
        <el-form-item :label="t('common.name')"><el-input v-model="form.name" /></el-form-item>
        <el-form-item :label="t('subscriptions.url')"><el-input v-model="form.url" /></el-form-item>
        <el-form-item :label="t('common.type')">
          <el-select v-model="form.type" class="w-full">
            <el-option :label="t('subscriptions.typeAuto')" value="auto" />
            <el-option :label="t('subscriptions.typeClash')" value="clash" />
            <el-option :label="t('subscriptions.typeLinks')" value="links" />
          </el-select>
          <p class="form-hint">{{ t('subscriptions.typeHint') }}</p>
        </el-form-item>
        <el-form-item :label="t('subscriptions.interval')">
          <el-input-number v-model="form.interval_sec" :min="300" :step="300" />
          <p class="form-hint">{{ t('subscriptions.intervalHint') }}</p>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">{{ t('common.cancel') }}</el-button>
        <el-button type="primary" :loading="saving" @click="save">{{ t('common.confirm') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>
