'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, ExternalLink, Loader2, Trash2,
  CheckCircle, AlertCircle, Copy, X, Tag, Sparkles,
  TrendingUp, Gavel, Target, Menu, Settings, LogOut, Crown, Zap, Coins,
  ArrowRight, RefreshCw, Globe, Lock, Briefcase
} 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 [showCreateWizard, setShowCreateWizard] = useState(false)
  const [selectedListing, setSelectedListing] = useState<Listing | null>(null)
  const [deletingId, setDeletingId] = useState<number | null>(null)
  const [menuOpen, setMenuOpen] = useState(false)
  
  const tier = subscription?.tier || 'scout'
  const isScout = tier === 'scout'
  const listingLimits: Record<string, number> = { scout: 0, trader: 5, tycoon: 50 }
  const maxListings = listingLimits[tier] || 0
  const canAddMore = listings.length < maxListings && !isScout
  const isTycoon = tier === 'tycoon'

  const activeListings = listings.filter(l => l.status === 'active').length
  const draftListings = listings.filter(l => l.status === 'draft').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 && !isScout) setShowCreateWizard(true) }, [prefillDomain, isScout])

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

  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 handlePublish = async (listing: Listing) => {
    if (!listing.is_verified) {
      setSelectedListing(listing)
      return
    }
    try {
      await api.updateListing(listing.id, { status: 'active' })
      await loadListings()
    } catch (err: any) { alert(err.message || 'Failed to publish') }
  }

  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/portfolio', label: 'Portfolio', icon: Briefcase },
      { 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 },
    ]}
  ]

  // ============================================================================
  // SCOUT UPGRADE PROMPT (Feature not available for free tier)
  // ============================================================================
  if (isScout) {
    return (
      <div className="min-h-screen bg-[#020202]">
        <div className="hidden lg:block"><Sidebar /></div>
        <main className="lg:pl-[240px]">
          <div className="min-h-screen flex items-center justify-center p-8">
            <div className="max-w-md text-center">
              <div className="w-20 h-20 bg-amber-400/10 border border-amber-400/20 flex items-center justify-center mx-auto mb-6">
                <Lock className="w-10 h-10 text-amber-400" />
              </div>
              <h1 className="font-display text-3xl text-white mb-4">For Sale</h1>
              <p className="text-white/50 text-sm font-mono mb-2">
                List domains on Pounce Direct.
              </p>
              <p className="text-white/30 text-xs font-mono mb-8">
                0% commission. DNS-verified ownership. Direct buyer contact.
              </p>
              
              <div className="bg-white/[0.02] border border-white/[0.08] p-6 mb-6">
                <div className="flex items-center justify-between mb-4 pb-4 border-b border-white/[0.08]">
                  <div className="flex items-center gap-3">
                    <TrendingUp className="w-5 h-5 text-accent" />
                    <span className="text-sm font-bold text-white">Trader</span>
                  </div>
                  <span className="text-accent font-mono text-sm">$9/mo</span>
                </div>
                <ul className="text-left space-y-2 text-sm text-white/60 mb-4">
                  <li className="flex items-center gap-2"><CheckCircle className="w-4 h-4 text-accent" />5 Active Listings</li>
                  <li className="flex items-center gap-2"><CheckCircle className="w-4 h-4 text-accent" />DNS Ownership Verification</li>
                  <li className="flex items-center gap-2"><CheckCircle className="w-4 h-4 text-accent" />Direct Buyer Contact</li>
                </ul>
              </div>
              
              <div className="bg-white/[0.02] border border-amber-400/20 p-6 mb-8">
                <div className="flex items-center justify-between mb-4 pb-4 border-b border-white/[0.08]">
                  <div className="flex items-center gap-3">
                    <Crown className="w-5 h-5 text-amber-400" />
                    <span className="text-sm font-bold text-white">Tycoon</span>
                  </div>
                  <span className="text-amber-400 font-mono text-sm">$29/mo</span>
                </div>
                <ul className="text-left space-y-2 text-sm text-white/60">
                  <li className="flex items-center gap-2"><CheckCircle className="w-4 h-4 text-amber-400" />50 Active Listings</li>
                  <li className="flex items-center gap-2"><CheckCircle className="w-4 h-4 text-amber-400" />Featured Placement</li>
                  <li className="flex items-center gap-2"><CheckCircle className="w-4 h-4 text-amber-400" />Priority in Market Feed</li>
                </ul>
              </div>
              
              <Link href="/pricing" className="inline-flex items-center gap-2 px-6 py-3 bg-accent text-black text-xs font-bold uppercase tracking-wider hover:bg-white transition-colors">
                <Sparkles className="w-4 h-4" />Upgrade
              </Link>
            </div>
          </div>
        </main>
      </div>
    )
  }

  // ============================================================================
  // MAIN LISTING VIEW (For Trader & Tycoon)
  // ============================================================================
  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">Pounce Direct</span>
              </div>
              <span className="text-[10px] font-mono text-white/40">{listings.length}/{maxListings}</span>
            </div>
            <div className="grid grid-cols-4 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">Live</div>
              </div>
              <div className="bg-amber-400/[0.05] border border-amber-400/20 p-2">
                <div className="text-lg font-bold text-amber-400 tabular-nums">{draftListings}</div>
                <div className="text-[9px] font-mono text-amber-400/60 uppercase tracking-wider">Draft</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">Pounce Direct</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>
              <p className="text-sm text-white/40 font-mono mt-2 max-w-lg">
                List your domains for sale. 0% commission, verified ownership, direct buyer contact.
              </p>
            </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">Live</div>
                </div>
                <div className="text-right">
                  <div className="text-2xl font-bold text-amber-400 font-mono">{draftListings}</div>
                  <div className="text-[9px] font-mono text-white/30 uppercase">Draft</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={() => setShowCreateWizard(true)} disabled={!canAddMore}
                className={clsx("flex items-center gap-2 px-4 py-2.5 text-xs font-bold uppercase tracking-wider transition-colors", 
                  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={() => setShowCreateWizard(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 mb-4">Create your first listing to start selling</p>
              <button onClick={() => setShowCreateWizard(true)} className="inline-flex items-center gap-2 px-4 py-2 bg-accent text-black text-xs font-bold uppercase">
                <Plus className="w-4 h-4" />Create Listing
              </button>
            </div>
          ) : (
            <div className="space-y-px bg-white/[0.04] border border-white/[0.08]">
              {/* Header */}
              <div className="hidden lg:grid grid-cols-[1fr_100px_100px_60px_60px_120px] 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) => (
                <ListingRow
                  key={listing.id}
                  listing={listing}
                  isTycoon={isTycoon}
                  onDelete={() => handleDelete(listing.id, listing.domain)}
                  onVerify={() => setSelectedListing(listing)}
                  onPublish={() => handlePublish(listing)}
                  isDeleting={deletingId === listing.id}
                />
              ))}
            </div>
          )}

          {!canAddMore && listings.length >= maxListings && (
            <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">
                {tier === 'trader' ? 'Upgrade to Tycoon for 50 listings' : 'Contact us for enterprise plans'}
              </p>
              {tier === 'trader' && (
                <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 to Tycoon
                </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 && (
          <MobileDrawer
            user={user}
            tierName={tierName}
            TierIcon={TierIcon}
            drawerNavSections={drawerNavSections}
            onClose={() => setMenuOpen(false)}
            onLogout={() => { logout(); setMenuOpen(false) }}
          />
        )}
      </main>

      {/* CREATE WIZARD (3-Step) */}
      {showCreateWizard && (
        <CreateListingWizard
          onClose={() => setShowCreateWizard(false)}
          onSuccess={() => { loadListings(); setShowCreateWizard(false) }}
          prefillDomain={prefillDomain || ''}
        />
      )}

      {/* DNS VERIFICATION MODAL */}
      {selectedListing && (
        <DnsVerificationModal
          listing={selectedListing}
          onClose={() => setSelectedListing(null)}
          onVerified={() => { loadListings(); setSelectedListing(null) }}
        />
      )}
    </div>
  )
}

// ============================================================================
// LISTING ROW COMPONENT
// ============================================================================

function ListingRow({ 
  listing, 
  isTycoon, 
  onDelete, 
  onVerify, 
  onPublish, 
  isDeleting 
}: { 
  listing: Listing
  isTycoon: boolean
  onDelete: () => void
  onVerify: () => void
  onPublish: () => void
  isDeleting: boolean
}) {
  const isDraft = listing.status === 'draft'
  const isActive = listing.status === 'active'
  const needsVerification = !listing.is_verified

  return (
    <div 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={clsx("w-8 h-8 border flex items-center justify-center",
              listing.is_verified ? "bg-accent/10 border-accent/20" : "bg-white/[0.02] border-white/[0.06]"
            )}>
              {listing.is_verified ? <Shield className="w-4 h-4 text-accent" /> : <AlertCircle className="w-4 h-4 text-amber-400" />}
            </div>
            <div>
              <span className="text-sm font-bold text-white font-mono">{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>
          <span className={clsx("px-1.5 py-0.5 text-[9px] font-mono border",
            isActive ? "bg-accent/10 text-accent border-accent/20" : 
            isDraft ? "bg-amber-400/10 text-amber-400 border-amber-400/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 mb-2">
          <span>${listing.asking_price?.toLocaleString() || 'Make Offer'}</span>
          <span>{listing.view_count} views · {listing.inquiry_count} leads</span>
        </div>
        <div className="flex gap-2">
          {isDraft && needsVerification && (
            <button onClick={onVerify} className="flex-1 py-2 bg-amber-400/10 border border-amber-400/20 text-amber-400 text-[10px] font-mono uppercase flex items-center justify-center gap-1">
              <Shield className="w-3 h-3" />Verify DNS
            </button>
          )}
          {isDraft && !needsVerification && (
            <button onClick={onPublish} className="flex-1 py-2 bg-accent text-black text-[10px] font-bold uppercase flex items-center justify-center gap-1">
              <Globe className="w-3 h-3" />Publish
            </button>
          )}
          {isActive && (
            <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={onDelete} disabled={isDeleting}
            className="px-4 py-2 border border-white/[0.08] text-white/40 hover:text-rose-400">
            {isDeleting ? <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_100px_100px_60px_60px_120px] gap-4 items-center px-3 py-3">
        <div className="flex items-center gap-3">
          <div className={clsx("w-8 h-8 border flex items-center justify-center",
            listing.is_verified ? "bg-accent/10 border-accent/20" : "bg-white/[0.02] border-white/[0.06]"
          )}>
            {listing.is_verified ? <Shield className="w-4 h-4 text-accent" /> : <AlertCircle className="w-4 h-4 text-amber-400" />}
          </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>}
            {!listing.is_verified && <span className="ml-2 text-[9px] text-amber-400/60 font-mono">Unverified</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-2 py-1 text-[9px] font-mono border",
            isActive ? "bg-accent/10 text-accent border-accent/20" : 
            isDraft ? "bg-amber-400/10 text-amber-400 border-amber-400/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">
          {isDraft && needsVerification && (
            <button onClick={onVerify} className="px-2 py-1 bg-amber-400/10 border border-amber-400/20 text-amber-400 text-[9px] font-mono uppercase hover:bg-amber-400/20 transition-colors">
              Verify
            </button>
          )}
          {isDraft && !needsVerification && (
            <button onClick={onPublish} className="px-2 py-1 bg-accent text-black text-[9px] font-bold uppercase hover:bg-white transition-colors">
              Publish
            </button>
          )}
          {isActive && (
            <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={onDelete} disabled={isDeleting}
            className="w-7 h-7 flex items-center justify-center border border-white/10 text-white/30 hover:text-rose-400">
            {isDeleting ? <Loader2 className="w-3.5 h-3.5 animate-spin" /> : <Trash2 className="w-3.5 h-3.5" />}
          </button>
        </div>
      </div>
    </div>
  )
}

// ============================================================================
// CREATE LISTING WIZARD (3-Step Process)
// ============================================================================

function CreateListingWizard({ onClose, onSuccess, prefillDomain }: { 
  onClose: () => void
  onSuccess: () => void
  prefillDomain: string 
}) {
  const [step, setStep] = useState(1)
  const [domain, setDomain] = useState(prefillDomain)
  const [price, setPrice] = useState('')
  const [priceType, setPriceType] = useState<'fixed' | 'negotiable'>('negotiable')
  const [loading, setLoading] = useState(false)
  const [error, setError] = useState<string | null>(null)
  const [createdListing, setCreatedListing] = useState<any>(null)
  const [verificationData, setVerificationData] = useState<any>(null)
  const [verifying, setVerifying] = useState(false)
  const [verified, setVerified] = useState(false)
  
  // Portfolio domains (for dropdown selection)
  const [portfolioDomains, setPortfolioDomains] = useState<{ id: number; domain: string }[]>([])
  const [loadingDomains, setLoadingDomains] = useState(true)
  
  useEffect(() => {
    const fetchPortfolioDomains = async () => {
      setLoadingDomains(true)
      try {
        const domains = await api.getPortfolio()
        // Filter out sold domains
        setPortfolioDomains(domains.filter(d => !d.is_sold).map(d => ({ id: d.id, domain: d.domain })))
      } catch (err) {
        console.error('Failed to load portfolio domains:', err)
      } finally {
        setLoadingDomains(false)
      }
    }
    fetchPortfolioDomains()
  }, [])

  // Step 1: Create listing
  const handleCreateListing = async () => {
    if (!domain.trim()) return
    setLoading(true)
    setError(null)
    try {
      const listing = await api.createListing({ 
        domain: domain.trim(), 
        asking_price: price ? parseFloat(price) : null, 
        currency: 'USD', 
        price_type: priceType 
      })
      setCreatedListing(listing)
      
      // Check if domain was already verified in portfolio
      if (listing.is_verified) {
        // Skip verification step, go directly to publish
        setVerified(true)
        setStep(3)
      } else {
        // Start DNS verification
        const verification = await api.startDnsVerification(listing.id)
        setVerificationData(verification)
        setStep(2)
      }
    } catch (err: any) { 
      setError(err.message || 'Failed to create listing') 
    } finally { 
      setLoading(false) 
    }
  }

  // Step 2: Check DNS verification
  const handleCheckVerification = async () => {
    if (!createdListing) return
    setVerifying(true)
    try {
      const result = await api.checkDnsVerification(createdListing.id)
      if (result.verified) {
        setVerified(true)
        setStep(3)
      } else {
        setError(result.message || 'DNS record not found yet. Please wait for propagation.')
      }
    } catch (err: any) { 
      setError(err.message || 'Verification check failed') 
    } finally { 
      setVerifying(false) 
    }
  }

  // Step 3: Publish
  const handlePublish = async () => {
    if (!createdListing) return
    setLoading(true)
    try {
      await api.updateListing(createdListing.id, { status: 'active' })
      onSuccess()
    } catch (err: any) { 
      setError(err.message || 'Failed to publish') 
    } finally { 
      setLoading(false) 
    }
  }

  const copyToClipboard = (text: string) => {
    navigator.clipboard.writeText(text)
  }

  return (
    <div className="fixed inset-0 z-[110] bg-black/90 flex items-center justify-center p-4" onClick={onClose}>
      <div className="w-full max-w-lg bg-[#0A0A0A] border border-white/[0.08]" onClick={(e) => e.stopPropagation()}>
        {/* Header */}
        <div className="flex items-center justify-between p-4 border-b border-white/[0.08]">
          <div className="flex items-center gap-3">
            <Tag className="w-4 h-4 text-accent" />
            <span className="text-xs font-mono text-accent uppercase tracking-wider">New Listing</span>
          </div>
          <div className="flex items-center gap-4">
            {/* Step Indicators */}
            <div className="flex items-center gap-2">
              {[1, 2, 3].map((s) => (
                <div key={s} className={clsx(
                  "w-6 h-6 flex items-center justify-center text-[10px] font-mono border",
                  step === s ? "bg-accent text-black border-accent" :
                  step > s ? "bg-accent/20 text-accent border-accent/40" :
                  "bg-white/5 text-white/30 border-white/10"
                )}>
                  {step > s ? <CheckCircle className="w-3 h-3" /> : s}
                </div>
              ))}
            </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>
        </div>

        {/* Step Content */}
        <div className="p-6">
          {error && (
            <div className="mb-4 p-3 bg-rose-500/10 border border-rose-500/20 text-rose-400 text-xs font-mono">
              {error}
            </div>
          )}

          {/* STEP 1: Domain & Price */}
          {step === 1 && (
            <div className="space-y-6">
              <div className="text-center mb-6">
                <h2 className="text-xl font-display text-white mb-2">Enter Domain Details</h2>
                <p className="text-xs font-mono text-white/40">Step 1 of 3: Set your domain and price</p>
              </div>

              <div>
                <label className="block text-[10px] font-mono text-white/40 uppercase mb-2">Select Domain from Portfolio *</label>
                {loadingDomains ? (
                  <div className="flex items-center justify-center py-4">
                    <Loader2 className="w-5 h-5 text-accent animate-spin" />
                  </div>
                ) : portfolioDomains.length === 0 ? (
                  <div className="p-4 bg-amber-400/5 border border-amber-400/20 text-center">
                    <AlertCircle className="w-6 h-6 text-amber-400 mx-auto mb-2" />
                    <p className="text-xs text-amber-400 font-mono mb-3">No domains in your portfolio</p>
                    <a href="/terminal/portfolio" className="inline-flex items-center gap-2 px-4 py-2 bg-accent text-black text-xs font-bold uppercase">
                      Add Domains to Portfolio
                    </a>
                  </div>
                ) : (
                  <select 
                    value={domain} 
                    onChange={(e) => setDomain(e.target.value)}
                    className="w-full px-4 py-3 bg-white/5 border border-white/10 text-white text-sm font-mono outline-none focus:border-accent/50"
                  >
                    <option value="">— Select a domain —</option>
                    {portfolioDomains.map(d => (
                      <option key={d.id} value={d.domain}>{d.domain}</option>
                    ))}
                  </select>
                )}
                <p className="text-[9px] font-mono text-white/30 mt-2">
                  Only domains from your portfolio can be listed. DNS verification required after listing.
                </p>
              </div>

              <div>
                <label className="block text-[10px] font-mono text-white/40 uppercase mb-2">Price Type</label>
                <div className="grid grid-cols-2 gap-3">
                  <button 
                    type="button" 
                    onClick={() => setPriceType('fixed')}
                    className={clsx("py-3 border text-xs font-mono uppercase transition-colors",
                      priceType === 'fixed' ? "bg-accent/10 border-accent text-accent" : "border-white/10 text-white/40 hover:border-white/30"
                    )}>
                    Fixed Price
                  </button>
                  <button 
                    type="button" 
                    onClick={() => setPriceType('negotiable')}
                    className={clsx("py-3 border text-xs font-mono uppercase transition-colors",
                      priceType === 'negotiable' ? "bg-accent/10 border-accent text-accent" : "border-white/10 text-white/40 hover:border-white/30"
                    )}>
                    Make Offer
                  </button>
                </div>
              </div>

              {priceType === 'fixed' && (
                <div>
                  <label className="block text-[10px] font-mono text-white/40 uppercase mb-2">Asking Price (USD)</label>
                  <input 
                    type="number" 
                    value={price} 
                    onChange={(e) => setPrice(e.target.value)} 
                    min="0"
                    className="w-full px-4 py-3 bg-white/5 border border-white/10 text-white text-sm font-mono placeholder:text-white/20 outline-none focus:border-accent/50" 
                    placeholder="Enter price" 
                  />
                </div>
              )}

              <button 
                onClick={handleCreateListing}
                disabled={loading || !domain.trim()}
                className="w-full py-3 bg-accent text-black text-xs font-bold uppercase tracking-wider flex items-center justify-center gap-2 disabled:opacity-50 hover:bg-white transition-colors"
              >
                {loading ? <Loader2 className="w-4 h-4 animate-spin" /> : <>Next: Verify Ownership <ArrowRight className="w-4 h-4" /></>}
              </button>
            </div>
          )}

          {/* STEP 2: DNS Verification */}
          {step === 2 && verificationData && (
            <div className="space-y-6">
              <div className="text-center mb-6">
                <h2 className="text-xl font-display text-white mb-2">Verify Ownership</h2>
                <p className="text-xs font-mono text-white/40">Step 2 of 3: Add a DNS TXT record to prove you own this domain</p>
              </div>

              <div className="p-4 bg-amber-400/5 border border-amber-400/20">
                <div className="flex items-start gap-3">
                  <Shield className="w-5 h-5 text-amber-400 shrink-0 mt-0.5" />
                  <div className="text-xs text-white/60 leading-relaxed">
                    Add this TXT record to your domain&apos;s DNS settings. This proves you control the domain.
                  </div>
                </div>
              </div>

              <div className="space-y-4">
                <div>
                  <label className="block text-[10px] font-mono text-white/40 uppercase mb-2">Record Type</label>
                  <div className="px-4 py-3 bg-white/5 border border-white/10 text-white text-sm font-mono">
                    TXT
                  </div>
                </div>

                <div>
                  <label className="block text-[10px] font-mono text-white/40 uppercase mb-2">Host / Name</label>
                  <div className="flex items-center gap-2">
                    <div className="flex-1 px-4 py-3 bg-white/5 border border-white/10 text-white text-sm font-mono overflow-x-auto">
                      {verificationData.dns_record_name}
                    </div>
                    <button onClick={() => copyToClipboard(verificationData.dns_record_name)} className="p-3 border border-white/10 text-white/40 hover:text-white">
                      <Copy className="w-4 h-4" />
                    </button>
                  </div>
                </div>

                <div>
                  <label className="block text-[10px] font-mono text-white/40 uppercase mb-2">Value</label>
                  <div className="flex items-center gap-2">
                    <div className="flex-1 px-4 py-3 bg-white/5 border border-white/10 text-accent text-sm font-mono overflow-x-auto">
                      {verificationData.verification_code}
                    </div>
                    <button onClick={() => copyToClipboard(verificationData.verification_code)} className="p-3 border border-white/10 text-white/40 hover:text-white">
                      <Copy className="w-4 h-4" />
                    </button>
                  </div>
                </div>
              </div>

              <div className="p-3 bg-white/[0.02] border border-white/[0.08] text-[11px] font-mono text-white/40 leading-relaxed">
                💡 DNS changes can take 1-5 minutes to propagate. If verification fails, wait a moment and try again.
              </div>

              <button 
                onClick={handleCheckVerification}
                disabled={verifying}
                className="w-full py-3 bg-accent text-black text-xs font-bold uppercase tracking-wider flex items-center justify-center gap-2 disabled:opacity-50 hover:bg-white transition-colors"
              >
                {verifying ? <Loader2 className="w-4 h-4 animate-spin" /> : <><RefreshCw className="w-4 h-4" /> Check Verification</>}
              </button>
            </div>
          )}

          {/* STEP 3: Publish */}
          {step === 3 && (
            <div className="space-y-6 text-center">
              <div className="w-16 h-16 bg-accent/10 border border-accent/20 flex items-center justify-center mx-auto">
                <CheckCircle className="w-8 h-8 text-accent" />
              </div>

              <div>
                <h2 className="text-xl font-display text-white mb-2">Ownership Verified!</h2>
                <p className="text-xs font-mono text-white/40">Step 3 of 3: Publish your listing to the Pounce Market</p>
              </div>

              <div className="p-4 bg-white/[0.02] border border-white/[0.08]">
                <div className="text-lg font-bold font-mono text-white mb-1">{domain}</div>
                <div className="text-sm text-accent font-mono">
                  {price ? `$${parseFloat(price).toLocaleString()}` : 'Make Offer'}
                </div>
              </div>

              <div className="p-3 bg-accent/5 border border-accent/20 text-[11px] font-mono text-white/60 leading-relaxed">
                Your listing will appear in the <strong>Market Feed</strong> with the <span className="text-accent">Pounce Direct</span> badge.
              </div>

              <button 
                onClick={handlePublish}
                disabled={loading}
                className="w-full py-3 bg-accent text-black text-xs font-bold uppercase tracking-wider flex items-center justify-center gap-2 disabled:opacity-50 hover:bg-white transition-colors"
              >
                {loading ? <Loader2 className="w-4 h-4 animate-spin" /> : <><Globe className="w-4 h-4" /> Publish to Market</>}
              </button>
            </div>
          )}
        </div>
      </div>
    </div>
  )
}

// ============================================================================
// DNS VERIFICATION MODAL (For existing drafts)
// ============================================================================

function DnsVerificationModal({ listing, onClose, onVerified }: { 
  listing: Listing
  onClose: () => void
  onVerified: () => void 
}) {
  const [verificationData, setVerificationData] = useState<any>(null)
  const [loading, setLoading] = useState(true)
  const [verifying, setVerifying] = useState(false)
  const [error, setError] = useState<string | null>(null)

  useEffect(() => {
    loadVerificationData()
  }, [listing.id])

  const loadVerificationData = async () => {
    try {
      const data = await api.startDnsVerification(listing.id)
      setVerificationData(data)
    } catch (err: any) {
      setError(err.message || 'Failed to load verification data')
    } finally {
      setLoading(false)
    }
  }

  const handleCheck = async () => {
    setVerifying(true)
    setError(null)
    try {
      const result = await api.checkDnsVerification(listing.id)
      if (result.verified) {
        onVerified()
      } else {
        setError(result.message || 'DNS record not found yet')
      }
    } catch (err: any) {
      setError(err.message || 'Check failed')
    } finally {
      setVerifying(false)
    }
  }

  const copyToClipboard = (text: string) => {
    navigator.clipboard.writeText(text)
  }

  return (
    <div className="fixed inset-0 z-[110] bg-black/90 flex items-center justify-center p-4" onClick={onClose}>
      <div className="w-full max-w-lg 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">
            <Shield className="w-4 h-4 text-amber-400" />
            <span className="text-xs font-mono text-amber-400 uppercase tracking-wider">DNS Verification</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>

        <div className="p-6">
          {loading ? (
            <div className="flex items-center justify-center py-10">
              <Loader2 className="w-6 h-6 text-accent animate-spin" />
            </div>
          ) : verificationData ? (
            <div className="space-y-4">
              {error && (
                <div className="p-3 bg-rose-500/10 border border-rose-500/20 text-rose-400 text-xs font-mono">
                  {error}
                </div>
              )}

              <div className="text-center mb-4">
                <h3 className="text-lg font-display text-white mb-1">{listing.domain}</h3>
                <p className="text-xs font-mono text-white/40">Add this TXT record to verify ownership</p>
              </div>

              <div>
                <label className="block text-[10px] font-mono text-white/40 uppercase mb-2">Host / Name</label>
                <div className="flex items-center gap-2">
                  <div className="flex-1 px-4 py-3 bg-white/5 border border-white/10 text-white text-sm font-mono overflow-x-auto">
                    {verificationData.dns_record_name}
                  </div>
                  <button onClick={() => copyToClipboard(verificationData.dns_record_name)} className="p-3 border border-white/10 text-white/40 hover:text-white">
                    <Copy className="w-4 h-4" />
                  </button>
                </div>
              </div>

              <div>
                <label className="block text-[10px] font-mono text-white/40 uppercase mb-2">TXT Value</label>
                <div className="flex items-center gap-2">
                  <div className="flex-1 px-4 py-3 bg-white/5 border border-white/10 text-accent text-sm font-mono overflow-x-auto">
                    {verificationData.verification_code}
                  </div>
                  <button onClick={() => copyToClipboard(verificationData.verification_code)} className="p-3 border border-white/10 text-white/40 hover:text-white">
                    <Copy className="w-4 h-4" />
                  </button>
                </div>
              </div>

              <button 
                onClick={handleCheck}
                disabled={verifying}
                className="w-full py-3 bg-accent text-black text-xs font-bold uppercase tracking-wider flex items-center justify-center gap-2 disabled:opacity-50"
              >
                {verifying ? <Loader2 className="w-4 h-4 animate-spin" /> : <><RefreshCw className="w-4 h-4" /> Check Verification</>}
              </button>
            </div>
          ) : (
            <div className="text-center py-10 text-white/40">Failed to load verification data</div>
          )}
        </div>
      </div>
    </div>
  )
}

// ============================================================================
// MOBILE DRAWER
// ============================================================================

function MobileDrawer({ user, tierName, TierIcon, drawerNavSections, onClose, onLogout }: any) {
  return (
    <div className="lg:hidden fixed inset-0 z-[100]">
      <div className="absolute inset-0 bg-black/80" onClick={onClose} />
      <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={onClose} 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: any) => (
            <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={onClose} 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={onClose} 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={onClose} 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={onClose} 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={onLogout} 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>
  )
}
