'use client'

import { useState, useEffect } from 'react'
import { ClusterVisualization } from '@/components/cluster-visualization'
import { BridgeNotesDashboard } from '@/components/bridge-notes-dashboard'
import { useRouter } from 'next/navigation'

interface Cluster {
  clusterId: number
  name?: string
  noteIds: string[]
}

interface BridgeNote {
  noteId: string
  bridgeScore: number
  clustersConnected: number[]
  note?: {
    id: string
    title: string | null
    content: string
  }
}

export default function InsightsPage() {
  const router = useRouter()
  const [clusters, setClusters] = useState<Cluster[]>([])
  const [bridgeNotes, setBridgeNotes] = useState<BridgeNote[]>([])
  const [loading, setLoading] = useState(true)
  const [recalculating, setRecalculating] = useState(false)
  const [message, setMessage] = useState<string | null>(null)

  useEffect(() => {
    loadClusters()
  }, [])

  const loadClusters = async () => {
    setLoading(true)
    try {
      const res = await fetch('/api/clusters')
      if (res.ok) {
        const data = await res.json()
        setClusters(data.clusters || [])

        if (data.message) {
          setMessage(data.message)
        }
      }
    } catch (error) {
      console.error('Error loading clusters:', error)
      setMessage('Failed to load clusters')
    } finally {
      setLoading(false)
    }
  }

  const recalculateClusters = async () => {
    setRecalculating(true)
    try {
      const res = await fetch('/api/clusters', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ force: true })
      })

      if (res.ok) {
        const data = await res.json()
        setClusters(data.clusters || [])
        setBridgeNotes(data.bridgeNotes || [])
        setMessage(data.message || 'Clusters recalculated successfully')
      }
    } catch (error) {
      console.error('Error recalculating clusters:', error)
      setMessage('Failed to recalculate clusters')
    } finally {
      setRecalculating(false)
    }
  }

  const handleNoteClick = (noteId: string, type: 'note' | 'cluster') => {
    if (type === 'note') {
      router.push(`/home?note=${noteId}`)
    }
  }

  return (
    <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
      {/* Header */}
      <div className="mb-8">
        <h1 className="text-3xl font-bold text-gray-900">
          Insights
        </h1>
        <p className="mt-2 text-gray-600">
          Discover thematic clusters and connections in your notes
        </p>
      </div>

      {/* Stats Bar */}
      <div className="grid grid-cols-1 md:grid-cols-4 gap-4 mb-8">
        <div className="bg-white rounded-lg shadow p-4">
          <div className="text-sm text-gray-500">Total Clusters</div>
          <div className="text-2xl font-bold text-gray-900">{clusters.length}</div>
        </div>
        <div className="bg-white rounded-lg shadow p-4">
          <div className="text-sm text-gray-500">Bridge Notes</div>
          <div className="text-2xl font-bold text-gray-900">{bridgeNotes.length}</div>
        </div>
        <div className="bg-white rounded-lg shadow p-4">
          <div className="text-sm text-gray-500">Notes Analyzed</div>
          <div className="text-2xl font-bold text-gray-900">
            {clusters.reduce((sum, c) => sum + c.noteIds.length, 0)}
          </div>
        </div>
        <div className="bg-white rounded-lg shadow p-4">
          <button
            onClick={recalculateClusters}
            disabled={recalculating || loading}
            className="w-full px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 disabled:bg-gray-400 disabled:cursor-not-allowed transition-colors"
          >
            {recalculating ? 'Calculating...' : 'Recalculate'}
          </button>
        </div>
      </div>

      {/* Message */}
      {message && (
        <div className="mb-6 p-4 bg-blue-50 border border-blue-200 rounded-lg">
          <p className="text-sm text-blue-800">{message}</p>
        </div>
      )}

      {/* Loading State */}
      {loading && (
        <div className="bg-white rounded-lg shadow p-12">
          <div className="flex flex-col items-center justify-center">
            <div className="animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600 mb-4"></div>
            <p className="text-gray-600">Analyzing your notes...</p>
          </div>
        </div>
      )}

      {/* Empty State */}
      {!loading && clusters.length === 0 && (
        <div className="bg-white rounded-lg shadow p-12">
          <div className="flex flex-col items-center justify-center text-center">
            <svg className="w-24 h-24 text-gray-400 mb-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
              <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1} d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10" />
            </svg>
            <h3 className="text-xl font-semibold text-gray-900 mb-2">
              Not enough notes to analyze
            </h3>
            <p className="text-gray-600 mb-6">
              Create at least 10 notes to start discovering clusters and connections
            </p>
            <button
              onClick={() => router.push('/home')}
              className="px-6 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors"
            >
              Create Notes
            </button>
          </div>
        </div>
      )}

      {/* Main Content */}
      {!loading && clusters.length > 0 && (
        <div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
          {/* Visualization */}
          <div className="lg:col-span-2">
            <div className="bg-white rounded-lg shadow p-4">
              <h2 className="text-lg font-semibold mb-4">Cluster Visualization</h2>
              <ClusterVisualization
                clusters={clusters}
                bridgeNotes={bridgeNotes}
                onNodeClick={handleNoteClick}
              />
            </div>
          </div>

          {/* Dashboard */}
          <div className="lg:col-span-1">
            <BridgeNotesDashboard onNoteClick={(noteId) => handleNoteClick(noteId, 'note')} />
          </div>
        </div>
      )}

      {/* Cluster List */}
      {!loading && clusters.length > 0 && (
        <div className="mt-8">
          <h2 className="text-lg font-semibold mb-4">All Clusters</h2>
          <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
            {clusters.map((cluster) => (
              <div
                key={cluster.clusterId}
                className="bg-white rounded-lg shadow p-4 hover:shadow-md transition-shadow cursor-pointer"
              >
                <h3 className="font-semibold text-gray-900 mb-1">
                  {cluster.name || `Cluster ${cluster.clusterId}`}
                </h3>
                <p className="text-sm text-gray-600">
                  {cluster.noteIds.length} {cluster.noteIds.length === 1 ? 'note' : 'notes'}
                </p>
              </div>
            ))}
          </div>
        </div>
      )}
    </div>
  )
}
