import { useState, useEffect } from 'react'
import styles from './Header.module.css'

export default function Header({ clusterReady }) {
  const [theme, setTheme] = useState(
    () => localStorage.getItem('kubekosh-theme') || 'dark'
  )

  useEffect(() => {
    document.documentElement.setAttribute('data-theme', theme)
    localStorage.setItem('kubekosh-theme', theme)
  }, [theme])

  const toggleTheme = () => setTheme(t => t === 'dark' ? 'light' : 'dark')

  return (
    <header className={styles.header}>
      <div className={styles.brand}>
        <div className={styles.logo}>
          <img src="/logo.svg" alt="KubeKosh Logo" className={styles.logoImage} />
          <span className={styles.logoText}>KubeKosh</span>
          <span className={styles.version}>{import.meta.env.VITE_APP_VERSION}</span>
        </div>
        <span className={styles.tagline}>Interactive Kubernetes Playground</span>
      </div>

      <div className={styles.right}>
        {/* GitHub link */}
        <a
          href="https://github.com/zeborg/kubekosh"
          target="_blank"
          rel="noopener noreferrer"
          className={styles.githubBtn}
          title="Visit GitHub repository"
          aria-label="GitHub Repository"
        >
          <svg viewBox="0 0 512 512" width="16" height="16" fill="currentColor">
            <path d="M256 6.3C114.6 6.3 0 120.9 0 262.3c0 113.3 73.3 209 175 242.9 12.8 2.2 17.6-5.4 17.6-12.2 0-6.1-.3-26.2-.3-47.7-64.3 11.8-81-15.7-86.1-30.1-2.9-7.4-15.4-30.1-26.2-36.2-9-4.8-21.8-16.6-.3-17 20.2-.3 34.6 18.6 39.4 26.2 23 38.7 59.8 27.8 74.6 21.1 2.2-16.6 9-27.8 16.3-34.2-57-6.4-116.5-28.5-116.5-126.4 0-27.8 9.9-50.9 26.2-68.8-2.6-6.4-11.5-32.6 2.6-67.8 0 0 21.4-6.7 70.4 26.2 20.5-5.8 42.2-8.6 64-8.6s43.5 2.9 64 8.6c49-33.3 70.4-26.2 70.4-26.2 14.1 35.2 5.1 61.4 2.6 67.8 16.3 17.9 26.2 40.6 26.2 68.8 0 98.2-59.8 120-116.8 126.4 9.3 8 17.3 23.4 17.3 47.4 0 34.2-.3 61.8-.3 70.4 0 6.7 4.8 14.7 17.6 12.2C438.7 471.3 512 375.3 512 262.3c0-141.4-114.6-256-256-256" fillRule="evenodd" clipRule="evenodd"/>
          </svg>
        </a>

        {/* Theme toggle */}
        <button
          className={styles.themeBtn}
          onClick={toggleTheme}
          title={theme === 'dark' ? 'Switch to Light mode' : 'Switch to Dark mode'}
          aria-label="Toggle theme"
        >
          {theme === 'dark' ? '☀️' : '🌙'}
        </button>

        {/* Cluster status */}
        <div className={`${styles.clusterBadge} ${clusterReady ? styles.ready : styles.notReady}`}>
          <span className={styles.dot} />
          <span>{clusterReady ? 'Cluster Ready' : 'Connecting…'}</span>
        </div>
      </div>
    </header>
  )
}
