import { useState, useCallback } from 'react';
import { motion, AnimatePresence } from 'motion/react';
import {
  GlobeFilled,
  VideoFilled,
  BoltFilled,
  CheckFilled,
  CalendarFilled,
  FileTextFilled,
} from '@/shared/icons/FilledIcons';
import { SectionWrapper } from '@/features/report/components/ui/SectionWrapper';
import { Button } from '@/shared/ui/button';
import { Textarea } from '@/shared/ui/textarea';
import { STAGES, STAGE_COLORS, channelIconMap } from '../data/workflowTrackerConstants';
import type { WorkflowData, WorkflowItem, WorkflowStage, WorkflowContentType } from '@/features/plan/types/plan';

interface WorkflowTrackerProps {
  data: WorkflowData;
}

function StageBar({ currentStage }: { currentStage: WorkflowStage }) {
  const currentIdx = STAGES.findIndex((s) => s.key === currentStage);
  return (
    <div className="flex items-center gap-0 mb-4">
      {STAGES.map((stage, idx) => {
        const isPast = idx < currentIdx;
        const isCurrent = idx === currentIdx;
        return (
          <div key={stage.key} className="flex items-center flex-1 min-w-0">
            <div className={`flex flex-col items-center flex-1 min-w-0`}>
              <div
                className={`w-6 h-6 rounded-full flex items-center justify-center text-[10px] font-bold shrink-0 transition-colors ${
                  isCurrent
                    ? 'bg-brand-purple-vivid text-white shadow-[0_0_0_3px_rgba(108,92,231,0.2)]'
                    : isPast
                    ? 'bg-brand-purple-soft text-white'
                    : 'bg-slate-200 text-slate-400'
                }`}
              >
                {isPast ? <CheckFilled size={10} /> : idx + 1}
              </div>
              <span className={`text-[9px] mt-1 font-medium whitespace-nowrap ${isCurrent ? 'text-brand-purple-vivid' : isPast ? 'text-brand-purple-soft' : 'text-slate-400'}`}>
                {stage.short}
              </span>
            </div>
            {idx < STAGES.length - 1 && (
              <div className={`h-0.5 flex-1 mx-1 rounded-full transition-colors ${idx < currentIdx ? 'bg-brand-purple-soft' : 'bg-slate-200'}`} />
            )}
          </div>
        );
      })}
    </div>
  );
}

function WorkflowCard({ item, onStageChange, onNotesChange }: {
  item: WorkflowItem;
  onStageChange: (id: string, stage: WorkflowStage) => void;
  onNotesChange: (id: string, notes: string) => void;
}) {
  const [expanded, setExpanded] = useState(false);
  const [editingNotes, setEditingNotes] = useState(false);
  const [notesValue, setNotesValue] = useState(item.userNotes ?? '');

  const stageColor = STAGE_COLORS[item.stage];
  const ChannelIcon = channelIconMap[item.channelIcon] ?? GlobeFilled;
  const currentStageIdx = STAGES.findIndex((s) => s.key === item.stage);
  const nextStage = STAGES[currentStageIdx + 1];

  const saveNotes = () => {
    onNotesChange(item.id, notesValue);
    setEditingNotes(false);
  };

  return (
    <motion.div
      layout
      className="bg-white rounded-2xl border border-slate-100 shadow-[3px_4px_12px_rgba(0,0,0,0.06)] overflow-hidden"
      animate={{ opacity: 1, y: 0 }}
      exit={{ opacity: 0, y: -8 }}
      transition={{ duration: 0.3 }}
    >
      {/* Card Header */}
      <Button
        type="button"
        variant="ghost"
        className="w-full flex items-center gap-3 px-5 py-4 h-auto text-left hover:bg-slate-50/50 rounded-none"
        onClick={() => setExpanded((p) => !p)}
      >
        <div className={`w-8 h-8 rounded-xl flex items-center justify-center shrink-0 ${stageColor.bg} ${stageColor.border} border`}>
          <ChannelIcon size={16} className={stageColor.text} />
        </div>

        <div className="flex-1 min-w-0">
          <p className="font-semibold text-brand-navy text-sm truncate">{item.title}</p>
          <p className="text-xs text-slate-500 mt-0.5">{item.channel}</p>
        </div>

        <div className="flex items-center gap-2 shrink-0">
          {/* Stage badge */}
          <span className={`text-[10px] font-semibold px-2.5 py-1 rounded-full border ${stageColor.bg} ${stageColor.text} ${stageColor.border}`}>
            {STAGES.find((s) => s.key === item.stage)?.label}
          </span>
          {item.scheduledDate && (
            <div className="flex items-center gap-1 text-[10px] text-slate-500">
              <CalendarFilled size={10} />
              <span>{item.scheduledDate.slice(5)}</span>
            </div>
          )}
          <svg
            className={`w-4 h-4 text-slate-400 transition-transform duration-200 ${expanded ? 'rotate-90' : ''}`}
            fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}
          >
            <path strokeLinecap="round" strokeLinejoin="round" d="M9 5l7 7-7 7" />
          </svg>
        </div>
      </Button>

      {/* Expanded Body */}
      <AnimatePresence>
        {expanded && (
          <motion.div
            animate={{ opacity: 1, height: 'auto' }}
            exit={{ opacity: 0, height: 0 }}
            transition={{ duration: 0.2 }}
            className="border-t border-slate-100"
          >
            <div className="px-5 py-4 space-y-4">
              {/* Stage Progress */}
              <StageBar currentStage={item.stage} />

              {/* AI Draft Content */}
              {item.contentType === 'video' && item.videoDraft && (
                <div className="space-y-3">
                  <div className="flex items-center gap-2">
                    <BoltFilled size={13} className="text-brand-purple-vivid" />
                    <p className="text-xs font-semibold text-slate-500 uppercase tracking-widest">AI 스크립트 초안</p>
                    <span className="text-[10px] text-slate-400 font-mono">{item.videoDraft.duration}</span>
                  </div>
                  <pre className="text-xs text-slate-700 bg-slate-50 rounded-xl p-4 whitespace-pre-wrap leading-relaxed border border-slate-100 font-sans">
                    {item.videoDraft.script}
                  </pre>

                  <div className="flex items-center gap-2 mt-2">
                    <FileTextFilled size={13} className="text-brand-purple-vivid" />
                    <p className="text-xs font-semibold text-slate-500 uppercase tracking-widest">촬영 지시서</p>
                  </div>
                  <ul className="space-y-1.5">
                    {item.videoDraft.shootingGuide.map((guide, i) => (
                      <li key={i} className="flex items-start gap-2 text-xs text-slate-600">
                        <span className="w-4 h-4 rounded-full bg-brand-tint-purple text-brand-purple-vivid flex items-center justify-center text-[9px] font-bold shrink-0 mt-0.5">{i + 1}</span>
                        {guide}
                      </li>
                    ))}
                  </ul>
                </div>
              )}

              {item.contentType === 'image-text' && item.imageTextDraft && (
                <div className="space-y-3">
                  <div className="flex items-center gap-2">
                    <BoltFilled size={13} className="text-brand-purple-vivid" />
                    <p className="text-xs font-semibold text-slate-500 uppercase tracking-widest">
                      AI {item.imageTextDraft.type === 'cardnews' ? '카드뉴스 카피' : '블로그 초안'}
                    </p>
                  </div>
                  <div className="bg-slate-50 rounded-xl p-4 border border-slate-100">
                    <p className="font-bold text-sm text-brand-navy mb-3">{item.imageTextDraft.headline}</p>
                    <ul className="space-y-2">
                      {item.imageTextDraft.copy.map((line, i) => (
                        <li key={i} className="text-xs text-slate-600 leading-relaxed whitespace-pre-line">{line}</li>
                      ))}
                    </ul>
                  </div>
                  {item.imageTextDraft.layoutHint && (
                    <p className="text-[10px] text-slate-400 italic">{item.imageTextDraft.layoutHint}</p>
                  )}
                </div>
              )}

              {/* User Notes */}
              <div>
                <div className="flex items-center justify-between mb-1.5">
                  <p className="text-xs font-semibold text-slate-500">수정 요청 / 메모</p>
                  {!editingNotes && (
                    <Button
                      type="button"
                      variant="ghost"
                      size="sm"
                      onClick={(e) => { e.stopPropagation(); setEditingNotes(true); }}
                      className="text-[10px] h-auto px-2 py-1 text-brand-purple-vivid hover:text-brand-purple-muted hover:bg-transparent font-medium"
                    >
                      {notesValue ? '편집' : '+ 추가'}
                    </Button>
                  )}
                </div>
                {editingNotes ? (
                  <div className="space-y-2">
                    <Textarea
                      value={notesValue}
                      onChange={(e) => setNotesValue(e.target.value)}
                      placeholder="수정할 내용이나 추가 지시사항을 입력하세요..."
                      className="w-full text-xs text-slate-700 border-slate-200 rounded-xl px-3 py-2.5 resize-none focus-visible:ring-brand-purple-vivid/30 focus-visible:border-brand-purple-vivid min-h-[80px] placeholder:text-slate-300"
                      autoFocus
                    />
                    <div className="flex gap-2">
                      <Button
                        type="button"
                        size="sm"
                        onClick={saveNotes}
                        className="text-xs px-3 py-1.5 h-auto rounded-lg bg-brand-purple-vivid text-white font-medium hover:bg-[#5A4DD4]"
                      >
                        저장
                      </Button>
                      <Button
                        type="button"
                        variant="ghost"
                        size="sm"
                        onClick={() => { setNotesValue(item.userNotes ?? ''); setEditingNotes(false); }}
                        className="text-xs px-3 py-1.5 h-auto rounded-lg bg-slate-100 text-slate-500 font-medium hover:bg-slate-200"
                      >
                        취소
                      </Button>
                    </div>
                  </div>
                ) : notesValue ? (
                  <p className="text-xs text-slate-600 bg-brand-earth-bg border border-brand-earth-soft rounded-xl px-3 py-2.5 leading-relaxed">
                    {notesValue}
                  </p>
                ) : (
                  <p className="text-xs text-slate-300 italic">없음</p>
                )}
              </div>

              {/* Stage Advance Button */}
              {nextStage && item.stage !== 'scheduled' && (
                <Button
                  type="button"
                  onClick={(e) => { e.stopPropagation(); onStageChange(item.id, nextStage.key); }}
                  className="w-full flex items-center justify-center gap-2 py-2.5 h-auto rounded-xl bg-gradient-to-r from-brand-purple to-brand-purple-deep text-white text-xs font-semibold hover:from-brand-purple hover:to-brand-purple-deep hover:opacity-90"
                >
                  <span>{nextStage.label}(으)로 이동</span>
                  <svg className="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2.5}>
                    <path strokeLinecap="round" strokeLinejoin="round" d="M9 5l7 7-7 7" />
                  </svg>
                </Button>
              )}
              {item.stage === 'scheduled' && (
                <div className="flex items-center justify-center gap-2 py-2.5 rounded-xl bg-brand-tint-purple border border-brand-tint-lavender">
                  <CheckFilled size={14} className="text-brand-purple-vivid" />
                  <span className="text-xs font-semibold text-brand-purple-muted">배포 예약 완료</span>
                </div>
              )}
            </div>
          </motion.div>
        )}
      </AnimatePresence>
    </motion.div>
  );
}

export default function WorkflowTracker({ data }: WorkflowTrackerProps) {
  const [items, setItems] = useState<WorkflowItem[]>(data.items);
  const [activeTab, setActiveTab] = useState<WorkflowContentType>('video');
  const [activeStageFilter, setActiveStageFilter] = useState<WorkflowStage | null>(null);

  const handleStageChange = useCallback((id: string, stage: WorkflowStage) => {
    setItems((prev) => prev.map((item) => item.id === id ? { ...item, stage } : item));
  }, []);

  const handleNotesChange = useCallback((id: string, notes: string) => {
    setItems((prev) => prev.map((item) => item.id === id ? { ...item, userNotes: notes } : item));
  }, []);

  const filtered = items.filter((item) => {
    if (item.contentType !== activeTab) return false;
    if (activeStageFilter && item.stage !== activeStageFilter) return false;
    return true;
  });

  // 현재 탭의 단계별 개수
  const stageCounts = STAGES.map((s) => ({
    ...s,
    count: items.filter((i) => i.contentType === activeTab && i.stage === s.key).length,
  }));

  return (
    <SectionWrapper
      id="workflow-tracker"
      title="Workflow Tracker"
      subtitle="콘텐츠 제작 파이프라인"
      dark
    >
      {/* Content Type Tabs */}
      <div className="flex bg-white/10 rounded-xl p-0.5 mb-6 w-fit" data-no-print>
        {(['video', 'image-text'] as WorkflowContentType[]).map((type) => (
          <Button
            type="button"
            variant="ghost"
            size="sm"
            key={type}
            onClick={() => { setActiveTab(type); setActiveStageFilter(null); }}
            className={`flex items-center gap-2 px-4 py-1.5 h-auto text-xs font-medium rounded-lg ${
              activeTab === type
                ? 'bg-white/20 text-white shadow-sm hover:bg-white/20 hover:text-white'
                : 'text-white/50 hover:text-white/80 hover:bg-transparent'
            }`}
          >
            {type === 'video'
              ? <VideoFilled size={12} />
              : <FileTextFilled size={12} />
            }
            {type === 'video' ? '동영상 콘텐츠' : '이미지+텍스트'}
          </Button>
        ))}
      </div>

      {/* Stage Filter Pills */}
      <div className="flex flex-wrap gap-2 mb-6">
        {stageCounts.map((stage) => {
          if (stage.count === 0) return null;
          const isActive = activeStageFilter === stage.key;
          const color = STAGE_COLORS[stage.key];
          return (
            <motion.button
              type="button"
              key={stage.key}
              onClick={() => setActiveStageFilter(isActive ? null : stage.key)}
              className={`flex items-center gap-2 px-3 py-1.5 rounded-full border text-xs font-medium transition-all ${color.bg} ${color.text} ${color.border} ${
                isActive ? 'ring-2 ring-white/40' : ''
              } ${activeStageFilter && !isActive ? 'opacity-40' : ''}`}
              animate={{ opacity: 1, scale: 1 }}
              transition={{ duration: 0.2 }}
            >
              <span className={`w-1.5 h-1.5 rounded-full ${color.dot}`} />
              {stage.label}
              <span className="font-bold">{stage.count}</span>
            </motion.button>
          );
        })}
        {activeStageFilter && (
          <Button
            type="button"
            variant="ghost"
            size="sm"
            onClick={() => setActiveStageFilter(null)}
            className="text-xs h-auto py-1 text-white/40 hover:text-white/70 hover:bg-transparent px-2"
          >
            × 전체
          </Button>
        )}
      </div>

      {/* Card List */}
      <div className="space-y-3">
        <AnimatePresence mode="popLayout">
          {filtered.length === 0 ? (
            <motion.div
              animate={{ opacity: 1 }}
              className="text-center py-10 text-white/30 text-sm"
            >
              해당 단계의 콘텐츠가 없습니다
            </motion.div>
          ) : (
            filtered.map((item: WorkflowItem) => (
              <WorkflowCard
                key={item.id}
                item={item}
                onStageChange={handleStageChange}
                onNotesChange={handleNotesChange}
              />
            ))
          )}
        </AnimatePresence>
      </div>
    </SectionWrapper>
  );
}
