'use client';

import { useState } from "react";
import { Input } from "@/components/ui/input";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select";
import { ChevronDown } from "lucide-react";

const emailDomains = [
  'gmail.com',
  'naver.com',
  'daum.net',
  'hanmail.net',
  'nate.com',
  'kakao.com',
  '직접입력',
];

// 시안 1: 직접입력 시 별도 행에 입력창
function EmailDomain1() {
  const [emailId, setEmailId] = useState('');
  const [emailDomain, setEmailDomain] = useState('');
  const [customDomain, setCustomDomain] = useState('');

  return (
    <div className="space-y-4">
      <h3 className="font-semibold">시안 1: 직접입력 시 별도 행</h3>
      <div className="flex flex-col gap-2">
        <div className="flex gap-2 items-center">
          <Input
            placeholder="이메일"
            value={emailId}
            onChange={(e) => setEmailId(e.target.value)}
            className="flex-1 h-11"
          />
          <span className="text-muted-foreground">@</span>
          <Select value={emailDomain} onValueChange={setEmailDomain}>
            <SelectTrigger className="flex-1 h-11">
              <SelectValue placeholder="도메인 선택" />
            </SelectTrigger>
            <SelectContent>
              {emailDomains.map((domain) => (
                <SelectItem key={domain} value={domain}>
                  {domain}
                </SelectItem>
              ))}
            </SelectContent>
          </Select>
        </div>
        {emailDomain === '직접입력' && (
          <Input
            placeholder="도메인을 입력하세요 (예: company.com)"
            value={customDomain}
            onChange={(e) => setCustomDomain(e.target.value)}
            className="h-11"
          />
        )}
      </div>
      <p className="text-sm text-muted-foreground">
        결과: {emailId}@{emailDomain === '직접입력' ? customDomain : emailDomain}
      </p>
    </div>
  );
}

// 시안 2: 직접입력 시 드롭다운 자리에 인풋 + 옆에 드롭다운 버튼
function EmailDomain2() {
  const [emailId, setEmailId] = useState('');
  const [emailDomain, setEmailDomain] = useState('');
  const [customDomain, setCustomDomain] = useState('');
  const [isSelectOpen, setIsSelectOpen] = useState(false);

  return (
    <div className="space-y-4">
      <h3 className="font-semibold">시안 2: 인풋 + 별도 드롭다운 버튼</h3>
      <div className="flex gap-2 items-center">
        <Input
          placeholder="이메일"
          value={emailId}
          onChange={(e) => setEmailId(e.target.value)}
          className="flex-1 h-11"
        />
        <span className="text-muted-foreground">@</span>
        {emailDomain === '직접입력' ? (
          <div className="flex flex-1 gap-1">
            <Input
              placeholder="도메인 입력"
              value={customDomain}
              onChange={(e) => setCustomDomain(e.target.value)}
              className="flex-1 h-11"
            />
            <Select value={emailDomain} onValueChange={(v) => {
              setEmailDomain(v);
              if (v !== '직접입력') setCustomDomain('');
            }}>
              <SelectTrigger className="w-11 h-11 px-0 justify-center">
                <ChevronDown className="h-4 w-4" />
              </SelectTrigger>
              <SelectContent>
                {emailDomains.map((domain) => (
                  <SelectItem key={domain} value={domain}>
                    {domain}
                  </SelectItem>
                ))}
              </SelectContent>
            </Select>
          </div>
        ) : (
          <Select value={emailDomain} onValueChange={setEmailDomain}>
            <SelectTrigger className="flex-1 h-11">
              <SelectValue placeholder="도메인 선택" />
            </SelectTrigger>
            <SelectContent>
              {emailDomains.map((domain) => (
                <SelectItem key={domain} value={domain}>
                  {domain}
                </SelectItem>
              ))}
            </SelectContent>
          </Select>
        )}
      </div>
      <p className="text-sm text-muted-foreground">
        결과: {emailId}@{emailDomain === '직접입력' ? customDomain : emailDomain}
      </p>
    </div>
  );
}

// 시안 3: Combobox 스타일 - 인풋이면서 드롭다운
function EmailDomain3() {
  const [emailId, setEmailId] = useState('');
  const [emailDomain, setEmailDomain] = useState('');
  const [customDomain, setCustomDomain] = useState('');
  const [isOpen, setIsOpen] = useState(false);

  const displayValue = emailDomain === '직접입력' ? customDomain : emailDomain;

  return (
    <div className="space-y-4">
      <h3 className="font-semibold">시안 3: Combobox 스타일 (인풋 + 드롭다운 통합)</h3>
      <div className="flex gap-2 items-center">
        <Input
          placeholder="이메일"
          value={emailId}
          onChange={(e) => setEmailId(e.target.value)}
          className="flex-1 h-11"
        />
        <span className="text-muted-foreground">@</span>
        <div className="relative flex-1">
          <Input
            placeholder="도메인 선택 또는 입력"
            value={displayValue}
            onChange={(e) => {
              setEmailDomain('직접입력');
              setCustomDomain(e.target.value);
            }}
            onFocus={() => setIsOpen(true)}
            className="h-11 pr-10"
          />
          <Select
            value={emailDomain}
            onValueChange={(v) => {
              setEmailDomain(v);
              if (v !== '직접입력') setCustomDomain('');
              setIsOpen(false);
            }}
            open={isOpen}
            onOpenChange={setIsOpen}
          >
            <SelectTrigger className="absolute right-0 top-0 w-10 h-11 border-0 bg-transparent hover:bg-transparent focus:ring-0">
              <ChevronDown className="h-4 w-4" />
            </SelectTrigger>
            <SelectContent>
              {emailDomains.map((domain) => (
                <SelectItem key={domain} value={domain}>
                  {domain}
                </SelectItem>
              ))}
            </SelectContent>
          </Select>
        </div>
      </div>
      <p className="text-sm text-muted-foreground">
        결과: {emailId}@{displayValue}
      </p>
    </div>
  );
}

// 시안 4: 드롭다운 영역과 입력 영역 분리
function EmailDomain4() {
  const [emailId, setEmailId] = useState('');
  const [emailDomain, setEmailDomain] = useState('');
  const [customDomain, setCustomDomain] = useState('');

  return (
    <div className="space-y-4">
      <h3 className="font-semibold">시안 4: 드롭다운 + 직접입력 시 인풋으로 교체 (동일 너비)</h3>
      <div className="flex gap-2 items-center">
        <Input
          placeholder="이메일"
          value={emailId}
          onChange={(e) => setEmailId(e.target.value)}
          className="w-[140px] h-11"
        />
        <span className="text-muted-foreground shrink-0">@</span>
        <div className="flex-1 flex gap-1">
          {emailDomain === '직접입력' ? (
            <Input
              placeholder="도메인 입력"
              value={customDomain}
              onChange={(e) => setCustomDomain(e.target.value)}
              className="flex-1 h-11"
            />
          ) : (
            <div className="flex-1" />
          )}
          <Select
            value={emailDomain}
            onValueChange={(v) => {
              setEmailDomain(v);
              if (v !== '직접입력') setCustomDomain('');
            }}
          >
            <SelectTrigger className={emailDomain === '직접입력' ? "w-[100px] h-11" : "w-full h-11"}>
              <SelectValue placeholder="도메인 선택" />
            </SelectTrigger>
            <SelectContent>
              {emailDomains.map((domain) => (
                <SelectItem key={domain} value={domain}>
                  {domain}
                </SelectItem>
              ))}
            </SelectContent>
          </Select>
        </div>
      </div>
      <p className="text-sm text-muted-foreground">
        결과: {emailId}@{emailDomain === '직접입력' ? customDomain : emailDomain}
      </p>
    </div>
  );
}

// 시안 5: 인풋과 드롭다운 자연스럽게 통합 (하나의 필드처럼 보이게)
function EmailDomain5() {
  const [emailId, setEmailId] = useState('');
  const [emailDomain, setEmailDomain] = useState('');
  const [customDomain, setCustomDomain] = useState('');

  return (
    <div className="space-y-4">
      <h3 className="font-semibold">시안 5: 인풋 + 드롭다운 자연스럽게 통합</h3>
      <div className="flex gap-2 items-center">
        <Input
          placeholder="이메일"
          value={emailId}
          onChange={(e) => setEmailId(e.target.value)}
          className="flex-1 h-11"
        />
        <span className="text-muted-foreground shrink-0">@</span>
        <div className="flex items-center flex-1 h-11 border rounded-md focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2">
          {emailDomain === '직접입력' ? (
            <Input
              placeholder="도메인 입력"
              value={customDomain}
              onChange={(e) => setCustomDomain(e.target.value)}
              className="flex-1 h-full border-0 focus-visible:ring-0 focus-visible:ring-offset-0 rounded-r-none"
            />
          ) : (
            <span className="flex-1 px-3 text-sm truncate">
              {emailDomain || <span className="text-muted-foreground">도메인 선택</span>}
            </span>
          )}
          <Select
            value={emailDomain}
            onValueChange={(v) => {
              setEmailDomain(v);
              if (v !== '직접입력') setCustomDomain('');
            }}
          >
            <SelectTrigger className="w-10 h-full border-0 bg-transparent px-0 focus:ring-0 rounded-l-none justify-center" />
            <SelectContent>
              {emailDomains.map((domain) => (
                <SelectItem key={domain} value={domain}>
                  {domain}
                </SelectItem>
              ))}
            </SelectContent>
          </Select>
        </div>
      </div>
      <p className="text-sm text-muted-foreground">
        결과: {emailId}@{emailDomain === '직접입력' ? customDomain : emailDomain}
      </p>
    </div>
  );
}

export default function EmailDomainDemo() {
  return (
    <div className="min-h-screen bg-gray-100 p-8">
      <div className="max-w-2xl mx-auto space-y-8">
        <div>
          <h1 className="text-2xl font-bold">이메일 도메인 입력 UI 시안</h1>
          <p className="text-muted-foreground mt-2">
            직접입력 선택 시 인풋과 드롭다운이 함께 동작하는 다양한 방식
          </p>
        </div>

        <div className="bg-white p-6 rounded-lg border space-y-8">
          <EmailDomain1 />
          <hr />
          <EmailDomain2 />
          <hr />
          <EmailDomain3 />
          <hr />
          <EmailDomain4 />
          <hr />
          <EmailDomain5 />
        </div>
      </div>
    </div>
  );
}
