"use client";

import React, { useState, useEffect } from "react";
import { Plus, Check, X, Clock, AlertCircle, Calendar as CalendarIcon, Wrench, Truck } from "lucide-react";
import { DashboardElement } from "@/components/admin/dashboard/types";
import { useDashboard } from "@/contexts/DashboardContext";

interface TaskItem {
  id: string;
  title: string;
  description?: string;
  priority: "urgent" | "high" | "normal" | "low";
  status: "pending" | "in_progress" | "completed" | "overdue";
  assignedTo?: string;
  dueDate?: string;
  createdAt: string;
  updatedAt: string;
  completedAt?: string;
  isUrgent: boolean;
  order: number;
  // 정비 일정 전용 필드
  vehicleNumber?: string;
  vehicleType?: string;
  maintenanceType?: string;
  estimatedCost?: number;
}

interface TaskStats {
  total: number;
  pending: number;
  inProgress: number;
  completed: number;
  urgent: number;
  overdue: number;
}

interface TaskWidgetProps {
  element?: DashboardElement;
}

export default function TaskWidget({ element }: TaskWidgetProps) {
  const { selectedDate } = useDashboard();
  
  const [tasks, setTasks] = useState<TaskItem[]>([]);
  const [internalTasks, setInternalTasks] = useState<TaskItem[]>([]);
  const [stats, setStats] = useState<TaskStats | null>(null);
  const [loading, setLoading] = useState(true);
  const [filter, setFilter] = useState<"all" | "pending" | "in_progress" | "completed">("all");
  const [showAddForm, setShowAddForm] = useState(false);
  const [newTask, setNewTask] = useState({
    title: "",
    description: "",
    priority: "normal" as TaskItem["priority"],
    isUrgent: false,
    dueDate: "",
    assignedTo: "",
    vehicleNumber: "",
    vehicleType: "",
    maintenanceType: "",
    estimatedCost: 0,
  });

  // 범용 위젯이므로 모드 구분 제거 - 모든 필드를 선택적으로 사용

  useEffect(() => {
    fetchTasks();
    const interval = setInterval(fetchTasks, 30000);
    return () => clearInterval(interval);
  }, [selectedDate]); // filter 제거 - 프론트엔드에서만 필터링

  const fetchTasks = async () => {
    try {
      const token = localStorage.getItem("authToken");
      const userLang = localStorage.getItem("userLang") || "KR";
      
      // 데이터베이스 쿼리가 있으면 DB에서만 가져오기
      if (element?.dataSource?.query) {
        const apiUrl = element.dataSource.connectionType === "external" && element.dataSource.externalConnectionId
          ? `http://localhost:9771/api/external-db/query?userLang=${userLang}`
          : `http://localhost:9771/api/dashboards/execute-query?userLang=${userLang}`;
        
        const requestBody = element.dataSource.connectionType === "external" && element.dataSource.externalConnectionId
          ? {
              connectionId: parseInt(element.dataSource.externalConnectionId),
              query: element.dataSource.query,
            }
          : { query: element.dataSource.query };

        const response = await fetch(apiUrl, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
            Authorization: `Bearer ${token}`,
          },
          body: JSON.stringify(requestBody),
        });

        if (response.ok) {
          const result = await response.json();
          const rows = result.data?.rows || result.data || [];
          const externalTasks = mapExternalDataToTasks(rows);
          setTasks(externalTasks); // DB 데이터만 사용
          setStats(calculateStatsFromTasks(externalTasks));
        }
      } else {
        // 쿼리가 없으면 내장 API 사용 (하위 호환성)
        const internalResponse = await fetch(`http://localhost:9771/api/todos`, {
          headers: { Authorization: `Bearer ${token}` },
        });
        
        if (internalResponse.ok) {
          const result = await internalResponse.json();
          const internalData = result.data || [];
          setInternalTasks(internalData);
          setTasks(internalData);
          setStats(calculateStatsFromTasks(internalData));
        }
      }
    } catch (error) {
      // console.error("Task 로딩 오류:", error);
    } finally {
      setLoading(false);
    }
  };

  const mapExternalDataToTasks = (data: any[]): TaskItem[] => {
    return data.map((row, index) => ({
      id: row.id || `task-${index}`,
      title: row.title || row.task || row.name || "제목 없음",
      description: row.description || row.desc || row.content || row.notes,
      priority: row.priority || "normal",
      status: row.status || "pending",
      assignedTo: row.assigned_to || row.assignedTo || row.user,
      dueDate: row.due_date || row.dueDate || row.deadline || row.scheduled_date || row.scheduledDate,
      createdAt: row.created_at || row.createdAt || new Date().toISOString(),
      updatedAt: row.updated_at || row.updatedAt || new Date().toISOString(),
      completedAt: row.completed_at || row.completedAt,
      isUrgent: row.is_urgent || row.isUrgent || row.urgent || false,
      order: row.display_order || row.order || index,
      vehicleNumber: row.vehicle_number || row.vehicleNumber,
      vehicleType: row.vehicle_type || row.vehicleType,
      maintenanceType: row.maintenance_type || row.maintenanceType,
      estimatedCost: row.estimated_cost || row.estimatedCost,
    }));
  };

  const calculateStatsFromTasks = (taskList: TaskItem[]): TaskStats => {
    return {
      total: taskList.length,
      pending: taskList.filter((t) => t.status === "pending").length,
      inProgress: taskList.filter((t) => t.status === "in_progress").length,
      completed: taskList.filter((t) => t.status === "completed").length,
      urgent: taskList.filter((t) => t.isUrgent).length,
      overdue: taskList.filter((t) => {
        if (!t.dueDate) return false;
        return new Date(t.dueDate) < new Date() && t.status !== "completed";
      }).length,
    };
  };

  const handleAddTask = async () => {
    if (!newTask.title.trim()) return;

    try {
      const token = localStorage.getItem("authToken");
      const userLang = localStorage.getItem("userLang") || "KR";

      // 데이터베이스 저장 (간편/고급 모드만 지원)
      let insertQuery = "";

      console.log("🔍 데이터베이스 연동 확인:", {
        enableDbSync: element?.chartConfig?.enableDbSync,
        dbSyncMode: element?.chartConfig?.dbSyncMode,
        tableName: element?.chartConfig?.tableName,
        hasInsertQuery: !!element?.chartConfig?.insertQuery,
      });

      // 1. 간편 모드: 사용자가 설정한 테이블/컬럼 사용
      if (element?.chartConfig?.enableDbSync && element.chartConfig.dbSyncMode === "simple" && element.chartConfig.tableName) {
        const table = element.chartConfig.tableName;
        const cols = element.chartConfig.columnMapping;
        
        const columns = [cols.title, cols.description, cols.priority, cols.status, cols.assignedTo, cols.dueDate, cols.isUrgent]
          .filter(Boolean)
          .join(", ");
        
        const values = [
          `'${newTask.title.replace(/'/g, "''")}'`,
          newTask.description ? `'${newTask.description.replace(/'/g, "''")}'` : "''",
          `'${newTask.priority}'`,
          "'pending'",
          newTask.assignedTo ? `'${newTask.assignedTo.replace(/'/g, "''")}'` : "''",
          newTask.dueDate ? `'${newTask.dueDate}'` : "NULL",
          newTask.isUrgent ? "TRUE" : "FALSE",
        ].filter((_, i) => [cols.title, cols.description, cols.priority, cols.status, cols.assignedTo, cols.dueDate, cols.isUrgent][i]);
        
        insertQuery = `INSERT INTO ${table} (${columns}) VALUES (${values.join(", ")})`;
        console.log("✅ 간편 모드 INSERT 쿼리 생성:", insertQuery);
      } 
      // 2. 고급 모드: 사용자가 입력한 쿼리 사용
      else if (element?.chartConfig?.enableDbSync && element.chartConfig.insertQuery) {
        insertQuery = element.chartConfig.insertQuery;
        insertQuery = insertQuery.replace(/\$\{title\}/g, newTask.title);
        insertQuery = insertQuery.replace(/\$\{description\}/g, newTask.description || '');
        insertQuery = insertQuery.replace(/\$\{priority\}/g, newTask.priority);
        insertQuery = insertQuery.replace(/\$\{status\}/g, 'pending');
        insertQuery = insertQuery.replace(/\$\{assignedTo\}/g, newTask.assignedTo || '');
        insertQuery = insertQuery.replace(/\$\{dueDate\}/g, newTask.dueDate || '');
        insertQuery = insertQuery.replace(/\$\{isUrgent\}/g, String(newTask.isUrgent));
        insertQuery = insertQuery.replace(/\$\{vehicleNumber\}/g, newTask.vehicleNumber || '');
        insertQuery = insertQuery.replace(/\$\{vehicleType\}/g, newTask.vehicleType || '');
        insertQuery = insertQuery.replace(/\$\{maintenanceType\}/g, newTask.maintenanceType || '');
        insertQuery = insertQuery.replace(/\$\{estimatedCost\}/g, String(newTask.estimatedCost || 0));
        console.log("✅ 고급 모드 INSERT 쿼리 생성:", insertQuery);
      }
      // 3. 쿼리 결과가 있으면 자동 생성
      else if (element?.dataSource?.query && tasks.length > 0) {
        const firstRow = tasks[0];
        const availableColumns = Object.keys(firstRow);
        console.log("🔍 쿼리 결과 컬럼:", availableColumns);

        // 테이블명 추출
        const selectMatch = element.dataSource.query.match(/FROM\s+(\w+)/i);
        let tableName = selectMatch ? selectMatch[1] : "unknown_table";

        // 필드 값 매핑 (camelCase와 snake_case 모두 대응)
        const fieldMapping: Record<string, any> = {
          title: newTask.title,
          description: newTask.description || '',
          priority: newTask.priority,
          status: 'pending',
          assignedTo: newTask.assignedTo || '',
          assigned_to: newTask.assignedTo || '',
          dueDate: newTask.dueDate || null,
          due_date: newTask.dueDate || null,
          isUrgent: newTask.isUrgent,
          is_urgent: newTask.isUrgent,
          createdAt: "NOW()",
          created_at: "NOW()",
          updatedAt: "NOW()",
          updated_at: "NOW()",
        };

        // camelCase를 snake_case로 변환
        const camelToSnake = (str: string): string => {
          return str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`);
        };

        // 쿼리 결과에 있는 컬럼만 매핑 (snake_case로 변환)
        const columns: string[] = [];
        const values: string[] = [];

        availableColumns.forEach(col => {
          // order는 제외하지만 id는 포함 (NOT NULL이므로)
          if (col === 'order') return;

          if (fieldMapping.hasOwnProperty(col)) {
            // camelCase를 snake_case로 변환
            const snakeCol = camelToSnake(col);
            columns.push(snakeCol);
            
            const val = fieldMapping[col];
            if (val === "NOW()") {
              values.push("NOW()");
            } else if (val === null) {
              values.push("NULL");
            } else if (typeof val === "boolean") {
              values.push(val ? "TRUE" : "FALSE");
            } else if (typeof val === "number") {
              values.push(String(val));
            } else {
              values.push(`'${String(val).replace(/'/g, "''")}'`);
            }
          }
        });

        // id가 없으면 UUID 생성
        if (!columns.includes('id')) {
          columns.unshift('id');
          values.unshift(`'${crypto.randomUUID()}'`);
        }

        // display_order가 없으면 0으로 추가
        if (!columns.includes('display_order')) {
          columns.push('display_order');
          values.push('0');
        }

        insertQuery = `INSERT INTO ${tableName} (${columns.join(", ")}) VALUES (${values.join(", ")})`;
        console.log("✅ 쿼리 결과 기반 자동 INSERT:", insertQuery);
      }
      // 4. 설정이 없으면 경고
      else {
        console.error("❌ 데이터베이스 연동 설정이 필요합니다!");
        alert("일정관리 위젯 속성에서 '데이터베이스 연동'을 설정해주세요.\n\n간편 모드: 테이블명과 컬럼 매핑 입력\n고급 모드: INSERT 쿼리 직접 작성\n\n또는 쿼리 결과가 있으면 자동으로 생성됩니다.");
        return;
      }

      // 쿼리 실행 (모든 경우 처리: 내장 DB, 외부 DB, API)
      if (insertQuery) {
        // 외부 데이터베이스 or 내장 데이터베이스
        const apiUrl = element?.dataSource?.connectionType === "external" && element.dataSource.externalConnectionId
          ? `http://localhost:9771/api/external-db/execute?userLang=${userLang}`
          : `http://localhost:9771/api/dashboards/execute-dml?userLang=${userLang}`;

        const requestBody = element?.dataSource?.connectionType === "external" && element.dataSource.externalConnectionId
          ? {
              connectionId: parseInt(element.dataSource.externalConnectionId),
              query: insertQuery,
            }
          : { query: insertQuery };

        console.log("📤 데이터베이스 INSERT 요청:", { apiUrl, requestBody });

        const response = await fetch(apiUrl, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
            Authorization: `Bearer ${token}`,
          },
          body: JSON.stringify(requestBody),
        });

        console.log("📥 데이터베이스 응답:", { status: response.status, ok: response.ok });

        if (response.ok) {
          const result = await response.json();
          console.log("✅ 데이터베이스 INSERT 성공:", result);
          setNewTask({
            title: "",
            description: "",
            priority: "normal",
            isUrgent: false,
            dueDate: "",
            assignedTo: "",
            vehicleNumber: "",
            vehicleType: "",
            maintenanceType: "",
            estimatedCost: 0,
          });
          setShowAddForm(false);
          fetchTasks();
        } else {
          const errorText = await response.text();
          console.error("❌ 데이터베이스 INSERT 실패:", { status: response.status, error: errorText });
        }
      } else {
        console.error("❌ INSERT 쿼리가 생성되지 않았습니다!");
      }
    } catch (error) {
      console.error("❌ Task 추가 오류:", error);
    }
  };

  const handleUpdateStatus = async (id: string, status: TaskItem["status"]) => {
    try {
      const token = localStorage.getItem("authToken");
      const userLang = localStorage.getItem("userLang") || "KR";

      let updateQuery = "";

      // 1. 간편 모드: 사용자가 설정한 테이블/컬럼 사용
      if (element?.chartConfig?.enableDbSync && element.chartConfig.dbSyncMode === "simple" && element.chartConfig.tableName) {
        const table = element.chartConfig.tableName;
        const cols = element.chartConfig.columnMapping;
        updateQuery = `UPDATE ${table} SET ${cols.status} = '${status}' WHERE ${cols.id} = '${id}'`;
      }
      // 2. 고급 모드: 사용자가 입력한 쿼리 사용
      else if (element?.chartConfig?.enableDbSync && element.chartConfig.updateQuery) {
        updateQuery = element.chartConfig.updateQuery;
        updateQuery = updateQuery.replace(/\$\{id\}/g, id);
        updateQuery = updateQuery.replace(/\$\{status\}/g, status);
      }
      // 3. 쿼리 결과가 있으면 자동 생성
      else if (element?.dataSource?.query) {
        const selectMatch = element.dataSource.query.match(/FROM\s+(\w+)/i);
        const tableName = selectMatch ? selectMatch[1] : "todo_items";
        updateQuery = `UPDATE ${tableName} SET status = '${status}', updated_at = NOW() WHERE id = '${id}'`;
        console.log("✅ 자동 생성 UPDATE:", updateQuery);
      }
      // 4. 설정이 없으면 무시
      else {
        console.warn("⚠️ 데이터베이스 연동 설정이 없어서 상태 변경이 저장되지 않습니다.");
        return;
      }

      // 쿼리 실행 (모든 경우 처리)
      if (updateQuery) {
        const apiUrl = element?.dataSource?.connectionType === "external" && element.dataSource.externalConnectionId
          ? `http://localhost:9771/api/external-db/execute?userLang=${userLang}`
          : `http://localhost:9771/api/dashboards/execute-dml?userLang=${userLang}`;

        const requestBody = element?.dataSource?.connectionType === "external" && element.dataSource.externalConnectionId
          ? {
              connectionId: parseInt(element.dataSource.externalConnectionId),
              query: updateQuery,
            }
          : { query: updateQuery };

        const response = await fetch(apiUrl, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
            Authorization: `Bearer ${token}`,
          },
          body: JSON.stringify(requestBody),
        });

        if (response.ok) {
          fetchTasks();
        }
      }
    } catch (error) {
      // console.error("상태 업데이트 오류:", error);
    }
  };

  const handleDelete = async (id: string) => {
    if (!confirm("이 항목을 삭제하시겠습니까?")) return;

    try {
      const token = localStorage.getItem("authToken");
      const userLang = localStorage.getItem("userLang") || "KR";

      let deleteQuery = "";

      // 1. 간편 모드: 사용자가 설정한 테이블/컬럼 사용
      if (element?.chartConfig?.enableDbSync && element.chartConfig.dbSyncMode === "simple" && element.chartConfig.tableName) {
        const table = element.chartConfig.tableName;
        const cols = element.chartConfig.columnMapping;
        deleteQuery = `DELETE FROM ${table} WHERE ${cols.id} = '${id}'`;
      }
      // 2. 고급 모드: 사용자가 입력한 쿼리 사용
      else if (element?.chartConfig?.enableDbSync && element.chartConfig.deleteQuery) {
        deleteQuery = element.chartConfig.deleteQuery;
        deleteQuery = deleteQuery.replace(/\$\{id\}/g, id);
      }
      // 3. 쿼리 결과가 있으면 자동 생성
      else if (element?.dataSource?.query) {
        const selectMatch = element.dataSource.query.match(/FROM\s+(\w+)/i);
        const tableName = selectMatch ? selectMatch[1] : "todo_items";
        deleteQuery = `DELETE FROM ${tableName} WHERE id = '${id}'`;
        console.log("✅ 자동 생성 DELETE:", deleteQuery);
      }
      // 4. 설정이 없으면 무시
      else {
        console.warn("⚠️ 데이터베이스 연동 설정이 없어서 삭제가 저장되지 않습니다.");
        return;
      }

      // 쿼리 실행 (모든 경우 처리)
      if (deleteQuery) {
        const apiUrl = element?.dataSource?.connectionType === "external" && element.dataSource.externalConnectionId
          ? `http://localhost:9771/api/external-db/execute?userLang=${userLang}`
          : `http://localhost:9771/api/dashboards/execute-dml?userLang=${userLang}`;

        const requestBody = element?.dataSource?.connectionType === "external" && element.dataSource.externalConnectionId
          ? {
              connectionId: parseInt(element.dataSource.externalConnectionId),
              query: deleteQuery,
            }
          : { query: deleteQuery };

        const response = await fetch(apiUrl, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
            Authorization: `Bearer ${token}`,
          },
          body: JSON.stringify(requestBody),
        });

        if (response.ok) {
          setTimeout(() => fetchTasks(), 300);
        }
      }
    } catch (error) {
      setTimeout(() => fetchTasks(), 300);
    }
  };

  const getPriorityColor = (priority: TaskItem["priority"]) => {
    switch (priority) {
      case "urgent": return "bg-destructive/10 text-destructive border-destructive";
      case "high": return "bg-warning/10 text-warning border-warning";
      case "normal": return "bg-primary/10 text-primary border-primary";
      case "low": return "bg-muted text-foreground border-border";
    }
  };

  const getPriorityIcon = (priority: TaskItem["priority"]) => {
    switch (priority) {
      case "urgent": return "🔴";
      case "high": return "🟠";
      case "normal": return "🟡";
      case "low": return "🟢";
    }
  };

  const getMaintenanceIcon = (type?: string) => {
    if (!type) return "🔧";
    if (type.includes("점검")) return "🔍";
    if (type.includes("수리")) return "🔧";
    if (type.includes("타이어")) return "⚙️";
    if (type.includes("오일")) return "🛢️";
    return "🔧";
  };

  const getTimeRemaining = (dueDate: string) => {
    const now = new Date();
    const due = new Date(dueDate);
    const diff = due.getTime() - now.getTime();
    const hours = Math.floor(diff / (1000 * 60 * 60));
    const days = Math.floor(hours / 24);

    if (diff < 0) return "⏰ 기한 초과";
    if (days > 0) return `📅 ${days}일 남음`;
    if (hours > 0) return `⏱️ ${hours}시간 남음`;
    return "⚠️ 오늘 마감";
  };

  const filteredTasks = tasks
    .filter((task) => {
      // 날짜 필터
      if (selectedDate) {
        if (!task.dueDate) return false;
        const taskDate = new Date(task.dueDate);
        const match = (
          taskDate.getFullYear() === selectedDate.getFullYear() &&
          taskDate.getMonth() === selectedDate.getMonth() &&
          taskDate.getDate() === selectedDate.getDate()
        );
        if (!match) return false;
      }
      
      // 상태 필터
      if (filter === "all") return true;
      return task.status === filter;
    });

  const formatSelectedDate = () => {
    if (!selectedDate) return null;
    const year = selectedDate.getFullYear();
    const month = selectedDate.getMonth() + 1;
    const day = selectedDate.getDate();
    return `${year}년 ${month}월 ${day}일`;
  };

  if (loading) {
    return (
      <div className="flex h-full items-center justify-center">
        <div className="text-muted-foreground">로딩 중...</div>
      </div>
    );
  }

  return (
    <div className="flex h-full flex-col bg-background">
      {/* 제목 */}
      <div className="border-b border-border bg-background px-4 py-2">
        <div className="flex items-center justify-between">
          <div>
            <h3 className="text-lg font-bold text-foreground">
              {element?.customTitle || "일정관리 위젯"}
            </h3>
            {selectedDate && (
              <div className="mt-1 flex items-center gap-1 text-xs text-success">
                <CalendarIcon className="h-3 w-3" />
                <span className="font-semibold">{formatSelectedDate()} 일정</span>
              </div>
            )}
          </div>
          <button
            onClick={() => setShowAddForm(!showAddForm)}
            className="flex items-center gap-1 rounded-lg bg-primary px-3 py-1.5 text-sm text-primary-foreground transition-colors hover:bg-primary/90"
          >
            <Plus className="h-4 w-4" />
            추가
          </button>
        </div>
      </div>

      {/* 헤더 (통계, 필터) */}
      {element?.showHeader !== false && (
        <div className="border-b border-border bg-background px-4 py-3">
          {stats && (
            <div className="grid grid-cols-4 gap-2 text-xs mb-3">
              <div className="rounded bg-primary/10 px-2 py-1.5 text-center">
                <div className="font-bold text-primary">{stats.pending}</div>
                <div className="text-primary">대기</div>
              </div>
              <div className="rounded bg-warning/10 px-2 py-1.5 text-center">
                <div className="font-bold text-warning">{stats.inProgress}</div>
                <div className="text-warning">진행중</div>
              </div>
              <div className="rounded bg-destructive/10 px-2 py-1.5 text-center">
                <div className="font-bold text-destructive">{stats.urgent}</div>
                <div className="text-destructive">긴급</div>
              </div>
              <div className="rounded bg-destructive/10 px-2 py-1.5 text-center">
                <div className="font-bold text-destructive">{stats.overdue}</div>
                <div className="text-destructive">지연</div>
              </div>
            </div>
          )}

          <div className="flex gap-2">
            {(["all", "pending", "in_progress", "completed"] as const).map((f) => (
              <button
                key={f}
                onClick={() => setFilter(f)}
                className={`rounded px-3 py-1 text-xs font-medium transition-colors ${
                  filter === f ? "bg-primary text-primary-foreground" : "bg-muted text-foreground hover:bg-muted"
                }`}
              >
                {f === "all" ? "전체" : f === "pending" ? "대기" : f === "in_progress" ? "진행중" : "완료"}
              </button>
            ))}
          </div>
        </div>
      )}

      {/* 추가 폼 */}
      {showAddForm && (
        <div className="max-h-[400px] overflow-y-auto border-b border-border bg-background p-4">
          <div className="space-y-2">
            <input
              type="text"
              placeholder="제목*"
              value={newTask.title}
              onChange={(e) => setNewTask({ ...newTask, title: e.target.value })}
              onKeyDown={(e) => e.stopPropagation()}
              className="w-full rounded border border-border px-3 py-2 text-sm focus:border-primary focus:outline-none"
            />
            <textarea
              placeholder="상세 설명 (선택)"
              value={newTask.description}
              onChange={(e) => setNewTask({ ...newTask, description: e.target.value })}
              onKeyDown={(e) => e.stopPropagation()}
              className="w-full rounded border border-border px-3 py-2 text-sm focus:border-primary focus:outline-none"
              rows={2}
            />
            <div className="grid grid-cols-2 gap-2">
              <select
                value={newTask.priority}
                onChange={(e) => setNewTask({ ...newTask, priority: e.target.value as TaskItem["priority"] })}
                className="rounded border border-border px-3 py-2 text-sm focus:border-primary focus:outline-none"
              >
                <option value="low">🟢 낮음</option>
                <option value="normal">🟡 보통</option>
                <option value="high">🟠 높음</option>
                <option value="urgent">🔴 긴급</option>
              </select>
              <input
                type="datetime-local"
                value={newTask.dueDate}
                onChange={(e) => setNewTask({ ...newTask, dueDate: e.target.value })}
                className="rounded border border-border px-3 py-2 text-sm focus:border-primary focus:outline-none"
              />
            </div>
            <div className="flex items-center gap-2">
              <label className="flex items-center gap-2 text-sm">
                <input
                  type="checkbox"
                  checked={newTask.isUrgent}
                  onChange={(e) => setNewTask({ ...newTask, isUrgent: e.target.checked })}
                  className="h-4 w-4 rounded border-border"
                />
                <span className="text-destructive font-medium">긴급</span>
              </label>
            </div>
            <div className="flex gap-2">
              <button
                onClick={handleAddTask}
                className="flex-1 rounded bg-primary px-4 py-2 text-sm text-primary-foreground hover:bg-primary/90"
              >
                추가
              </button>
              <button
                onClick={() => setShowAddForm(false)}
                className="rounded bg-muted px-4 py-2 text-sm text-foreground hover:bg-muted/90"
              >
                취소
              </button>
            </div>
          </div>
        </div>
      )}

      {/* Task 리스트 */}
      <div className="flex-1 overflow-y-auto p-4 min-h-0">
        {filteredTasks.length === 0 ? (
          <div className="flex h-full items-center justify-center text-muted-foreground">
            <div className="text-center">
              <div className="mb-2 text-4xl">📝</div>
              <div>{selectedDate ? `${formatSelectedDate()} 일정이 없습니다` : `일정이 없습니다`}</div>
            </div>
          </div>
        ) : (
          <div className="space-y-2">
            {filteredTasks.map((task) => (
              <div
                key={task.id}
                className={`group relative rounded-lg border-2 bg-background p-3 shadow-sm transition-all hover:shadow-md ${
                  task.isUrgent || task.status === "overdue" ? "border-destructive" : "border-border"
                } ${task.status === "completed" ? "opacity-60" : ""}`}
              >
                <div className="flex items-start gap-3">
                  {/* 아이콘 */}
                  <div className="mt-1 text-lg">
                    {task.maintenanceType ? getMaintenanceIcon(task.maintenanceType) : getPriorityIcon(task.priority)}
                  </div>

                  {/* 내용 */}
                  <div className="flex-1">
                    <div className="flex items-start justify-between gap-2">
                      <div className="flex-1">
                        <div className={`font-medium ${task.status === "completed" ? "line-through" : ""}`}>
                          {task.isUrgent && <span className="mr-1 text-destructive">⚡</span>}
                          {task.vehicleNumber ? (
                            <>
                              <span className="font-bold">{task.vehicleNumber}</span>
                              {task.vehicleType && <span className="ml-2 text-xs text-foreground">({task.vehicleType})</span>}
                            </>
                          ) : (
                            task.title
                          )}
                        </div>
                        {task.maintenanceType && (
                          <div className="mt-1 rounded bg-muted px-2 py-1 text-xs font-medium text-foreground">
                            {task.maintenanceType}
                          </div>
                        )}
                        {task.description && (
                          <div className="mt-1 text-xs text-foreground">{task.description}</div>
                        )}
                        {task.dueDate && (
                          <div className="mt-1 text-xs text-muted-foreground">{getTimeRemaining(task.dueDate)}</div>
                        )}
                        {task.estimatedCost && (
                          <div className="mt-1 text-xs font-bold text-primary">
                            예상 비용: {task.estimatedCost.toLocaleString()}원
                          </div>
                        )}
                      </div>

                      {/* 액션 버튼 */}
                      <div className="flex gap-1">
                        {task.status !== "completed" && (
                          <button
                            onClick={() => handleUpdateStatus(task.id, "completed")}
                            className="rounded p-1 text-success hover:bg-success/10"
                            title="완료"
                          >
                            <Check className="h-4 w-4" />
                          </button>
                        )}
                        <button
                          onClick={() => handleDelete(task.id)}
                          className="rounded p-1 text-destructive hover:bg-destructive/10"
                          title="삭제"
                        >
                          <X className="h-4 w-4" />
                        </button>
                      </div>
                    </div>

                    {/* 상태 변경 */}
                    {task.status !== "completed" && (
                      <div className="mt-2 flex gap-1">
                        <button
                          onClick={() => handleUpdateStatus(task.id, "pending")}
                          className={`rounded px-2 py-1 text-xs ${
                            task.status === "pending"
                              ? "bg-primary/10 text-primary"
                              : "bg-muted text-foreground hover:bg-muted"
                          }`}
                        >
                          대기
                        </button>
                        <button
                          onClick={() => handleUpdateStatus(task.id, "in_progress")}
                          className={`rounded px-2 py-1 text-xs ${
                            task.status === "in_progress"
                              ? "bg-warning/10 text-warning"
                              : "bg-muted text-foreground hover:bg-muted"
                          }`}
                        >
                          진행중
                        </button>
                      </div>
                    )}
                  </div>
                </div>
              </div>
            ))}
          </div>
        )}
      </div>
    </div>
  );
}

