'use client'

import { ChevronRight } from 'lucide-react'
import type { BacklogItem, BacklogCategory } from '../types'
import { statusLabels, priorityLabels } from '../data'

interface BacklogItemCardProps {
  item: BacklogItem
  category: BacklogCategory | undefined
  isExpanded: boolean
  onToggleExpand: (id: string) => void
  onUpdateStatus: (id: string, status: BacklogItem['status']) => void
  onToggleSubtask: (itemId: string, subtaskId: string) => void
}

export function BacklogItemCard({
  item,
  category,
  isExpanded,
  onToggleExpand,
  onUpdateStatus,
  onToggleSubtask,
}: BacklogItemCardProps) {
  const completedSubtasks = item.subtasks?.filter((st) => st.completed).length || 0
  const totalSubtasks = item.subtasks?.length || 0

  return (
    <div className="bg-white rounded-xl border border-slate-200 overflow-hidden">
      <div
        className="p-4 cursor-pointer hover:bg-slate-50 transition-colors"
        onClick={() => onToggleExpand(item.id)}
      >
        <div className="flex items-start gap-3">
          {/* Expand Icon */}
          <button className="mt-1 text-slate-400">
            <ChevronRight
              className={`w-5 h-5 transition-transform ${isExpanded ? 'rotate-90' : ''}`}
            />
          </button>

          {/* Content */}
          <div className="flex-1 min-w-0">
            <div className="flex items-center gap-2 mb-1 flex-wrap">
              <h3 className="font-semibold text-slate-900">{item.title}</h3>
              <span
                className={`px-2 py-0.5 rounded text-xs font-medium ${
                  priorityLabels[item.priority].color
                }`}
              >
                {priorityLabels[item.priority].label}
              </span>
            </div>
            <p className="text-sm text-slate-500 mb-2">{item.description}</p>
            {item.notes && (
              <p className="text-xs text-slate-400 mb-2 italic">{item.notes}</p>
            )}
            <div className="flex items-center gap-3 text-xs">
              <span className={`px-2 py-1 rounded border ${category?.bgColor}`}>
                {category?.name}
              </span>
              {totalSubtasks > 0 && (
                <span className="text-slate-500">
                  {completedSubtasks}/{totalSubtasks} Teilaufgaben
                </span>
              )}
            </div>
          </div>

          {/* Status */}
          <select
            value={item.status}
            onChange={(e) => {
              e.stopPropagation()
              onUpdateStatus(item.id, e.target.value as BacklogItem['status'])
            }}
            onClick={(e) => e.stopPropagation()}
            className={`px-3 py-1.5 rounded-lg text-sm font-medium border-0 cursor-pointer ${
              statusLabels[item.status].color
            }`}
          >
            {Object.entries(statusLabels).map(([value, { label }]) => (
              <option key={value} value={value}>
                {label}
              </option>
            ))}
          </select>
        </div>

        {/* Progress Bar */}
        {totalSubtasks > 0 && (
          <div className="mt-3 ml-8">
            <div className="w-full bg-slate-200 rounded-full h-1.5">
              <div
                className="bg-green-500 h-1.5 rounded-full transition-all"
                style={{ width: `${(completedSubtasks / totalSubtasks) * 100}%` }}
              />
            </div>
          </div>
        )}
      </div>

      {/* Expanded Subtasks */}
      {isExpanded && item.subtasks && item.subtasks.length > 0 && (
        <div className="border-t border-slate-200 bg-slate-50 p-4 pl-12">
          <h4 className="text-sm font-medium text-slate-700 mb-3">Teilaufgaben</h4>
          <ul className="space-y-2">
            {item.subtasks.map((subtask) => (
              <li key={subtask.id} className="flex items-center gap-3">
                <input
                  type="checkbox"
                  checked={subtask.completed}
                  onChange={() => onToggleSubtask(item.id, subtask.id)}
                  className="w-4 h-4 rounded border-slate-300 text-blue-600 focus:ring-blue-500"
                />
                <span
                  className={`text-sm ${
                    subtask.completed ? 'text-slate-400 line-through' : 'text-slate-700'
                  }`}
                >
                  {subtask.title}
                </span>
              </li>
            ))}
          </ul>
        </div>
      )}
    </div>
  )
}
