File size: 16,797 Bytes
1222f44
 
ee2c5d5
 
 
 
 
 
 
 
 
 
9492321
 
 
9eed0b2
dfb0010
9492321
 
ee2c5d5
 
b081ffe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1222f44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b081ffe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1222f44
b081ffe
1222f44
 
 
 
 
 
 
 
 
 
 
 
b081ffe
1222f44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72c8847
9492321
 
ee2c5d5
 
 
9492321
 
 
ee2c5d5
 
72c8847
 
 
 
 
 
 
4bf8d80
72c8847
 
ee2c5d5
9492321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee2c5d5
 
 
9492321
 
9eed0b2
 
9492321
 
 
 
 
9eed0b2
ee2c5d5
9492321
ee2c5d5
9492321
 
1222f44
 
 
 
 
 
ee2c5d5
9492321
 
ee2c5d5
 
9492321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee2c5d5
 
 
9492321
 
1222f44
 
 
 
 
 
ee2c5d5
 
9492321
 
ee2c5d5
9492321
ee2c5d5
 
9492321
ee2c5d5
 
 
 
 
1222f44
 
ee2c5d5
 
9492321
ee2c5d5
9492321
 
 
 
 
 
 
 
 
 
 
 
 
 
ee2c5d5
 
 
9eed0b2
 
 
 
 
 
 
 
 
 
 
ee2c5d5
 
 
 
 
 
9492321
 
 
ee2c5d5
9492321
ee2c5d5
9492321
 
 
 
ee2c5d5
 
 
 
9492321
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import React, { useState } from 'react'
import { X, Globe, ExternalLink } from 'lucide-react'

const ENTITY_COLORS = {
  startup: '#3B82F6', sme: '#10B981', college_ecell: '#FBBF24',
  incubator: '#A855F7', accelerator: '#EC4899',
}
const ENTITY_LABELS = {
  startup: 'πŸš€ Startup', sme: '🏒 SME', college_ecell: 'πŸŽ“ E-Cell',
  incubator: 'πŸ§ͺ Incubator', accelerator: '⚑ Accelerator',
}

function formatFunding(v) {
  if (!v || v === 0) return 'Bootstrapped'
  const cr = v / 10000000
  if (cr >= 10000) return `β‚Ή${(cr/100000).toFixed(2)} Lakh Cr`
  if (cr >= 100) return `β‚Ή${cr.toLocaleString('en-IN', { maximumFractionDigits: 0 })} Cr`
  if (cr >= 1) return `β‚Ή${cr.toFixed(1)} Cr`
  return `β‚Ή${(v/100000).toFixed(0)} L`
}

// ─── URL Validation ───────────────────────────────────────────────────────────
function isValidHttpUrl(url) {
  if (!url || typeof url !== 'string') return false
  try {
    const u = new URL(url.trim())
    const protocol = u.protocol.toLowerCase()
    if (protocol !== 'http:' && protocol !== 'https:') return false
    const hostname = u.hostname.toLowerCase()
    // Block private IPs and localhost
    if (hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '0.0.0.0') return false
    if (hostname.startsWith('192.168.') || hostname.startsWith('10.')) return false
    if (hostname.startsWith('172.')) {
      const parts = hostname.split('.')
      if (parts.length >= 2) {
        const second = parseInt(parts[1], 10)
        if (second >= 16 && second <= 31) return false
      }
    }
    return true
  } catch {
    return false
  }
}

function sanitizeUrl(url) {
  if (!url || typeof url !== 'string') return ''
  const trimmed = url.trim()
  const lower = trimmed.toLowerCase()
  if (lower.startsWith('javascript:') || lower.startsWith('data:') || lower.startsWith('vbscript:')) {
    return ''
  }
  return trimmed
}

const SOCIAL_CONFIG = {
  linkedin: { icon: 'πŸ’Ό', color: '#0A66C2', label: 'LinkedIn' },
  twitter: { icon: '𝕏', color: '#000000', label: 'X (Twitter)' },
  instagram: { icon: 'πŸ“Έ', color: '#E4405F', label: 'Instagram' },
  facebook: { icon: 'πŸ“˜', color: '#1877F2', label: 'Facebook' },
  website: { icon: '🌐', color: '#6366F1', label: 'Website' },
  crunchbase: { icon: 'CB', color: '#0288D1', label: 'Crunchbase' },
  tracxn: { icon: 'TX', color: '#FF6F00', label: 'Tracxn' },
  google: { icon: 'πŸ”', color: '#4285F4', label: 'Google Search' },
}

function SocialLinksGrid({ entity }) {
  const links = []
  const name = entity.name

  if (entity.linkedin_url && isValidHttpUrl(entity.linkedin_url)) {
    links.push({ ...SOCIAL_CONFIG.linkedin, url: sanitizeUrl(entity.linkedin_url), verified: true })
  }
  if (entity.twitter_url && isValidHttpUrl(entity.twitter_url)) {
    links.push({ ...SOCIAL_CONFIG.twitter, url: sanitizeUrl(entity.twitter_url), verified: true })
  }
  if (entity.instagram_url && isValidHttpUrl(entity.instagram_url)) {
    links.push({ ...SOCIAL_CONFIG.instagram, url: sanitizeUrl(entity.instagram_url), verified: true })
  }
  if (entity.facebook_url && isValidHttpUrl(entity.facebook_url)) {
    links.push({ ...SOCIAL_CONFIG.facebook, url: sanitizeUrl(entity.facebook_url), verified: true })
  }
  if (entity.website && isValidHttpUrl(entity.website)) {
    links.push({ ...SOCIAL_CONFIG.website, url: sanitizeUrl(entity.website), verified: true })
  }

  // Search links (always available, constructed safely)
  const slug = name.toLowerCase().replace(/\s+/g, '-').replace('.', '')
  const encName = encodeURIComponent(name)
  if (!entity.linkedin_url) links.push({ ...SOCIAL_CONFIG.linkedin, url: `https://www.linkedin.com/search/results/companies/?keywords=${encName}`, search: true })
  if (!entity.twitter_url) links.push({ ...SOCIAL_CONFIG.twitter, url: `https://x.com/search?q=${encName}&src=typed_query`, search: true })
  if (!entity.instagram_url) links.push({ ...SOCIAL_CONFIG.instagram, url: `https://www.instagram.com/${slug}`, search: true })
  if (!entity.website) links.push({ ...SOCIAL_CONFIG.website, url: `https://www.google.com/search?q=${encName}+company`, search: true })
  links.push({ ...SOCIAL_CONFIG.crunchbase, url: `https://www.crunchbase.com/organization/${slug}`, search: true })
  links.push({ ...SOCIAL_CONFIG.tracxn, url: `https://tracxn.com/d/companies/${slug}/`, search: true })
  links.push({ ...SOCIAL_CONFIG.google, url: `https://www.google.com/search?q=${encName}+startup+India+news+2024+2025`, search: true })

  return (
    <div className="grid grid-cols-4 gap-1.5">
      {links.filter(l => isValidHttpUrl(l.url)).map(l => (
        <a key={l.label + l.url}
          href={l.url}
          target="_blank"
          rel="noopener noreferrer"
          title={`${l.label}${l.search ? ' (search)' : ''}`}
          className="flex flex-col items-center gap-1 p-2 rounded-lg hover:bg-atlas-surface transition-colors group"
        >
          <span className="text-lg" style={{ color: l.color }}>{l.icon}</span>
          <span className="text-[9px] text-atlas-muted group-hover:text-atlas-text text-center leading-tight">{l.label}</span>
          {l.verified && <span className="w-1 h-1 rounded-full bg-emerald-400" title="Verified" />}
        </a>
      ))}
    </div>
  )
}

function AIAnalysisSection({ entity }) {
  const [analysis, setAnalysis] = useState(null)
  const [loading, setLoading] = useState(false)

  const runAnalysis = async () => {
    setLoading(true)
    try {
      const resp = await fetch('/api/agent/analyze-startup', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          company_name: entity.name,
          sector: Array.isArray(entity.sectors) ? entity.sectors[0] : entity.dpiit_category || '',
          city: entity.city || '',
        }),
      })
      const data = await resp.json()
      setAnalysis(data)
    } catch (e) {
      setAnalysis({ summary: 'Analysis temporarily unavailable. Try again later.', latest_news: [], confidence: 'low' })
    }
    setLoading(false)
  }

  return (
    <div className="border-t border-atlas-border/30 pt-3">
      {!analysis && (
        <button onClick={runAnalysis} disabled={loading}
          className="w-full flex items-center justify-center gap-2 py-2 rounded-lg bg-brand-500/10 text-brand-400 hover:bg-brand-500/20 transition-colors text-xs font-medium">
          {loading ? (
            <><span className="w-3 h-3 border-2 border-brand-400 border-t-transparent rounded-full animate-spin" /> Analyzing...</>
          ) : (
            <><span>πŸ€–</span> AI Web Analysis β€” Latest News & Trends</>
          )}
        </button>
      )}
      {analysis && (
        <div className="space-y-2">
          <div className="flex items-center gap-2 mb-1">
            <span className="text-xs font-semibold text-atlas-muted">πŸ€– AI Analysis</span>
            <span className={`text-[10px] px-1.5 py-0.5 rounded-full ${
              analysis.confidence === 'high' ? 'bg-emerald-500/15 text-emerald-300' :
              analysis.confidence === 'medium' ? 'bg-amber-500/15 text-amber-300' :
              'bg-red-500/15 text-red-300'
            }`}>{analysis.confidence} confidence</span>
          </div>
          <p className="text-xs text-atlas-muted leading-relaxed">{analysis.summary}</p>
          {analysis.latest_news?.length > 0 && (
            <div className="space-y-1.5">
              <span className="text-[10px] font-semibold text-atlas-muted/70 uppercase">Latest News</span>
              {analysis.latest_news.slice(0, 3).map((n, i) => (
                <a key={i} href={n.url} target="_blank" rel="noopener noreferrer"
                  className="block text-xs text-brand-400 hover:text-brand-300 hover:bg-brand-500/5 rounded px-2 py-1.5 transition-colors truncate">
                  β€’ {n.title}
                </a>
              ))}
            </div>
          )}
          <button onClick={() => setAnalysis(null)}
            className="text-[10px] text-atlas-muted/60 hover:text-atlas-muted transition-colors">
            ↻ Run again
          </button>
        </div>
      )}
    </div>
  )
}

export default function EntityDetail({ entity, onClose, onEntityClick, isMobile }) {
  if (!entity) return null

  const e = entity
  const color = ENTITY_COLORS[e.entity_type] || '#64748B'
  const label = ENTITY_LABELS[e.entity_type] || e.entity_type
  const sectors = Array.isArray(e.sectors) ? e.sectors : []
  const investors = Array.isArray(e.investors) ? e.investors : []
  const nearby = Array.isArray(e.nearby) ? e.nearby : []

  return (
    <div className={
      isMobile
        ? "fixed inset-x-0 bottom-0 z-40 max-h-[80vh] pointer-events-auto animate-slide-up"
        : "absolute top-16 right-3 bottom-4 w-96 z-30 animate-slide-in-right pointer-events-none"
    }>
      <div className={
        isMobile
          ? "bg-atlas-bg/95 border-t border-atlas-border rounded-t-2xl shadow-2xl flex flex-col overflow-hidden max-h-[80vh]"
          : "glass rounded-2xl shadow-2xl flex flex-col overflow-hidden pointer-events-auto max-h-full"
      }>
        {/* Header */}
        <div className="relative px-5 pt-5 pb-4 border-b border-atlas-border flex-shrink-0"
             style={{ borderTop: `3px solid ${color}` }}>
          <button onClick={onClose}
            className="absolute top-3 right-3 p-1.5 hover:bg-atlas-surface rounded-lg transition-colors">
            <X size={18} className="text-atlas-muted" />
          </button>

          <div className="flex items-start gap-3">
            <div className="w-12 h-12 rounded-xl bg-atlas-surface flex items-center justify-center text-2xl border border-atlas-border">
              {e.entity_type === 'startup' ? 'πŸš€' : e.entity_type === 'sme' ? '🏒' :
               e.entity_type === 'college_ecell' ? 'πŸŽ“' : e.entity_type === 'incubator' ? 'πŸ§ͺ' : '⚑'}
            </div>
            <div className="flex-1">
              <h2 className="text-lg font-bold text-atlas-text leading-tight">{e.name}</h2>
              <p className="text-sm text-atlas-muted mt-0.5">πŸ“ {e.city}, {e.state}</p>
              <span className="inline-block text-xs font-medium px-2 py-0.5 rounded-full mt-1"
                    style={{ backgroundColor: `${color}20`, color }}>
                {label}
              </span>
            </div>
          </div>

          {/* Badges */}
          <div className="flex flex-wrap gap-1.5 mt-3">
            {e.dpiit_recognized && <Badge text="βœ… Verified (DPIIT)" cls="bg-emerald-500/15 text-emerald-300 ring-1 ring-emerald-500/20" />}
            {e.unicorn_status === 'unicorn' && <Badge text="πŸ¦„ Unicorn" cls="bg-purple-500/20 text-purple-300" />}
            {e.unicorn_status === 'soonicorn' && <Badge text="🌟 Soonicorn" cls="bg-purple-500/15 text-purple-300" />}
            {e.is_women_led && <Badge text="πŸ‘© Women-led" cls="bg-pink-500/15 text-pink-300" />}
            {e.is_rural_impact && <Badge text="🌾 Rural Impact" cls="bg-green-500/15 text-green-300" />}
            {e.is_campus_startup && <Badge text="πŸŽ“ Campus" cls="bg-blue-500/15 text-blue-300" />}
            {e.nsa_winner && <Badge text="πŸ† NSA Winner" cls="bg-amber-500/15 text-amber-300" />}
            {!e.dpiit_recognized && <Badge text="⚠️ Unverified" cls="bg-atlas-surface text-atlas-muted/60" />}
          </div>
        </div>

        {/* Scrollable body */}
        <div className="flex-1 overflow-y-auto px-5 py-4 space-y-4 scrollbar-thin">
          {/* Social Media Links */}
          <div>
            <h4 className="text-xs font-semibold text-atlas-muted uppercase tracking-wider mb-2">Social & Profiles</h4>
            <SocialLinksGrid entity={e} />
          </div>

          {/* Description */}
          {e.description && (
            <p className="text-sm text-atlas-muted leading-relaxed">{e.description}</p>
          )}

          {/* Key Metrics */}
          <div className="grid grid-cols-3 gap-2">
            <MetricCard label="Founded" value={e.founded_year || 'β€”'} icon="πŸ“…" />
            <MetricCard label="Team" value={e.linkedin_team_size || e.employee_count || 'β€”'} icon="πŸ‘₯" />
            <MetricCard label="Funding" value={formatFunding(e.funding_inr)} icon="πŸ’°" />
          </div>

          {/* Sectors */}
          {sectors.length > 0 && (
            <div>
              <h4 className="text-xs font-semibold text-atlas-muted uppercase tracking-wider mb-2">Sectors</h4>
              <div className="flex flex-wrap gap-1.5">
                {sectors.map(s => (
                  <span key={s} className="text-xs bg-atlas-surface text-atlas-muted px-2.5 py-1 rounded-lg border border-atlas-border">{s}</span>
                ))}
              </div>
            </div>
          )}

          {/* Additional Info */}
          <div className="space-y-2">
            {e.stage && <InfoRow label="Stage" value={e.stage.replace('_', ' ').replace(/\b\w/g, l => l.toUpperCase())} />}
            {e.business_model && <InfoRow label="Business Model" value={e.business_model.replace('_', ' ').replace(/\b\w/g, l => l.toUpperCase())} />}
            {e.dpiit_category && <InfoRow label="DPIIT Category" value={e.dpiit_category} />}
            {e.college_name && <InfoRow label="College" value={e.college_name} />}
            {e.funding_stage && <InfoRow label="Funding Stage" value={e.funding_stage} />}
            {e.valuation_usd && <InfoRow label="Valuation" value={`$${(e.valuation_usd / 1e9).toFixed(1)}B`} />}
          </div>

          {/* Investors */}
          {investors.length > 0 && (
            <div>
              <h4 className="text-xs font-semibold text-atlas-muted uppercase tracking-wider mb-2">Investors</h4>
              <div className="flex flex-wrap gap-1.5">
                {investors.map((inv, i) => (
                  <span key={i} className="text-xs bg-indigo-500/10 text-indigo-300 px-2.5 py-1 rounded-lg">{inv}</span>
                ))}
              </div>
            </div>
          )}

          {/* AI Web Analysis */}
          <AIAnalysisSection entity={e} />

          {/* Nearby Entities */}
          {nearby.length > 0 && (
            <div>
              <h4 className="text-xs font-semibold text-atlas-muted uppercase tracking-wider mb-2">Nearby ({nearby.length})</h4>
              <div className="space-y-1">
                {nearby.slice(0, 5).map(n => (
                  <button key={n.id} onClick={() => onEntityClick(n.slug)}
                    className="w-full flex items-center gap-2 px-3 py-2 rounded-lg hover:bg-atlas-surface transition-colors text-left">
                    <span className="text-sm">
                      {n.entity_type === 'startup' ? 'πŸš€' : n.entity_type === 'sme' ? '🏒' : 'πŸ§ͺ'}
                    </span>
                    <div className="flex-1 min-w-0">
                      <p className="text-xs font-medium text-atlas-text truncate">{n.name}</p>
                      <p className="text-[10px] text-atlas-muted">{n.city} β€’ {n.distance_km} km</p>
                    </div>
                  </button>
                ))}
              </div>
            </div>
          )}

          {/* Report Error */}
          <div className="pt-2 border-t border-atlas-border/30">
            <a
              href={`mailto:ram2005.dev@gmail.com?subject=Data%20Error%20Report%3A%20${encodeURIComponent(e.name)}&body=Entity%3A%20${encodeURIComponent(e.name)}%0ASlug%3A%20${encodeURIComponent(e.slug)}%0A%0AWhat%20is%20incorrect%3A%0A`}
              className="flex items-center justify-center gap-1.5 text-xs text-atlas-muted/60 hover:text-red-400 transition-colors py-2 rounded-lg hover:bg-red-500/5"
            >
              <span>🚩</span>
              <span>Report an error in this data</span>
            </a>
          </div>
        </div>
      </div>
    </div>
  )
}

function Badge({ text, cls }) {
  return <span className={`text-xs font-medium px-2 py-0.5 rounded-full ${cls}`}>{text}</span>
}

function MetricCard({ label, value, icon }) {
  return (
    <div className="bg-atlas-surface/60 border border-atlas-border/50 rounded-xl px-3 py-2.5 text-center">
      <span className="text-base">{icon}</span>
      <p className="text-sm font-bold text-atlas-text mt-1 leading-none">{value}</p>
      <p className="text-[10px] text-atlas-muted mt-1 uppercase">{label}</p>
    </div>
  )
}

function InfoRow({ label, value }) {
  return (
    <div className="flex items-center justify-between text-sm">
      <span className="text-atlas-muted">{label}</span>
      <span className="text-atlas-text font-medium">{value}</span>
    </div>
  )
}