import { useState, useEffect } from 'react'
import { AreaChart, Area, ResponsiveContainer, Tooltip, XAxis, YAxis } from 'recharts'
import { getMetricsHistory } from '../api'
import { useUser } from '../context/UserContext'

const GRAFANA_URL = 'https://grafana.dimension47.de'

export default function MetricsChart({ serverId, serverName, expanded = false }) {
  const { token } = useUser()
  const [data, setData] = useState(null)
  const [loading, setLoading] = useState(true)
  const [range, setRange] = useState('1h')

  useEffect(() => {
    const fetchData = async () => {
      try {
        const metrics = await getMetricsHistory(token, serverId, range)
        setData(metrics)
      } catch (err) {
        console.error('Failed to fetch metrics:', err)
      } finally {
        setLoading(false)
      }
    }

    fetchData()
    const interval = setInterval(fetchData, 60000)
    return () => clearInterval(interval)
  }, [token, serverId, range])

  const formatTime = (timestamp) => {
    const date = new Date(timestamp)
    return date.toLocaleTimeString('de-DE', { hour: '2-digit', minute: '2-digit' })
  }

  const formatBytes = (bytes) => {
    if (bytes < 1024) return bytes.toFixed(0) + ' B/s'
    if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB/s'
    return (bytes / 1024 / 1024).toFixed(1) + ' MB/s'
  }

  if (loading) {
    return (
      <div className="card p-4">
        <div className="h-20 flex items-center justify-center">
          <span className="text-neutral-500 text-sm">Loading metrics...</span>
        </div>
      </div>
    )
  }

  if (!data || data.cpu.length === 0) {
    return (
      <div className="card p-4">
        <div className="h-20 flex items-center justify-center">
          <span className="text-neutral-500 text-sm">No data available</span>
        </div>
      </div>
    )
  }

  const combinedData = data.cpu.map((point, i) => ({
    timestamp: point.timestamp,
    cpu: point.value,
    memory: data.memory[i]?.value || 0,
    networkRx: data.networkRx[i]?.value || 0,
    networkTx: data.networkTx[i]?.value || 0
  }))

  const CustomTooltip = ({ active, payload, label }) => {
    if (active && payload && payload.length) {
      return (
        <div className="bg-neutral-900 border border-neutral-700 rounded-md p-3 text-xs shadow-lg">
          <div className="text-neutral-400 mb-2">{formatTime(label)}</div>
          {payload.map((entry, i) => (
            <div key={i} className="flex justify-between gap-4" style={{ color: entry.color }}>
              <span>{entry.name}:</span>
              <span className="font-semibold">
                {entry.name.includes('Network') ? formatBytes(entry.value) : entry.value.toFixed(1) + '%'}
              </span>
            </div>
          ))}
        </div>
      )
    }
    return null
  }

  const ChartCard = ({ title, dataKey, color, domain, formatter }) => (
    <div className="card p-4">
      <div className="flex justify-between items-center mb-3">
        <span className="text-neutral-400 text-xs uppercase tracking-wide">{title}</span>
        <span className="text-white text-sm font-semibold">
          {formatter
            ? formatter(combinedData[combinedData.length - 1]?.[dataKey] || 0)
            : (combinedData[combinedData.length - 1]?.[dataKey] || 0).toFixed(1) + '%'
          }
        </span>
      </div>
      <ResponsiveContainer width="100%" height={expanded ? 150 : 80}>
        <AreaChart data={combinedData}>
          <defs>
            <linearGradient id={`gradient-${dataKey}`} x1="0" y1="0" x2="0" y2="1">
              <stop offset="5%" stopColor={color} stopOpacity={0.3}/>
              <stop offset="95%" stopColor={color} stopOpacity={0.05}/>
            </linearGradient>
          </defs>
          {expanded && (
            <>
              <XAxis
                dataKey="timestamp"
                tickFormatter={formatTime}
                tick={{ fill: '#737373', fontSize: 10 }}
                axisLine={{ stroke: '#404040' }}
                tickLine={false}
                minTickGap={50}
              />
              <YAxis
                domain={domain || [0, 'auto']}
                tick={{ fill: '#737373', fontSize: 10 }}
                axisLine={{ stroke: '#404040' }}
                tickLine={false}
                width={40}
                tickFormatter={formatter ? (v) => formatBytes(v).split(' ')[0] : (v) => v.toFixed(0)}
              />
              <Tooltip content={<CustomTooltip />} />
            </>
          )}
          <Area
            type="monotone"
            dataKey={dataKey}
            name={title}
            stroke={color}
            strokeWidth={2}
            fill={`url(#gradient-${dataKey})`}
            isAnimationActive={true}
            animationDuration={500}
          />
        </AreaChart>
      </ResponsiveContainer>
    </div>
  )

  return (
    <div className="space-y-4">
      {/* Header */}
      <div className="flex items-center justify-between">
        <span className="text-neutral-400 text-sm">Metrics History</span>
        <div className="flex items-center gap-3">
          <select
            value={range}
            onChange={(e) => setRange(e.target.value)}
            className="select"
          >
            <option value="15m">15 min</option>
            <option value="1h">1 hour</option>
            <option value="6h">6 hours</option>
            <option value="24h">24 hours</option>
          </select>
          <a
            href={`${GRAFANA_URL}/d/rYdddlPWk/node-exporter-full?var-job=${serverId}`}
            target="_blank"
            rel="noopener noreferrer"
            className="btn btn-secondary text-sm"
          >
            Grafana
          </a>
        </div>
      </div>

      {/* Charts */}
      {expanded ? (
        <div className="space-y-4">
          <ChartCard title="CPU Usage" dataKey="cpu" color="#22c55e" domain={[0, 100]} />
          <ChartCard title="Memory Usage" dataKey="memory" color="#3b82f6" domain={[0, 100]} />
          <div className="grid grid-cols-2 gap-4">
            <ChartCard title="Network RX" dataKey="networkRx" color="#a855f7" formatter={formatBytes} />
            <ChartCard title="Network TX" dataKey="networkTx" color="#eab308" formatter={formatBytes} />
          </div>
        </div>
      ) : (
        <div className="grid grid-cols-2 gap-3">
          <ChartCard title="CPU" dataKey="cpu" color="#22c55e" domain={[0, 100]} />
          <ChartCard title="Memory" dataKey="memory" color="#3b82f6" domain={[0, 100]} />
        </div>
      )}
    </div>
  )
}
