import { useState } from 'react';
import { motion } from 'motion/react';
import { YoutubeFilled } from '@/shared/icons/FilledIcons';
import { SectionWrapper } from '@/features/report/components/ui/SectionWrapper';
import { EmptyState } from '@/shared/ui/empty-state';
import { Button } from '@/shared/ui/button';
import AssetDetailModal from './AssetDetailModal';
import type { AssetCollectionData, AssetCard, YouTubeRepurposeItem, AssetSource, AssetStatus, AssetType } from '@/features/plan/types/plan';

type ModalAsset =
  | { kind: 'asset'; data: AssetCard }
  | { kind: 'youtube'; data: YouTubeRepurposeItem };

interface AssetCollectionProps {
  data: AssetCollectionData;
}

const filterTabs = [
  { key: 'all', label: '전체' },
  { key: 'homepage', label: '홈페이지' },
  { key: 'naver_place', label: '네이버' },
  { key: 'blog', label: '블로그' },
  { key: 'social', label: '소셜미디어' },
  { key: 'youtube', label: 'YouTube' },
] as const;

type FilterKey = (typeof filterTabs)[number]['key'];

const sourceBadgeColors: Record<AssetSource, string> = {
  homepage: 'bg-slate-100 text-slate-700',
  naver_place: 'bg-brand-tint-purple text-brand-purple-muted',
  blog: 'bg-brand-tint-violet text-brand-purple-faint',
  social: 'bg-brand-rose-bg text-brand-rose',
  youtube: 'bg-brand-rose-bg text-brand-rose',
};

const typeBadgeColors: Record<AssetType, string> = {
  photo: 'bg-brand-tint-violet text-brand-purple-faint',
  video: 'bg-brand-tint-purple text-brand-purple-muted',
  text: 'bg-brand-earth-bg text-brand-earth',
};

const statusConfig: Record<AssetStatus, { className: string; label: string }> = {
  collected: { className: 'bg-brand-tint-purple text-brand-purple-muted', label: '수집완료' },
  pending: { className: 'bg-brand-earth-bg text-brand-earth', label: '수집 대기' },
  needs_creation: { className: 'bg-brand-rose-bg text-brand-rose', label: '제작 필요' },
};

function formatViews(views: number): string {
  if (views >= 1_000_000) return `${(views / 1_000_000).toFixed(1)}M`;
  if (views >= 1_000) return `${Math.round(views / 1_000)}K`;
  return String(views);
}

export default function AssetCollection({ data }: AssetCollectionProps) {
  const [activeFilter, setActiveFilter] = useState<FilterKey>('all');
  const [selectedAsset, setSelectedAsset] = useState<ModalAsset | null>(null);

  const filteredAssets =
    activeFilter === 'all'
      ? data.assets
      : data.assets.filter((a) => a.source === activeFilter);

  return (
    <SectionWrapper
      id="asset-collection"
      title="Asset Collection"
      subtitle="에셋 수집 & 리퍼포징 소스"
    >
      {/* Source Filter Tabs */}
      <div className="flex flex-wrap gap-2 mb-8">
        {filterTabs.map((tab) => (
          <Button
            key={tab.key}
            onClick={() => setActiveFilter(tab.key)}
            variant={activeFilter === tab.key ? 'default' : 'outline'}
            className={`rounded-full px-4 py-2 h-auto text-sm font-medium ${
              activeFilter === tab.key
                ? 'bg-gradient-to-r from-brand-purple to-brand-purple-deep text-white shadow-lg hover:from-brand-purple hover:to-brand-purple-deep'
                : 'bg-white border-slate-200 text-slate-600 hover:bg-slate-50'
            }`}
          >
            {tab.label}
          </Button>
        ))}
      </div>

      {/* Asset Cards Grid */}
      {filteredAssets.length === 0 ? (
        <div className="mb-12">
          <EmptyState size="md" />
        </div>
      ) : (
      <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 mb-12">
        {filteredAssets.map((asset, i) => {
          const statusInfo = statusConfig[asset.status];
          return (
            <motion.div
              key={asset.id}
              className="rounded-2xl border border-slate-100 bg-white shadow-sm p-5 cursor-pointer hover:shadow-[3px_4px_12px_rgba(0,0,0,0.06)] hover:border-brand-tint-lavender transition-all"
              onClick={() => setSelectedAsset({ kind: 'asset', data: asset })}
              animate={{ opacity: 1, y: 0 }}
              transition={{ duration: 0.4, delay: i * 0.05 }}
            >
              {/* Top badges row */}
              <div className="flex items-center gap-2 mb-3 flex-wrap">
                <span
                  className={`rounded-full px-3 py-1 text-xs font-medium ${sourceBadgeColors[asset.source]}`}
                >
                  {asset.sourceLabel}
                </span>
                <span
                  className={`rounded-full px-3 py-1 text-xs font-medium ${typeBadgeColors[asset.type]}`}
                >
                  {asset.type}
                </span>
                <span
                  className={`rounded-full px-3 py-1 text-xs font-medium ml-auto ${statusInfo.className}`}
                >
                  {statusInfo.label}
                </span>
              </div>

              {/* Title & Description */}
              <h4 className="font-semibold text-brand-navy mb-1">{asset.title}</h4>
              <p className="text-sm text-slate-600 mb-3">{asset.description}</p>

              {/* Repurposing suggestions */}
              {asset.repurposingSuggestions.length > 0 && (
                <div>
                  <p className="text-xs font-semibold text-slate-500 uppercase mb-2">
                    Repurposing &rarr;
                  </p>
                  <div className="flex flex-wrap gap-2">
                    {asset.repurposingSuggestions.map((suggestion, j) => (
                      <span
                        key={j}
                        className="rounded-full bg-brand-tint-purple text-brand-purple-muted text-xs px-2 py-1"
                      >
                        {suggestion}
                      </span>
                    ))}
                  </div>
                </div>
              )}
            </motion.div>
          );
        })}
      </div>
      )}

      {/* YouTube Repurpose Section */}
      {data.youtubeRepurpose.length > 0 && (
        <div>
          <h3 className="font-serif text-2xl font-bold text-brand-navy mb-4">
            YouTube Top Videos for Repurposing
          </h3>
          <div className="flex overflow-x-auto gap-4 pb-4">
            {data.youtubeRepurpose.map((video, i) => (
              <motion.div
                key={video.title}
                className="min-w-[280px] rounded-2xl border border-slate-100 bg-white shadow-sm p-5 shrink-0 cursor-pointer hover:shadow-[3px_4px_12px_rgba(0,0,0,0.06)] hover:border-brand-tint-lavender transition-all"
                onClick={() => setSelectedAsset({ kind: 'youtube', data: video })}
                animate={{ opacity: 1, x: 0 }}
                transition={{ duration: 0.4, delay: i * 0.1 }}
              >
                <div className="flex items-start gap-2 mb-3">
                  <YoutubeFilled size={18} className="text-brand-rose-mid shrink-0 mt-0.5" />
                  <h4 className="font-semibold text-sm text-brand-navy">{video.title}</h4>
                </div>
                <div className="flex items-center gap-2 mb-3">
                  <span className="rounded-full bg-slate-100 text-slate-700 px-3 py-1 text-xs font-medium">
                    {formatViews(video.views)} views
                  </span>
                  <span
                    className={`rounded-full px-3 py-1 text-xs font-medium ${
                      video.type === 'Short'
                        ? 'bg-brand-tint-purple text-brand-purple-muted'
                        : 'bg-brand-tint-violet text-brand-purple-faint'
                    }`}
                  >
                    {video.type}
                  </span>
                </div>
                <p className="text-xs font-semibold text-slate-500 uppercase mb-2">
                  Repurpose As:
                </p>
                <div className="flex flex-wrap gap-2">
                  {video.repurposeAs.map((suggestion, j) => (
                    <span
                      key={j}
                      className="rounded-full bg-brand-tint-purple text-brand-purple-muted text-xs px-2 py-1"
                    >
                      {suggestion}
                    </span>
                  ))}
                </div>
              </motion.div>
            ))}
          </div>
        </div>
      )}
      <AssetDetailModal
        asset={selectedAsset}
        onClose={() => setSelectedAsset(null)}
      />
    </SectionWrapper>
  );
}
