import { useState, useEffect, useContext } from 'react'
import { useParams, useNavigate } from 'react-router-dom'
import { ArrowLeft, Plus, Edit2, Trash2, DollarSign, Eye, MousePointer, Target, TrendingUp, FileText, Megaphone, Search, Globe, Pencil, Users, X, UserPlus, MessageCircle, Settings } from 'lucide-react'
import { format } from 'date-fns'
import { AppContext } from '../App'
import { useAuth } from '../contexts/AuthContext'
import { useLanguage } from '../i18n/LanguageContext'
import { api, PLATFORMS, getInitials } from '../utils/api'
import PlatformIcon, { PlatformIcons } from '../components/PlatformIcon'
import StatusBadge from '../components/StatusBadge'
import BrandBadge from '../components/BrandBadge'
import Modal from '../components/Modal'
import BudgetBar from '../components/BudgetBar'
import CommentsSection from '../components/CommentsSection'
import CampaignDetailPanel from '../components/CampaignDetailPanel'
import TrackDetailPanel from '../components/TrackDetailPanel'
import PostDetailPanel from '../components/PostDetailPanel'

const TRACK_TYPES = {
  organic_social: { label: 'Organic Social', icon: Megaphone, color: 'text-green-600 bg-green-50', hasBudget: false },
  paid_social: { label: 'Paid Social', icon: DollarSign, color: 'text-blue-600 bg-blue-50', hasBudget: true },
  paid_search: { label: 'Paid Search (PPC)', icon: Search, color: 'text-amber-600 bg-amber-50', hasBudget: true },
  seo_content: { label: 'SEO / Content', icon: Globe, color: 'text-purple-600 bg-purple-50', hasBudget: false },
  production: { label: 'Production', icon: FileText, color: 'text-red-600 bg-red-50', hasBudget: true },
}

const TRACK_STATUSES = ['planned', 'active', 'paused', 'completed']

function MetricBox({ label, value, icon: Icon, color = 'text-text-primary' }) {
  return (
    <div className="text-center">
      <Icon className={`w-4 h-4 mx-auto mb-0.5 ${color}`} />
      <div className={`text-base font-bold ${color}`}>{value ?? '—'}</div>
      <div className="text-[10px] text-text-tertiary">{label}</div>
    </div>
  )
}

export default function CampaignDetail() {
  const { id } = useParams()
  const navigate = useNavigate()
  const { brands, getBrandName, teamMembers } = useContext(AppContext)
  const { lang, currencySymbol } = useLanguage()
  const { permissions, user } = useAuth()
  const isSuperadmin = user?.role === 'superadmin'
  const [campaign, setCampaign] = useState(null)
  const [tracks, setTracks] = useState([])
  const [posts, setPosts] = useState([])
  const [assignments, setAssignments] = useState([])
  const [allUsers, setAllUsers] = useState([])
  const [loading, setLoading] = useState(true)
  const [showAssignModal, setShowAssignModal] = useState(false)
  const [selectedUserIds, setSelectedUserIds] = useState([])
  const canSetBudget = permissions?.canSetBudget
  const [editingBudget, setEditingBudget] = useState(false)
  const [budgetValue, setBudgetValue] = useState('')
  const [showDeleteConfirm, setShowDeleteConfirm] = useState(false)
  const [trackToDelete, setTrackToDelete] = useState(null)
  const [selectedPost, setSelectedPost] = useState(null)
  const [showDiscussion, setShowDiscussion] = useState(false)
  const [allCampaigns, setAllCampaigns] = useState([])

  // Panel state
  const [panelCampaign, setPanelCampaign] = useState(null)
  const [panelTrack, setPanelTrack] = useState(null)
  const [trackScrollToMetrics, setTrackScrollToMetrics] = useState(false)

  const isCreator = campaign?.createdByUserId === user?.id || campaign?.created_by_user_id === user?.id
  const canManage = isSuperadmin || (permissions?.canEditCampaigns && isCreator)
  const canAssign = isSuperadmin || (permissions?.canAssignCampaigns && isCreator)

  useEffect(() => { loadAll() }, [id])
  useEffect(() => {
    api.get('/campaigns').then(r => setAllCampaigns(Array.isArray(r) ? r : [])).catch(() => {})
  }, [])

  const loadAll = async () => {
    try {
      const [campRes, tracksRes, postsRes, assignRes] = await Promise.all([
        api.get(`/campaigns/${id}`),
        api.get(`/campaigns/${id}/tracks`),
        api.get(`/campaigns/${id}/posts`),
        api.get(`/campaigns/${id}/assignments`),
      ])
      setCampaign(campRes)
      setTracks(Array.isArray(tracksRes) ? tracksRes : [])
      setPosts(Array.isArray(postsRes) ? postsRes : [])
      setAssignments(Array.isArray(assignRes) ? assignRes : [])
    } catch (err) {
      console.error('Failed to load campaign:', err)
    } finally {
      setLoading(false)
    }
  }

  const loadUsersForAssign = async () => {
    try {
      const users = await api.get('/users/team?all=true')
      setAllUsers(Array.isArray(users) ? users : [])
    } catch (err) {
      console.error('Failed to load users:', err)
    }
  }

  const openAssignModal = () => {
    loadUsersForAssign()
    setSelectedUserIds(assignments.map(a => a.user_id))
    setShowAssignModal(true)
  }

  const saveAssignments = async () => {
    try {
      const currentIds = assignments.map(a => a.user_id)
      const toAdd = selectedUserIds.filter(id => !currentIds.includes(id))
      const toRemove = currentIds.filter(id => !selectedUserIds.includes(id))

      if (toAdd.length > 0) {
        await api.post(`/campaigns/${id}/assignments`, { user_ids: toAdd })
      }
      for (const uid of toRemove) {
        await api.delete(`/campaigns/${id}/assignments/${uid}`)
      }
      setShowAssignModal(false)
      loadAll()
    } catch (err) {
      console.error('Failed to save assignments:', err)
    }
  }

  const removeAssignment = async (userId) => {
    try {
      await api.delete(`/campaigns/${id}/assignments/${userId}`)
      loadAll()
    } catch (err) {
      console.error('Failed to remove assignment:', err)
    }
  }

  // Panel handlers
  const handleCampaignPanelSave = async (campaignId, data) => {
    await api.patch(`/campaigns/${campaignId}`, data)
    loadAll()
  }

  const handleCampaignPanelDelete = async (campaignId) => {
    await api.delete(`/campaigns/${campaignId}`)
    navigate('/campaigns')
  }

  const handleTrackPanelSave = async (trackId, data) => {
    if (trackId) {
      await api.patch(`/tracks/${trackId}`, data)
    } else {
      await api.post(`/campaigns/${id}/tracks`, data)
    }
    setPanelTrack(null)
    loadAll()
  }

  const handleTrackPanelDelete = async (trackId) => {
    await api.delete(`/tracks/${trackId}`)
    setPanelTrack(null)
    loadAll()
  }

  const handlePostPanelSave = async (postId, data) => {
    if (postId) {
      await api.patch(`/posts/${postId}`, data)
    } else {
      await api.post('/posts', data)
    }
    loadAll()
  }

  const handlePostPanelDelete = async (postId) => {
    await api.delete(`/posts/${postId}`)
    setSelectedPost(null)
    loadAll()
  }

  const deleteTrack = async (trackId) => {
    setTrackToDelete(trackId)
    setShowDeleteConfirm(true)
  }

  const confirmDeleteTrack = async () => {
    if (!trackToDelete) return
    await api.delete(`/tracks/${trackToDelete}`)
    setTrackToDelete(null)
    loadAll()
  }

  if (loading) {
    return (
      <div className="space-y-6 animate-pulse">
        <div className="flex items-start gap-4">
          <div className="w-8 h-8 bg-surface-tertiary rounded-lg"></div>
          <div className="flex-1">
            <div className="h-6 bg-surface-tertiary rounded w-64 mb-2"></div>
            <div className="h-4 bg-surface-tertiary rounded w-96 mb-2"></div>
            <div className="h-3 bg-surface-tertiary rounded w-48"></div>
          </div>
        </div>
        <div className="h-24 bg-surface-tertiary rounded-xl"></div>
        <div className="h-48 bg-surface-tertiary rounded-xl"></div>
        <div className="h-64 bg-surface-tertiary rounded-xl"></div>
      </div>
    )
  }

  if (!campaign) {
    return (
      <div className="text-center py-12 text-text-tertiary">
        Campaign not found. <button onClick={() => navigate('/campaigns')} className="text-brand-primary underline">Go back</button>
      </div>
    )
  }

  // Aggregates from tracks
  const totalAllocated = tracks.reduce((s, t) => s + (t.budget_allocated || 0), 0)
  const totalSpent = tracks.reduce((s, t) => s + (t.budget_spent || 0), 0)
  const totalImpressions = tracks.reduce((s, t) => s + (t.impressions || 0), 0)
  const totalClicks = tracks.reduce((s, t) => s + (t.clicks || 0), 0)
  const totalConversions = tracks.reduce((s, t) => s + (t.conversions || 0), 0)
  const totalRevenue = tracks.reduce((s, t) => s + (t.revenue || 0), 0)

  return (
    <div className="flex gap-6 animate-fade-in">
      {/* Main content */}
      <div className={`space-y-6 min-w-0 ${showDiscussion ? 'flex-1' : 'w-full'}`}>
      {/* Header */}
      <div className="flex items-start gap-4">
        <button onClick={() => navigate('/campaigns')} className="mt-1 p-1.5 hover:bg-surface-tertiary rounded-lg">
          <ArrowLeft className="w-5 h-5 text-text-secondary" />
        </button>
        <div className="flex-1 min-w-0">
          <div className="flex items-center gap-3 mb-1">
            <h1 className="text-xl font-bold text-text-primary">{campaign.name}</h1>
            <StatusBadge status={campaign.status} />
            {(campaign.brand_id || campaign.brand_name) && <BrandBadge brand={getBrandName(campaign.brand_id) || campaign.brand_name} />}
          </div>
          {campaign.description && <p className="text-sm text-text-secondary">{campaign.description}</p>}
          <div className="flex items-center gap-3 mt-2 text-xs text-text-tertiary">
            {campaign.start_date && campaign.end_date && (
              <span>{format(new Date(campaign.start_date), 'MMM d')} – {format(new Date(campaign.end_date), 'MMM d, yyyy')}</span>
            )}
            <span>
              Budget: {campaign.budget > 0 ? `${campaign.budget.toLocaleString()} ${currencySymbol}` : 'Not set'}
            </span>
            {campaign.platforms && campaign.platforms.length > 0 && (
              <PlatformIcons platforms={campaign.platforms} size={16} />
            )}
          </div>
        </div>
        {/* Action buttons */}
        <div className="flex items-center gap-2 shrink-0">
          <button
            onClick={() => setShowDiscussion(prev => !prev)}
            className={`flex items-center gap-1.5 px-3 py-2 rounded-lg text-sm font-medium transition-colors ${
              showDiscussion
                ? 'bg-brand-primary text-white shadow-sm'
                : 'bg-surface-tertiary text-text-secondary hover:bg-surface-tertiary/80 hover:text-text-primary'
            }`}
          >
            <MessageCircle className="w-4 h-4" />
            Discussion
          </button>
          {canSetBudget && (
            <button
              onClick={() => { setBudgetValue(campaign.budget || ''); setEditingBudget(true) }}
              className="flex items-center gap-1.5 px-3 py-2 bg-surface-tertiary text-text-secondary hover:bg-surface-tertiary/80 hover:text-text-primary rounded-lg text-sm font-medium transition-colors"
            >
              <DollarSign className="w-4 h-4" />
              Budget
            </button>
          )}
          {canManage && (
            <button
              onClick={() => setPanelCampaign(campaign)}
              className="flex items-center gap-1.5 px-3 py-2 bg-brand-primary text-white rounded-lg text-sm font-medium hover:bg-brand-primary-light shadow-sm transition-colors"
            >
              <Settings className="w-4 h-4" />
              Edit
            </button>
          )}
        </div>
      </div>

      {/* Assigned Team */}
      <div className="bg-white rounded-xl border border-border p-5">
        <div className="flex items-center justify-between mb-3">
          <h3 className="text-xs uppercase tracking-wider text-text-tertiary font-medium flex items-center gap-1.5">
            <Users className="w-3.5 h-3.5" /> Assigned Team
          </h3>
          {canAssign && (
            <button
              onClick={openAssignModal}
              className="flex items-center gap-1.5 px-3 py-1.5 bg-brand-primary text-white rounded-lg text-xs font-medium hover:bg-brand-primary-light"
            >
              <UserPlus className="w-3.5 h-3.5" /> Assign Members
            </button>
          )}
        </div>
        {assignments.length === 0 ? (
          <p className="text-xs text-text-tertiary py-2">No team members assigned yet.</p>
        ) : (
          <div className="flex flex-wrap gap-2">
            {assignments.map(a => (
              <div key={a.user_id} className="flex items-center gap-2 bg-surface-secondary rounded-full pl-1 pr-2 py-1">
                <div className="w-6 h-6 rounded-full bg-brand-primary/10 text-brand-primary flex items-center justify-center text-[10px] font-bold shrink-0">
                  {a.user_avatar ? (
                    <img src={a.user_avatar} className="w-full h-full rounded-full object-cover" alt="" />
                  ) : (
                    getInitials(a.user_name)
                  )}
                </div>
                <span className="text-xs font-medium text-text-primary">{a.user_name}</span>
                {canAssign && (
                  <button
                    onClick={() => removeAssignment(a.user_id)}
                    className="p-0.5 rounded-full hover:bg-red-100 text-text-tertiary hover:text-red-500"
                  >
                    <X className="w-3 h-3" />
                  </button>
                )}
              </div>
            ))}
          </div>
        )}
      </div>

      {/* Aggregate Metrics */}
      {tracks.length > 0 && (
        <div className="bg-white rounded-xl border border-border p-5">
          <h3 className="text-xs uppercase tracking-wider text-text-tertiary font-medium mb-4">Campaign Totals (from tracks)</h3>
          <div className="grid grid-cols-3 md:grid-cols-6 gap-4">
            <MetricBox icon={DollarSign} label="Allocated" value={`${totalAllocated.toLocaleString()}`} color="text-blue-600" />
            <MetricBox icon={TrendingUp} label="Spent" value={`${totalSpent.toLocaleString()}`} color="text-amber-600" />
            <MetricBox icon={Eye} label="Impressions" value={totalImpressions.toLocaleString()} color="text-purple-600" />
            <MetricBox icon={MousePointer} label="Clicks" value={totalClicks.toLocaleString()} color="text-green-600" />
            <MetricBox icon={Target} label="Conversions" value={totalConversions.toLocaleString()} color="text-red-600" />
            <MetricBox icon={DollarSign} label="Revenue" value={`${totalRevenue.toLocaleString()}`} color="text-emerald-600" />
          </div>
          {totalAllocated > 0 && (
            <div className="mt-4">
              <BudgetBar budget={totalAllocated} spent={totalSpent} height="h-2" />
            </div>
          )}
        </div>
      )}

      {/* Tracks */}
      <div className="bg-white rounded-xl border border-border overflow-hidden">
        <div className="flex items-center justify-between px-5 py-4 border-b border-border">
          <h3 className="font-semibold text-text-primary">Tracks</h3>
          {canManage && (
            <button
              onClick={() => { setPanelTrack({}); setTrackScrollToMetrics(false) }}
              className="flex items-center gap-1.5 px-3 py-1.5 bg-brand-primary text-white rounded-lg text-xs font-medium hover:bg-brand-primary-light"
            >
              <Plus className="w-3.5 h-3.5" /> Add Track
            </button>
          )}
        </div>

        {tracks.length === 0 ? (
          <div className="py-12 text-center text-sm text-text-tertiary">
            No tracks yet. Add organic, paid, or SEO tracks to organize this campaign.
          </div>
        ) : (
          <div className="divide-y divide-border-light">
            {tracks.map(track => {
              const typeInfo = TRACK_TYPES[track.type] || TRACK_TYPES.organic_social
              const TypeIcon = typeInfo.icon
              const trackPosts = posts.filter(p => p.track_id === track.id)
              return (
                <div key={track.id} className="px-5 py-4">
                  <div className="flex items-start gap-3">
                    <div className={`p-2 rounded-lg ${typeInfo.color}`}>
                      <TypeIcon className="w-4 h-4" />
                    </div>
                    <div className="flex-1 min-w-0">
                      <div className="flex items-center gap-2 mb-0.5">
                        <h4 className="text-sm font-semibold text-text-primary">
                          {track.name || typeInfo.label}
                        </h4>
                        <span className="text-[10px] px-1.5 py-0.5 rounded bg-surface-tertiary text-text-tertiary">
                          {typeInfo.label}
                        </span>
                        {track.platform && (
                          <PlatformIcon platform={track.platform} size={16} />
                        )}
                        <StatusBadge status={track.status} size="xs" />
                      </div>

                      {/* Budget bar for paid tracks */}
                      {track.budget_allocated > 0 && (
                        <div className="w-48 mt-1.5">
                          <BudgetBar budget={track.budget_allocated} spent={track.budget_spent || 0} height="h-2" />
                        </div>
                      )}

                      {/* Quick metrics */}
                      {(track.impressions > 0 || track.clicks > 0 || track.conversions > 0) && (
                        <div className="flex items-center gap-3 mt-1.5 text-[10px] text-text-tertiary">
                          {track.impressions > 0 && <span>👁 {track.impressions.toLocaleString()}</span>}
                          {track.clicks > 0 && <span>🖱 {track.clicks.toLocaleString()}</span>}
                          {track.conversions > 0 && <span>🎯 {track.conversions.toLocaleString()}</span>}
                          {track.clicks > 0 && track.budget_spent > 0 && (
                            <span>CPC: {(track.budget_spent / track.clicks).toFixed(2)} {currencySymbol}</span>
                          )}
                          {track.impressions > 0 && track.clicks > 0 && (
                            <span>CTR: {(track.clicks / track.impressions * 100).toFixed(2)}%</span>
                          )}
                        </div>
                      )}

                      {/* Linked posts count */}
                      {trackPosts.length > 0 && (
                        <div className="text-[10px] text-text-tertiary mt-1">
                          📝 {trackPosts.length} post{trackPosts.length !== 1 ? 's' : ''} linked
                        </div>
                      )}

                      {track.notes && (
                        <p className="text-xs text-text-secondary mt-1 line-clamp-1">{track.notes}</p>
                      )}
                    </div>

                    {/* Actions */}
                    {canManage && (
                      <div className="flex items-center gap-1 shrink-0">
                        <button
                          onClick={() => { setPanelTrack(track); setTrackScrollToMetrics(true) }}
                          title="Update metrics"
                          className="p-1.5 hover:bg-surface-tertiary rounded-lg text-text-tertiary hover:text-brand-primary"
                        >
                          <TrendingUp className="w-4 h-4" />
                        </button>
                        <button
                          onClick={() => { setPanelTrack(track); setTrackScrollToMetrics(false) }}
                          title="Edit track"
                          className="p-1.5 hover:bg-surface-tertiary rounded-lg text-text-tertiary hover:text-text-primary"
                        >
                          <Edit2 className="w-4 h-4" />
                        </button>
                        <button
                          onClick={() => deleteTrack(track.id)}
                          title="Delete track"
                          className="p-1.5 hover:bg-red-50 rounded-lg text-text-tertiary hover:text-red-500"
                        >
                          <Trash2 className="w-4 h-4" />
                        </button>
                      </div>
                    )}
                  </div>
                </div>
              )
            })}
          </div>
        )}
      </div>

      {/* Linked Posts */}
      {posts.length > 0 && (
        <div className="bg-white rounded-xl border border-border overflow-hidden">
          <div className="px-5 py-4 border-b border-border">
            <h3 className="font-semibold text-text-primary">Linked Posts ({posts.length})</h3>
          </div>
          <div className="divide-y divide-border-light">
            {posts.map(post => (
              <div
                key={post.id}
                onClick={() => setSelectedPost(post)}
                className="flex items-center gap-3 px-5 py-3 hover:bg-surface-secondary cursor-pointer transition-colors"
              >
                {post.thumbnail_url && (
                  <img src={post.thumbnail_url} alt="" className="w-10 h-10 rounded-lg object-cover shrink-0" />
                )}
                <div className="flex-1 min-w-0">
                  <div className="flex items-center gap-2">
                    <h4 className="text-sm font-medium text-text-primary">{post.title}</h4>
                    <StatusBadge status={post.status} size="xs" />
                  </div>
                  <div className="flex items-center gap-2 mt-0.5 text-[10px] text-text-tertiary">
                    {post.track_name && <span className="px-1.5 py-0.5 rounded bg-surface-tertiary">{post.track_name}</span>}
                    {post.brand_name && <BrandBadge brand={post.brand_name} />}
                    {post.assigned_name && <span>→ {post.assigned_name}</span>}
                    {post.platforms && post.platforms.length > 0 && (
                      <PlatformIcons platforms={post.platforms} size={14} />
                    )}
                  </div>
                </div>
              </div>
            ))}
          </div>
        </div>
      )}

      </div>{/* end main content */}

      {/* ─── DISCUSSION SIDEBAR ─── */}
      {showDiscussion && (
        <div className="w-[340px] shrink-0 bg-white rounded-xl border border-border flex flex-col self-start sticky top-4" style={{ maxHeight: 'calc(100vh - 6rem)' }}>
          <div className="flex items-center justify-between px-4 py-3 border-b border-border">
            <h3 className="text-sm font-semibold text-text-primary flex items-center gap-1.5">
              <MessageCircle className="w-4 h-4" />
              Discussion
            </h3>
            <button onClick={() => setShowDiscussion(false)} className="p-1 hover:bg-surface-tertiary rounded-lg text-text-tertiary">
              <X className="w-4 h-4" />
            </button>
          </div>
          <div className="flex-1 overflow-y-auto p-4">
            <CommentsSection entityType="campaign" entityId={Number(id)} />
          </div>
        </div>
      )}

      {/* Delete Track Confirmation */}
      <Modal
        isOpen={showDeleteConfirm}
        onClose={() => { setShowDeleteConfirm(false); setTrackToDelete(null) }}
        title="Delete Track?"
        isConfirm
        danger
        confirmText="Delete Track"
        onConfirm={confirmDeleteTrack}
      >
        Are you sure you want to delete this campaign track? This action cannot be undone.
      </Modal>

      {/* Assign Members Modal */}
      <Modal
        isOpen={showAssignModal}
        onClose={() => setShowAssignModal(false)}
        title="Assign Team Members"
      >
        <div className="space-y-3 max-h-80 overflow-y-auto">
          {allUsers.map(u => {
            const checked = selectedUserIds.includes(u.id || u._id)
            return (
              <label
                key={u.id || u._id}
                className={`flex items-center gap-3 p-2 rounded-lg cursor-pointer hover:bg-surface-secondary ${checked ? 'bg-brand-primary/5' : ''}`}
              >
                <input
                  type="checkbox"
                  checked={checked}
                  onChange={() => {
                    const uid = u.id || u._id
                    setSelectedUserIds(prev =>
                      prev.includes(uid) ? prev.filter(id => id !== uid) : [...prev, uid]
                    )
                  }}
                  className="rounded border-border text-brand-primary focus:ring-brand-primary"
                />
                <div className="w-7 h-7 rounded-full bg-brand-primary/10 text-brand-primary flex items-center justify-center text-[10px] font-bold shrink-0">
                  {u.avatar ? (
                    <img src={u.avatar} className="w-full h-full rounded-full object-cover" alt="" />
                  ) : (
                    getInitials(u.name)
                  )}
                </div>
                <div className="flex-1 min-w-0">
                  <div className="text-sm font-medium text-text-primary">{u.name}</div>
                  {u.team_role && <div className="text-[10px] text-text-tertiary">{u.team_role}</div>}
                </div>
              </label>
            )
          })}
        </div>
        <div className="flex justify-end gap-3 pt-4 border-t border-border mt-4">
          <button onClick={() => setShowAssignModal(false)} className="px-4 py-2 text-sm text-text-secondary hover:bg-surface-tertiary rounded-lg">Cancel</button>
          <button onClick={saveAssignments} className="px-5 py-2 bg-brand-primary text-white rounded-lg text-sm font-medium hover:bg-brand-primary-light shadow-sm">
            Save Assignments
          </button>
        </div>
      </Modal>

      {/* Budget Modal */}
      <Modal isOpen={editingBudget} onClose={() => setEditingBudget(false)} title="Set Campaign Budget" size="sm">
        <div className="space-y-4">
          <div>
            <label className="block text-sm font-medium text-text-primary mb-1">Budget ({currencySymbol})</label>
            <input
              type="number"
              value={budgetValue}
              onChange={e => setBudgetValue(e.target.value)}
              autoFocus
              min="0"
              className="w-full px-3 py-2 text-sm border border-border rounded-lg focus:outline-none focus:ring-2 focus:ring-brand-primary/20 focus:border-brand-primary"
              placeholder="Enter budget amount"
            />
          </div>
          <div className="flex items-center justify-end gap-3 pt-4 border-t border-border">
            <button
              onClick={() => setEditingBudget(false)}
              className="px-4 py-2 text-sm font-medium text-text-secondary hover:bg-surface-tertiary rounded-lg"
            >
              Cancel
            </button>
            <button
              onClick={async () => {
                try {
                  await api.patch(`/campaigns/${id}`, { budget: budgetValue ? Number(budgetValue) : null })
                  setEditingBudget(false)
                  loadAll()
                } catch (err) {
                  console.error('Failed to update budget:', err)
                }
              }}
              className="px-5 py-2 bg-brand-primary text-white rounded-lg text-sm font-medium hover:bg-brand-primary-light shadow-sm"
            >
              Save
            </button>
          </div>
        </div>
      </Modal>

      {/* Post Detail Panel */}
      {selectedPost && (
        <PostDetailPanel
          post={selectedPost}
          onClose={() => setSelectedPost(null)}
          onSave={handlePostPanelSave}
          onDelete={handlePostPanelDelete}
          brands={brands}
          teamMembers={teamMembers}
          campaigns={allCampaigns}
        />
      )}

      {/* Campaign Edit Panel */}
      {panelCampaign && (
        <CampaignDetailPanel
          campaign={panelCampaign}
          onClose={() => setPanelCampaign(null)}
          onSave={handleCampaignPanelSave}
          onDelete={permissions?.canDeleteCampaigns ? handleCampaignPanelDelete : null}
          brands={brands}
          permissions={permissions}
        />
      )}

      {/* Track Detail Panel */}
      {panelTrack && (
        <TrackDetailPanel
          track={panelTrack}
          campaignId={id}
          onClose={() => setPanelTrack(null)}
          onSave={handleTrackPanelSave}
          onDelete={handleTrackPanelDelete}
          scrollToMetrics={trackScrollToMetrics}
        />
      )}
    </div>
  )
}
