'use client'

import { useEffect, useState, useCallback } from 'react'
import { useSearchParams } from 'next/navigation'
import { useStore } from '@/lib/store'
import { api } from '@/lib/api'
import { Sidebar } from '@/components/Sidebar'
import {
  Plus, Shield, Eye, MessageSquare, ExternalLink, Loader2, Trash2,
  CheckCircle, AlertCircle, Copy, DollarSign, X, Tag, Sparkles,
  TrendingUp, Gavel, Target, Menu, Settings, LogOut, Crown, Zap, Coins, Check
} from 'lucide-react'
import Link from 'next/link'
import Image from 'next/image'
import clsx from 'clsx'

// ============================================================================
// TYPES
// ============================================================================

interface Listing {
  id: number
  domain: string
  slug: string
  title: string | null
  description: string | null
  asking_price: number | null
  min_offer: number | null
  currency: string
  price_type: string
  pounce_score: number | null
  verification_status: string
  is_verified: boolean
  status: string
  view_count: number
  inquiry_count: number
  public_url: string
  created_at: string
}

// ============================================================================
// MAIN PAGE
// ============================================================================

export default function MyListingsPage() {
  const { subscription, user, logout, checkAuth } = useStore()
  const searchParams = useSearchParams()
  const prefillDomain = searchParams.get('domain')
  
  const [listings, setListings] = useState<Listing[]>([])
  const [loading, setLoading] = useState(true)
  const [showCreateModal, setShowCreateModal] = useState(false)
  const [deletingId, setDeletingId] = useState<number | null>(null)
  const [menuOpen, setMenuOpen] = useState(false)
  
  const tier = subscription?.tier || 'scout'
  const listingLimits: Record<string, number> = { scout: 0, trader: 5, tycoon: 50 }
  const maxListings = listingLimits[tier] || 0
  const canAddMore = listings.length < maxListings
  const isTycoon = tier === 'tycoon'

  const activeListings = listings.filter(l => l.status === 'active').length
  const totalViews = listings.reduce((sum, l) => sum + l.view_count, 0)
  const totalInquiries = listings.reduce((sum, l) => sum + l.inquiry_count, 0)

  useEffect(() => { checkAuth() }, [checkAuth])
  useEffect(() => { if (prefillDomain) setShowCreateModal(true) }, [prefillDomain])

  const loadListings = useCallback(async () => {
    setLoading(true)
    try {
      const data = await api.getMyListings()
      setListings(data)
    } catch (err) { console.error(err) }
    finally { setLoading(false) }
  }, [])

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

  const handleDelete = async (id: number, domain: string) => {
    if (!confirm(`Delete listing for ${domain}?`)) return
    setDeletingId(id)
    try {
      await api.deleteListing(id)
      await loadListings()
    } catch (err: any) { alert(err.message || 'Failed') }
    finally { setDeletingId(null) }
  }

  const mobileNavItems = [
    { href: '/terminal/radar', label: 'Radar', icon: Target, active: false },
    { href: '/terminal/market', label: 'Market', icon: Gavel, active: false },
    { href: '/terminal/watchlist', label: 'Watch', icon: Eye, active: false },
    { href: '/terminal/intel', label: 'Intel', icon: TrendingUp, active: false },
  ]
  
  const tierName = subscription?.tier_name || subscription?.tier || 'Scout'
  const TierIcon = tierName === 'Tycoon' ? Crown : tierName === 'Trader' ? TrendingUp : Zap
  
  const drawerNavSections = [
    { title: 'Discover', items: [
      { href: '/terminal/radar', label: 'Radar', icon: Target },
      { href: '/terminal/market', label: 'Market', icon: Gavel },
      { href: '/terminal/intel', label: 'Intel', icon: TrendingUp },
    ]},
    { title: 'Manage', items: [
      { href: '/terminal/watchlist', label: 'Watchlist', icon: Eye },
      { href: '/terminal/sniper', label: 'Sniper', icon: Target },
    ]},
    { title: 'Monetize', items: [
      { href: '/terminal/yield', label: 'Yield', icon: Coins },
      { href: '/terminal/listing', label: 'For Sale', icon: Tag, active: true },
    ]}
  ]

  return (
    <div className="min-h-screen bg-[#020202]">
      <div className="hidden lg:block"><Sidebar /></div>
      
      <main className="lg:pl-[240px]">
        {/* MOBILE HEADER */}
        <header className="lg:hidden sticky top-0 z-40 bg-[#020202]/95 backdrop-blur-md border-b border-white/[0.08]" style={{ paddingTop: 'env(safe-area-inset-top)' }}>
          <div className="px-4 py-3">
            <div className="flex items-center justify-between mb-3">
              <div className="flex items-center gap-2">
                <div className="w-1.5 h-1.5 bg-accent animate-pulse" />
                <span className="text-[10px] font-mono tracking-[0.2em] text-accent uppercase">For Sale</span>
              </div>
              <span className="text-[10px] font-mono text-white/40">{listings.length}/{maxListings}</span>
            </div>
            <div className="grid grid-cols-3 gap-2">
              <div className="bg-accent/[0.05] border border-accent/20 p-2">
                <div className="text-lg font-bold text-accent tabular-nums">{activeListings}</div>
                <div className="text-[9px] font-mono text-accent/60 uppercase tracking-wider">Active</div>
              </div>
              <div className="bg-white/[0.02] border border-white/[0.08] p-2">
                <div className="text-lg font-bold text-white tabular-nums">{totalViews}</div>
                <div className="text-[9px] font-mono text-white/30 uppercase tracking-wider">Views</div>
              </div>
              <div className="bg-white/[0.02] border border-white/[0.08] p-2">
                <div className="text-lg font-bold text-white tabular-nums">{totalInquiries}</div>
                <div className="text-[9px] font-mono text-white/30 uppercase tracking-wider">Leads</div>
              </div>
            </div>
          </div>
        </header>

        {/* DESKTOP HEADER */}
        <section className="hidden lg:block px-10 pt-10 pb-6">
          <div className="flex items-end justify-between gap-6">
            <div className="space-y-3">
              <div className="flex items-center gap-2">
                <div className="w-1.5 h-1.5 bg-accent animate-pulse" />
                <span className="text-[10px] font-mono tracking-[0.2em] text-accent uppercase">Domain Marketplace</span>
              </div>
              <h1 className="font-display text-[2.5rem] leading-[1] tracking-[-0.02em]">
                <span className="text-white">For Sale</span>
                <span className="text-white/30 ml-3 font-mono text-[2rem]">{listings.length}/{maxListings}</span>
              </h1>
            </div>
            <div className="flex items-center gap-6">
              <div className="flex gap-8">
                <div className="text-right">
                  <div className="text-2xl font-bold text-accent font-mono">{activeListings}</div>
                  <div className="text-[9px] font-mono text-white/30 uppercase">Active</div>
                </div>
                <div className="text-right">
                  <div className="text-2xl font-bold text-white font-mono">{totalViews}</div>
                  <div className="text-[9px] font-mono text-white/30 uppercase">Views</div>
                </div>
                <div className="text-right">
                  <div className="text-2xl font-bold text-white font-mono">{totalInquiries}</div>
                  <div className="text-[9px] font-mono text-white/30 uppercase">Leads</div>
                </div>
              </div>
              <button onClick={() => setShowCreateModal(true)} disabled={!canAddMore}
                className={clsx("flex items-center gap-2 px-4 py-2.5 text-xs font-bold uppercase tracking-wider", canAddMore ? "bg-accent text-black hover:bg-white" : "bg-white/10 text-white/40 cursor-not-allowed")}>
                <Plus className="w-4 h-4" />New Listing
              </button>
            </div>
          </div>
        </section>

        {/* ADD BUTTON MOBILE */}
        <section className="lg:hidden px-4 py-3 border-b border-white/[0.08]">
          <button onClick={() => setShowCreateModal(true)} disabled={!canAddMore}
            className={clsx("w-full flex items-center justify-center gap-2 py-3 text-xs font-bold uppercase tracking-wider", canAddMore ? "bg-accent text-black" : "bg-white/10 text-white/40")}>
            <Plus className="w-4 h-4" />New Listing
          </button>
        </section>

        {/* CONTENT */}
        <section className="px-4 lg:px-10 py-4 pb-28 lg:pb-10">
          {loading ? (
            <div className="flex items-center justify-center py-20">
              <Loader2 className="w-6 h-6 text-accent animate-spin" />
            </div>
          ) : listings.length === 0 ? (
            <div className="text-center py-16 border border-dashed border-white/[0.08]">
              <Tag className="w-8 h-8 text-white/10 mx-auto mb-3" />
              <p className="text-white/40 text-sm font-mono mb-2">No listings yet</p>
              <p className="text-white/25 text-xs font-mono">Create your first listing</p>
            </div>
          ) : (
            <div className="space-y-px bg-white/[0.04] border border-white/[0.08]">
              {/* Header */}
              <div className="hidden lg:grid grid-cols-[1fr_80px_80px_60px_60px_80px] gap-4 px-3 py-2 text-[10px] font-mono text-white/40 uppercase tracking-wider border-b border-white/[0.08]">
                <div>Domain</div>
                <div className="text-right">Price</div>
                <div className="text-center">Status</div>
                <div className="text-right">Views</div>
                <div className="text-right">Leads</div>
                <div className="text-right">Actions</div>
              </div>
              
              {listings.map((listing) => (
                <div key={listing.id} className="bg-[#020202] hover:bg-white/[0.02] transition-all group">
                  {/* Mobile */}
                  <div className="lg:hidden p-3">
                    <div className="flex items-center justify-between mb-2">
                      <div className="flex items-center gap-3">
                        <div className="w-8 h-8 bg-white/[0.02] border border-white/[0.06] flex items-center justify-center">
                          {listing.is_verified ? <Shield className="w-4 h-4 text-accent" /> : <Tag className="w-4 h-4 text-white/30" />}
                        </div>
                        <span className="text-sm font-bold text-white font-mono">{listing.domain}</span>
                      </div>
                      <span className={clsx("px-1.5 py-0.5 text-[9px] font-mono border",
                        listing.status === 'active' ? "bg-accent/10 text-accent border-accent/20" : "bg-white/5 text-white/40 border-white/10"
                      )}>{listing.status}</span>
                    </div>
                    <div className="flex justify-between text-[10px] font-mono text-white/40">
                      <span>${listing.asking_price?.toLocaleString() || 'Negotiable'}</span>
                      <span>{listing.view_count} views · {listing.inquiry_count} leads</span>
                    </div>
                    <div className="flex gap-2 mt-2">
                      <a href={listing.public_url} target="_blank" className="flex-1 py-2 border border-white/[0.08] text-[10px] font-mono text-white/40 flex items-center justify-center gap-1">
                        <ExternalLink className="w-3 h-3" />View
                      </a>
                      <button onClick={() => handleDelete(listing.id, listing.domain)} disabled={deletingId === listing.id}
                        className="px-4 py-2 border border-white/[0.08] text-white/40 hover:text-rose-400">
                        {deletingId === listing.id ? <Loader2 className="w-4 h-4 animate-spin" /> : <Trash2 className="w-4 h-4" />}
                      </button>
                    </div>
                  </div>
                  
                  {/* Desktop */}
                  <div className="hidden lg:grid grid-cols-[1fr_80px_80px_60px_60px_80px] gap-4 items-center px-3 py-3">
                    <div className="flex items-center gap-3">
                      <div className="w-8 h-8 bg-white/[0.02] border border-white/[0.06] flex items-center justify-center">
                        {listing.is_verified ? <Shield className="w-4 h-4 text-accent" /> : <Tag className="w-4 h-4 text-white/30" />}
                      </div>
                      <div>
                        <span className="text-sm font-bold text-white font-mono group-hover:text-accent transition-colors">{listing.domain}</span>
                        {isTycoon && <span className="ml-2 px-1 py-0.5 text-[8px] font-mono bg-amber-400/10 text-amber-400 border border-amber-400/20">Featured</span>}
                      </div>
                    </div>
                    <div className="text-right text-sm font-bold font-mono text-accent">${listing.asking_price?.toLocaleString() || '—'}</div>
                    <div className="flex justify-center">
                      <span className={clsx("px-1.5 py-0.5 text-[9px] font-mono border",
                        listing.status === 'active' ? "bg-accent/10 text-accent border-accent/20" : "bg-white/5 text-white/40 border-white/10"
                      )}>{listing.status}</span>
                    </div>
                    <div className="text-right text-xs font-mono text-white/60">{listing.view_count}</div>
                    <div className="text-right text-xs font-mono text-white/60">{listing.inquiry_count}</div>
                    <div className="flex items-center justify-end gap-1 opacity-50 group-hover:opacity-100 transition-opacity">
                      <a href={listing.public_url} target="_blank" className="w-7 h-7 flex items-center justify-center border border-white/10 text-white/30 hover:text-accent">
                        <ExternalLink className="w-3.5 h-3.5" />
                      </a>
                      <button onClick={() => handleDelete(listing.id, listing.domain)} disabled={deletingId === listing.id}
                        className="w-7 h-7 flex items-center justify-center border border-white/10 text-white/30 hover:text-rose-400">
                        {deletingId === listing.id ? <Loader2 className="w-3.5 h-3.5 animate-spin" /> : <Trash2 className="w-3.5 h-3.5" />}
                      </button>
                    </div>
                  </div>
                </div>
              ))}
            </div>
          )}

          {!canAddMore && (
            <div className="mt-6 p-6 border border-white/[0.08] bg-white/[0.02] text-center">
              <Crown className="w-6 h-6 text-amber-400 mx-auto mb-3" />
              <h3 className="text-sm font-bold text-white mb-1">Listing Limit Reached</h3>
              <p className="text-xs font-mono text-white/40 mb-4">Upgrade for more listings</p>
              <Link href="/pricing" className="inline-flex items-center gap-2 px-4 py-2 bg-amber-400 text-black text-xs font-bold uppercase tracking-wider">
                <Sparkles className="w-3 h-3" />Upgrade
              </Link>
            </div>
          )}
        </section>

        {/* MOBILE BOTTOM NAV */}
        <nav className="lg:hidden fixed bottom-0 left-0 right-0 z-50 bg-[#020202] border-t border-white/[0.08]" style={{ paddingBottom: 'env(safe-area-inset-bottom)' }}>
          <div className="flex items-stretch h-14">
            {mobileNavItems.map((item) => (
              <Link key={item.href} href={item.href} className={clsx("flex-1 flex flex-col items-center justify-center gap-0.5 relative", item.active ? "text-accent" : "text-white/40")}>
                {item.active && <div className="absolute top-0 left-1/2 -translate-x-1/2 w-10 h-0.5 bg-accent" />}
                <item.icon className="w-5 h-5" />
                <span className="text-[9px] font-mono uppercase tracking-wider">{item.label}</span>
              </Link>
            ))}
            <button onClick={() => setMenuOpen(true)} className="flex-1 flex flex-col items-center justify-center gap-0.5 text-white/40">
              <Menu className="w-5 h-5" /><span className="text-[9px] font-mono uppercase tracking-wider">Menu</span>
            </button>
          </div>
        </nav>

        {/* DRAWER */}
        {menuOpen && (
          <div className="lg:hidden fixed inset-0 z-[100]">
            <div className="absolute inset-0 bg-black/80" onClick={() => setMenuOpen(false)} />
            <div className="absolute top-0 right-0 bottom-0 w-[80%] max-w-[300px] bg-[#0A0A0A] border-l border-white/[0.08] flex flex-col" style={{ paddingTop: 'env(safe-area-inset-top)', paddingBottom: 'env(safe-area-inset-bottom)' }}>
              <div className="flex items-center justify-between p-4 border-b border-white/[0.08]">
                <div className="flex items-center gap-3">
                  <Image src="/pounce-puma.png" alt="Pounce" width={28} height={28} className="object-contain" />
                  <div><h2 className="text-sm font-bold text-white">POUNCE</h2><p className="text-[9px] text-white/40 font-mono uppercase">Terminal v1.0</p></div>
                </div>
                <button onClick={() => setMenuOpen(false)} className="w-8 h-8 flex items-center justify-center border border-white/10 text-white/60"><X className="w-4 h-4" /></button>
              </div>
              <div className="flex-1 overflow-y-auto py-4">
                {drawerNavSections.map((section) => (
                  <div key={section.title} className="mb-4">
                    <div className="flex items-center gap-2 px-4 mb-2"><div className="w-1 h-3 bg-accent" /><span className="text-[9px] font-bold text-white/30 uppercase tracking-[0.2em]">{section.title}</span></div>
                    {section.items.map((item: any) => (
                      <Link key={item.href} href={item.href} onClick={() => setMenuOpen(false)} className={clsx("flex items-center gap-3 px-4 py-2.5 border-l-2 border-transparent", item.active ? "text-accent border-accent bg-white/[0.02]" : "text-white/60")}>
                        <item.icon className="w-4 h-4 text-white/30" /><span className="text-sm font-medium flex-1">{item.label}</span>
                      </Link>
                    ))}
                  </div>
                ))}
                <div className="pt-3 border-t border-white/[0.08] mx-4">
                  <Link href="/terminal/settings" onClick={() => setMenuOpen(false)} className="flex items-center gap-3 py-2.5 text-white/50"><Settings className="w-4 h-4" /><span className="text-sm">Settings</span></Link>
                  {user?.is_admin && <Link href="/admin" onClick={() => setMenuOpen(false)} className="flex items-center gap-3 py-2.5 text-amber-500/70"><Shield className="w-4 h-4" /><span className="text-sm">Admin</span></Link>}
                </div>
              </div>
              <div className="p-4 bg-white/[0.02] border-t border-white/[0.08]">
                <div className="flex items-center gap-3 mb-3">
                  <div className="w-8 h-8 bg-accent/10 border border-accent/20 flex items-center justify-center"><TierIcon className="w-4 h-4 text-accent" /></div>
                  <div className="flex-1 min-w-0"><p className="text-sm font-bold text-white truncate">{user?.name || user?.email?.split('@')[0] || 'User'}</p><p className="text-[9px] font-mono text-white/40 uppercase">{tierName}</p></div>
                </div>
                {tierName === 'Scout' && <Link href="/pricing" onClick={() => setMenuOpen(false)} className="flex items-center justify-center gap-2 w-full py-2.5 bg-accent text-black text-xs font-bold uppercase mb-2"><Sparkles className="w-3 h-3" />Upgrade</Link>}
                <button onClick={() => { logout(); setMenuOpen(false) }} className="flex items-center justify-center gap-2 w-full py-2 border border-white/10 text-white/40 text-[10px] font-mono uppercase"><LogOut className="w-3 h-3" />Sign out</button>
              </div>
            </div>
          </div>
        )}
      </main>

      {/* CREATE MODAL */}
      {showCreateModal && (
        <CreateListingModal
          onClose={() => setShowCreateModal(false)}
          onSuccess={() => { loadListings(); setShowCreateModal(false) }}
          prefillDomain={prefillDomain || ''}
        />
      )}
    </div>
  )
}

// ============================================================================
// CREATE MODAL (simplified)
// ============================================================================

function CreateListingModal({ onClose, onSuccess, prefillDomain }: { onClose: () => void; onSuccess: () => void; prefillDomain: string }) {
  const [domain, setDomain] = useState(prefillDomain)
  const [price, setPrice] = useState('')
  const [loading, setLoading] = useState(false)
  const [error, setError] = useState<string | null>(null)

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault()
    if (!domain.trim()) return
    setLoading(true)
    setError(null)
    try {
      await api.createListing({ domain: domain.trim(), asking_price: price ? parseFloat(price) : null, currency: 'USD', price_type: price ? 'fixed' : 'negotiable' })
      onSuccess()
    } catch (err: any) { setError(err.message || 'Failed') }
    finally { setLoading(false) }
  }

  return (
    <div className="fixed inset-0 z-[110] bg-black/80 flex items-center justify-center p-4" onClick={onClose}>
      <div className="w-full max-w-md bg-[#0A0A0A] border border-white/[0.08]" onClick={(e) => e.stopPropagation()}>
        <div className="flex items-center justify-between p-4 border-b border-white/[0.08]">
          <div className="flex items-center gap-2"><Tag className="w-4 h-4 text-accent" /><span className="text-xs font-mono text-accent uppercase tracking-wider">New Listing</span></div>
          <button onClick={onClose} className="w-8 h-8 flex items-center justify-center border border-white/10 text-white/40"><X className="w-4 h-4" /></button>
        </div>
        <form onSubmit={handleSubmit} className="p-4 space-y-4">
          {error && <div className="p-2 bg-rose-500/10 border border-rose-500/20 text-rose-400 text-xs">{error}</div>}
          <div>
            <label className="block text-[9px] font-mono text-white/40 uppercase mb-1.5">Domain *</label>
            <input type="text" value={domain} onChange={(e) => setDomain(e.target.value)} required
              className="w-full px-3 py-2.5 bg-white/5 border border-white/10 text-white text-sm font-mono placeholder:text-white/20 outline-none focus:border-accent/50" placeholder="example.com" />
          </div>
          <div>
            <label className="block text-[9px] font-mono text-white/40 uppercase mb-1.5">Asking Price (USD)</label>
            <input type="number" value={price} onChange={(e) => setPrice(e.target.value)} min="0"
              className="w-full px-3 py-2.5 bg-white/5 border border-white/10 text-white text-sm font-mono placeholder:text-white/20 outline-none focus:border-accent/50" placeholder="Leave empty for negotiable" />
          </div>
          <div className="flex gap-3 pt-2">
            <button type="button" onClick={onClose} className="flex-1 py-2.5 border border-white/10 text-white/60 text-xs font-mono uppercase">Cancel</button>
            <button type="submit" disabled={loading || !domain.trim()} className="flex-1 flex items-center justify-center gap-2 py-2.5 bg-accent text-black text-xs font-bold uppercase disabled:opacity-50">
              {loading ? <Loader2 className="w-4 h-4 animate-spin" /> : <CheckCircle className="w-4 h-4" />}Create
            </button>
          </div>
        </form>
      </div>
    </div>
  )
}
