"use client";

/**
 * V2Media 설정 패널
 * 토스식 단계별 UX: 미디어 타입 카드 선택 -> 기본 설정 -> 타입별 설정 -> 고급 설정(접힘)
 */

import React, { useState } from "react";
import { Input } from "@/components/ui/input";
import { Switch } from "@/components/ui/switch";
import {
  Collapsible,
  CollapsibleContent,
  CollapsibleTrigger,
} from "@/components/ui/collapsible";
import {
  FileText,
  Image,
  Video,
  Music,
  Settings,
  ChevronDown,
} from "lucide-react";
import { cn } from "@/lib/utils";

// ─── 미디어 타입 카드 정의 ───
const MEDIA_TYPE_CARDS = [
  {
    value: "file",
    icon: FileText,
    title: "파일",
    description: "일반 파일을 업로드해요",
  },
  {
    value: "image",
    icon: Image,
    title: "이미지",
    description: "사진이나 그림을 올려요",
  },
  {
    value: "video",
    icon: Video,
    title: "비디오",
    description: "동영상을 업로드해요",
  },
  {
    value: "audio",
    icon: Music,
    title: "오디오",
    description: "음악이나 녹음을 올려요",
  },
] as const;

interface V2MediaConfigPanelProps {
  config: Record<string, any>;
  onChange: (config: Record<string, any>) => void;
}

export const V2MediaConfigPanel: React.FC<V2MediaConfigPanelProps> = ({
  config,
  onChange,
}) => {
  const [advancedOpen, setAdvancedOpen] = useState(false);

  const updateConfig = (field: string, value: any) => {
    onChange({ ...config, [field]: value });
  };

  const currentMediaType = config.mediaType || config.type || "image";
  const isImageType = currentMediaType === "image";
  const isPlayerType = currentMediaType === "video" || currentMediaType === "audio";

  return (
    <div className="space-y-4">
      {/* ─── 1단계: 미디어 타입 선택 (카드) ─── */}
      <div className="space-y-2">
        <p className="text-sm font-medium">어떤 미디어를 업로드하나요?</p>
        <div className="grid grid-cols-2 gap-2">
          {MEDIA_TYPE_CARDS.map((card) => {
            const Icon = card.icon;
            const isSelected = currentMediaType === card.value;
            return (
              <button
                key={card.value}
                type="button"
                onClick={() => updateConfig("mediaType", card.value)}
                className={cn(
                  "flex flex-col items-center justify-center rounded-lg border p-3 text-center transition-all min-h-[80px]",
                  isSelected
                    ? "border-primary bg-primary/5 ring-1 ring-primary/20"
                    : "border-border hover:border-primary/50 hover:bg-muted/50",
                )}
              >
                <Icon className="h-5 w-5 mb-1.5 text-primary" />
                <span className="text-xs font-medium leading-tight">
                  {card.title}
                </span>
                <span className="text-[10px] text-muted-foreground leading-tight mt-0.5">
                  {card.description}
                </span>
              </button>
            );
          })}
        </div>
      </div>

      {/* ─── 2단계: 기본 설정 ─── */}
      <div className="rounded-lg border bg-muted/30 p-4 space-y-3">
        <span className="text-sm font-medium">파일 설정</span>

        <div className="flex items-center justify-between py-1.5">
          <span className="text-xs text-muted-foreground">허용 형식</span>
          <Input
            value={config.accept || ""}
            onChange={(e) => updateConfig("accept", e.target.value)}
            placeholder=".jpg,.png,.pdf"
            className="h-8 w-[180px] text-sm"
          />
        </div>
      </div>

      {/* ─── 3단계: 업로드 옵션 (Switch + 설명) ─── */}
      <div className="space-y-2">
        <div className="flex items-center justify-between py-1">
          <div>
            <p className="text-sm">여러 파일 업로드</p>
            <p className="text-[11px] text-muted-foreground">
              한 번에 여러 파일을 선택할 수 있어요
            </p>
          </div>
          <Switch
            checked={config.multiple || false}
            onCheckedChange={(checked) => updateConfig("multiple", checked)}
          />
        </div>

        <div className="flex items-center justify-between py-1">
          <div>
            <p className="text-sm">미리보기</p>
            <p className="text-[11px] text-muted-foreground">
              업로드한 파일의 미리보기가 표시돼요
            </p>
          </div>
          <Switch
            checked={config.preview !== false}
            onCheckedChange={(checked) => updateConfig("preview", checked)}
          />
        </div>

        <div className="flex items-center justify-between py-1">
          <div>
            <p className="text-sm">드래그 앤 드롭</p>
            <p className="text-[11px] text-muted-foreground">
              파일을 끌어다 놓아서 업로드할 수 있어요
            </p>
          </div>
          <Switch
            checked={config.dragDrop !== false}
            onCheckedChange={(checked) => updateConfig("dragDrop", checked)}
          />
        </div>
      </div>

      {/* ─── 4단계: 타입별 설정 ─── */}

      {/* 이미지 타입: 크기 제한 + 자르기 */}
      {isImageType && (
        <div className="space-y-3">
          <div className="rounded-lg border bg-muted/30 p-4 space-y-3">
            <div className="flex items-center gap-2">
              <Image className="h-4 w-4 text-primary" />
              <span className="text-sm font-medium">이미지 설정</span>
            </div>

            <div className="flex gap-2">
              <div className="flex-1">
                <span className="text-xs text-muted-foreground">최대 너비 (px)</span>
                <Input
                  type="number"
                  value={config.maxWidth || ""}
                  onChange={(e) => updateConfig("maxWidth", e.target.value ? Number(e.target.value) : undefined)}
                  placeholder="자동"
                  className="mt-1 h-8 text-sm"
                />
              </div>
              <div className="flex-1">
                <span className="text-xs text-muted-foreground">최대 높이 (px)</span>
                <Input
                  type="number"
                  value={config.maxHeight || ""}
                  onChange={(e) => updateConfig("maxHeight", e.target.value ? Number(e.target.value) : undefined)}
                  placeholder="자동"
                  className="mt-1 h-8 text-sm"
                />
              </div>
            </div>
          </div>

          <div className="flex items-center justify-between py-1">
            <div>
              <p className="text-sm">자르기 기능</p>
              <p className="text-[11px] text-muted-foreground">
                이미지를 원하는 크기로 잘라서 올릴 수 있어요
              </p>
            </div>
            <Switch
              checked={config.crop || false}
              onCheckedChange={(checked) => updateConfig("crop", checked)}
            />
          </div>
        </div>
      )}

      {/* 비디오/오디오 타입: 플레이어 설정 */}
      {isPlayerType && (
        <div className="space-y-2">
          <div className="rounded-lg border bg-muted/30 p-4">
            <div className="flex items-center gap-2">
              {currentMediaType === "video" ? (
                <Video className="h-4 w-4 text-primary" />
              ) : (
                <Music className="h-4 w-4 text-primary" />
              )}
              <span className="text-sm font-medium">플레이어 설정</span>
            </div>
          </div>

          <div className="flex items-center justify-between py-1">
            <div>
              <p className="text-sm">자동 재생</p>
              <p className="text-[11px] text-muted-foreground">
                페이지를 열면 자동으로 재생돼요
              </p>
            </div>
            <Switch
              checked={config.autoplay || false}
              onCheckedChange={(checked) => updateConfig("autoplay", checked)}
            />
          </div>

          <div className="flex items-center justify-between py-1">
            <div>
              <p className="text-sm">컨트롤 표시</p>
              <p className="text-[11px] text-muted-foreground">
                재생, 정지, 볼륨 등 조작 버튼이 보여요
              </p>
            </div>
            <Switch
              checked={config.controls !== false}
              onCheckedChange={(checked) => updateConfig("controls", checked)}
            />
          </div>

          <div className="flex items-center justify-between py-1">
            <div>
              <p className="text-sm">반복 재생</p>
              <p className="text-[11px] text-muted-foreground">
                끝나면 처음부터 다시 재생해요
              </p>
            </div>
            <Switch
              checked={config.loop || false}
              onCheckedChange={(checked) => updateConfig("loop", checked)}
            />
          </div>
        </div>
      )}

      {/* ─── 5단계: 고급 설정 (기본 접혀있음) ─── */}
      <Collapsible open={advancedOpen} onOpenChange={setAdvancedOpen}>
        <CollapsibleTrigger asChild>
          <button
            type="button"
            className="flex w-full items-center justify-between rounded-lg border bg-muted/30 px-4 py-2.5 text-left transition-colors hover:bg-muted/50"
          >
            <div className="flex items-center gap-2">
              <Settings className="h-4 w-4 text-muted-foreground" />
              <span className="text-sm font-medium">고급 설정</span>
            </div>
            <ChevronDown
              className={cn(
                "h-4 w-4 text-muted-foreground transition-transform duration-200",
                advancedOpen && "rotate-180",
              )}
            />
          </button>
        </CollapsibleTrigger>
        <CollapsibleContent>
          <div className="rounded-b-lg border border-t-0 p-4 space-y-3">
            <div className="flex items-center justify-between py-1.5">
              <span className="text-xs text-muted-foreground">최대 크기 (MB)</span>
              <Input
                type="number"
                value={config.maxSize || ""}
                onChange={(e) => updateConfig("maxSize", e.target.value ? Number(e.target.value) : undefined)}
                placeholder="10"
                min="1"
                className="h-8 w-[180px] text-sm"
              />
            </div>

            <div className="flex items-center justify-between py-1.5">
              <span className="text-xs text-muted-foreground">최대 파일 수</span>
              <Input
                type="number"
                value={config.maxFiles || ""}
                onChange={(e) => updateConfig("maxFiles", e.target.value ? Number(e.target.value) : undefined)}
                placeholder="제한 없음"
                min="1"
                className="h-8 w-[180px] text-sm"
              />
            </div>
          </div>
        </CollapsibleContent>
      </Collapsible>
    </div>
  );
};

V2MediaConfigPanel.displayName = "V2MediaConfigPanel";

export default V2MediaConfigPanel;
