import { type ComponentType } from 'react';
import {
  YoutubeFilled,
  InstagramFilled,
  FacebookFilled,
  GlobeFilled,
  TiktokFilled,
} from '@/shared/icons/FilledIcons';
import { CHANNEL_OPTIONS, type StudioChannel, type ContentFormat } from '@/features/studio/types/studio';
import { Button } from '@/shared/ui/button';

interface Props {
  selectedChannel: StudioChannel | null;
  selectedFormat: ContentFormat | null;
  onChannelSelect: (ch: StudioChannel | null) => void;
  onFormatSelect: (fmt: ContentFormat | null) => void;
}

const iconMap: Record<string, ComponentType<{ size?: number; className?: string }>> = {
  youtube: YoutubeFilled,
  instagram: InstagramFilled,
  facebook: FacebookFilled,
  globe: GlobeFilled,
  tiktok: TiktokFilled,
};

export default function ChannelFormatStep({ selectedChannel, selectedFormat, onChannelSelect, onFormatSelect }: Props) {
  const activeChannel = CHANNEL_OPTIONS.find(c => c.channel === selectedChannel);

  return (
    <div>
      {/* Channel Selection */}
      <div className="mb-10">
        <h3 className="font-serif font-bold text-2xl text-brand-navy mb-2">채널 선택</h3>
        <p className="text-sm text-slate-500 mb-6">콘텐츠를 게시할 채널을 선택하세요</p>

        <div className="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-5 gap-4">
          {CHANNEL_OPTIONS.map(ch => {
            const Icon = iconMap[ch.icon] ?? GlobeFilled;
            const isSelected = selectedChannel === ch.channel;

            return (
              <Button
                key={ch.channel}
                variant="ghost"
                onClick={() => {
                  onChannelSelect(isSelected ? null : ch.channel);
                  onFormatSelect(null);
                }}
                className={`relative flex flex-col items-center gap-3 p-6 h-auto rounded-2xl border-2 whitespace-normal transition-all hover:bg-transparent ${
                  isSelected
                    ? 'border-brand-purple-vivid bg-brand-tint-purple/30 shadow-[3px_4px_12px_rgba(108,92,231,0.12)]'
                    : 'border-slate-100 bg-white shadow-[3px_4px_12px_rgba(0,0,0,0.06)] hover:border-brand-tint-lavender'
                }`}
              >
                {isSelected && (
                  <div className="absolute top-3 right-3 w-5 h-5 rounded-full bg-brand-purple-vivid flex items-center justify-center">
                    <svg width="10" height="10" viewBox="0 0 14 14" fill="none">
                      <path d="M2 7L5.5 10.5L12 3.5" stroke="white" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round" />
                    </svg>
                  </div>
                )}
                <div className={`w-12 h-12 rounded-xl flex items-center justify-center ${
                  isSelected ? 'bg-brand-tint-purple' : 'bg-slate-50'
                }`}>
                  <Icon size={24} className={isSelected ? 'text-brand-purple-vivid' : 'text-brand-purple-soft'} />
                </div>
                <span className={`text-sm font-semibold ${
                  isSelected ? 'text-brand-navy' : 'text-slate-600'
                }`}>
                  {ch.label}
                </span>
              </Button>
            );
          })}
        </div>
      </div>

      {/* Format Selection */}
      {activeChannel && (
        <div>
          <h3 className="font-serif font-bold text-2xl text-brand-navy mb-2">포맷 선택</h3>
          <p className="text-sm text-slate-500 mb-6">{activeChannel.label}에 적합한 콘텐츠 포맷을 선택하세요</p>

          <div className="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-4 gap-4">
            {activeChannel.formats.map(fmt => {
              const isSelected = selectedFormat === fmt.key;

              return (
                <Button
                  key={fmt.key}
                  variant="ghost"
                  onClick={() => onFormatSelect(isSelected ? null : fmt.key)}
                  className={`relative flex flex-col items-center gap-3 p-5 h-auto rounded-2xl border-2 whitespace-normal transition-all hover:bg-transparent ${
                    isSelected
                      ? 'border-brand-purple-vivid bg-brand-tint-purple/30 shadow-[3px_4px_12px_rgba(108,92,231,0.12)]'
                      : 'border-slate-100 bg-white shadow-[3px_4px_12px_rgba(0,0,0,0.06)] hover:border-brand-tint-lavender'
                  }`}
                >
                  {isSelected && (
                    <div className="absolute top-3 right-3 w-5 h-5 rounded-full bg-brand-purple-vivid flex items-center justify-center">
                      <svg width="10" height="10" viewBox="0 0 14 14" fill="none">
                        <path d="M2 7L5.5 10.5L12 3.5" stroke="white" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round" />
                      </svg>
                    </div>
                  )}

                  {/* Aspect ratio preview */}
                  <div className={`rounded-lg bg-slate-100 border border-slate-200 flex items-center justify-center ${
                    fmt.aspectRatio === '9:16' ? 'w-10 h-16' :
                    fmt.aspectRatio === '16:9' ? 'w-16 h-10' :
                    fmt.aspectRatio === '4:5' ? 'w-12 h-14' :
                    'w-12 h-12'
                  }`}>
                    <span className="text-xs text-slate-400 font-medium">{fmt.aspectRatio}</span>
                  </div>

                  <span className={`text-sm font-semibold ${
                    isSelected ? 'text-brand-navy' : 'text-slate-600'
                  }`}>
                    {fmt.label}
                  </span>
                </Button>
              );
            })}
          </div>
        </div>
      )}
    </div>
  );
}
