import { useState } from 'react'

function App() {
  const [inputText, setInputText] = useState("Critical satellite telemetry data sequence initialized.")
  const [corruptedText, setCorruptedText] = useState("")
  const [decodedText, setDecodedText] = useState("")
  const [loading, setLoading] = useState(false)

  const handleTransmit = async () => {
    setLoading(true)
    setCorruptedText("")
    setDecodedText("")

    try {
      // 1. Send your custom text to the backend
      const response = await fetch('/api/transmit', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ text: inputText })
      })
      
      const data = await response.json()
      
      // 2. Display the results
      setCorruptedText(data.corrupted)
      setDecodedText(data.decoded)
    } catch (error) {
      setDecodedText("Error connecting to the server.")
    }
    setLoading(false)
  }

  return (
    <div className="min-h-screen bg-gray-900 text-white p-8 font-sans">
      <div className="max-w-2xl mx-auto space-y-6">
        <h1 className="text-3xl font-bold text-blue-400">🧠 6G Semantic Decoder</h1>
        
        {/* The New Text Input Box */}
        <div className="space-y-2">
          <label className="text-sm text-gray-400">Message to Transmit:</label>
          <textarea 
            className="w-full p-3 bg-gray-800 rounded border border-gray-700 text-white"
            rows="3"
            value={inputText}
            onChange={(e) => setInputText(e.target.value)}
          />
        </div>

        <button 
          onClick={handleTransmit}
          disabled={loading}
          className="w-full bg-blue-600 hover:bg-blue-500 text-white font-bold py-3 px-4 rounded transition-colors"
        >
          {loading ? "Transmitting..." : "Transmit Message"}
        </button>

        {/* Display the Corrupted Text */}
        {corruptedText && (
          <div className="p-4 bg-red-900/30 border border-red-700 rounded">
            <h3 className="text-red-400 text-sm font-bold mb-1">Traditional PHY Decoder (Noisy):</h3>
            <p className="font-mono">{corruptedText}</p>
          </div>
        )}

        {/* Display the AI Decoded Text */}
        {decodedText && (
          <div className="p-4 bg-green-900/30 border border-green-700 rounded">
            <h3 className="text-green-400 text-sm font-bold mb-1">AI Semantic Recovery:</h3>
            <p className="font-mono">{decodedText}</p>
          </div>
        )}
      </div>
    </div>
  )
}

export default App