'use client'

import { useState } from 'react'
import { Copy, Check } from 'lucide-react'

const methods = [
  {
    label: 'Homebrew (macOS)',
    cmd: 'brew tap greyhavenhq/tap\nbrew install greywall',
  },
  {
    label: 'Linux / Mac',
    cmd: 'curl -fsSL https://raw.githubusercontent.com/GreyhavenHQ/greywall/main/install.sh | sh',
  },
  {
    label: 'Go install',
    cmd: 'go install github.com/GreyhavenHQ/greywall/cmd/greywall@latest',
  },
  {
    label: 'Build from source',
    cmd: 'git clone https://github.com/GreyhavenHQ/greywall\ncd greywall\nmake setup && make build',
  },
]

function CodeBlock({ cmd, label }: { cmd: string; label: string }) {
  const [copied, setCopied] = useState(false)

  function copy() {
    navigator.clipboard.writeText(cmd)
    setCopied(true)
    setTimeout(() => setCopied(false), 2000)
  }

  return (
    <div>
      <div className="text-xs font-sans font-medium text-muted-foreground mb-2">{label}</div>
      <div className="code-block px-4 sm:px-5 py-3.5 flex items-start justify-between gap-3">
        <div className="overflow-x-auto min-w-0 flex-1 scrollbar-hide">
          <pre className="font-mono text-xs sm:text-sm text-greyhaven-offwhite whitespace-pre">{cmd}</pre>
        </div>
        <button
          onClick={copy}
          className="shrink-0 p-1.5 rounded-md text-muted-foreground hover:text-foreground hover:bg-accent/30 transition-all mt-0.5"
          title="Copy to clipboard"
        >
          {copied ? (
            <Check className="h-4 w-4 text-muted-foreground" />
          ) : (
            <Copy className="h-4 w-4" />
          )}
        </button>
      </div>
    </div>
  )
}

export function GettingStarted() {
  return (
    <section id="getting-started" className="py-24 px-4 sm:px-6 border-t border-border/30">
      <div className="mx-auto max-w-5xl text-center">
        <h2 className="title-serif text-[36px] md:text-[48px] leading-none mb-4">
          Start with the CLI.
        </h2>
        <p className="text-serif font-normal text-[15px] md:text-[16px] leading-[1.55] text-muted-foreground mb-10">
          Install Greywall, then prefix the agent command you already use.
        </p>

        <div className="mx-auto max-w-2xl text-left space-y-6">
          {methods.map((m) => (
            <CodeBlock key={m.label} label={m.label} cmd={m.cmd} />
          ))}
        </div>
      </div>
    </section>
  )
}
