import Link from 'next/link'
import { redirect } from 'next/navigation'
import { getCurrentUser } from '@/lib/session'
import { execAgent } from '@/lib/agent-client'
import FlowPanel from './_components/flow-panel'

export const dynamic = 'force-dynamic'

export default async function UpdateCaddyConfigPage() {
  const user = await getCurrentUser()
  if (!user) redirect('/login')

  let caddyfile = ''
  let caddyfileError: string | null = null
  try {
    caddyfile = await execAgent('caddy_show_config')
  } catch (err) {
    caddyfileError = err instanceof Error ? err.message : 'failed to load Caddyfile'
  }

  return (
    <div className="min-h-screen bg-background p-6">
      <div className="mx-auto max-w-4xl space-y-6">
        <div className="flex items-center gap-3">
          <Link href="/" className="text-sm text-muted-foreground hover:text-foreground">
            ← Home
          </Link>
          <span className="text-muted-foreground">/</span>
          <h1 className="text-2xl font-semibold tracking-tight">Update Caddy Config</h1>
        </div>

        <FlowPanel caddyfile={caddyfile} caddyfileError={caddyfileError} />
      </div>
    </div>
  )
}
