'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState, useCallback } from 'react';
import { Plus, Check, X, Sparkles } from 'lucide-react';
import { cn } from '../../../lib/utils.js';
import { Button } from '../../ui/button.js';
import { Badge } from '../../ui/badge.js';
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from '../../ui/dialog.js';
import { Input } from '../../ui/input.js';
import { Label } from '../../ui/label.js';
import { Textarea } from '../../ui/textarea.js';
import { createIntakeItem, acceptIntakeItem, declineIntakeItem, autoTriageIntakeItem, } from '../../../app/actions/manage-intake.js';
const STATUS_VARIANT = {
    Pending: 'default',
    Accepted: 'secondary',
    Declined: 'destructive',
    Duplicate: 'outline',
};
export function IntakeTriagePanel({ projectId, items, onItemsChange, onWorkItemCreated, className, }) {
    const [showCreateDialog, setShowCreateDialog] = useState(false);
    const [showDeclineDialog, setShowDeclineDialog] = useState(null);
    const [newTitle, setNewTitle] = useState('');
    const [newDescription, setNewDescription] = useState('');
    const [declineReason, setDeclineReason] = useState('');
    const [loading, setLoading] = useState(false);
    const [triagingId, setTriagingId] = useState(null);
    const handleCreate = useCallback(async () => {
        if (!newTitle.trim())
            return;
        setLoading(true);
        const result = await createIntakeItem({
            projectId,
            title: newTitle.trim(),
            source: 'manual',
            description: newDescription.trim() || undefined,
        });
        setLoading(false);
        if (result.intakeItem) {
            onItemsChange?.([result.intakeItem, ...items]);
            setShowCreateDialog(false);
            setNewTitle('');
            setNewDescription('');
        }
    }, [newTitle, newDescription, projectId, items, onItemsChange]);
    const handleAccept = useCallback(async (itemId) => {
        setLoading(true);
        const result = await acceptIntakeItem(itemId);
        setLoading(false);
        if (result.workItem) {
            onItemsChange?.(items.filter((i) => i.id !== itemId));
            onWorkItemCreated?.(result.workItem);
        }
    }, [items, onItemsChange, onWorkItemCreated]);
    const handleDecline = useCallback(async () => {
        if (!showDeclineDialog || !declineReason.trim())
            return;
        setLoading(true);
        const result = await declineIntakeItem(showDeclineDialog, declineReason.trim());
        setLoading(false);
        if (!result.error) {
            onItemsChange?.(items.filter((i) => i.id !== showDeclineDialog));
            setShowDeclineDialog(null);
            setDeclineReason('');
        }
    }, [showDeclineDialog, declineReason, items, onItemsChange]);
    const handleAutoTriage = useCallback(async (itemId) => {
        setTriagingId(itemId);
        const result = await autoTriageIntakeItem(itemId);
        setTriagingId(null);
        if (result.suggestions) {
            const suggestions = result.suggestions;
            onItemsChange?.(items.map((i) => i.id === itemId
                ? {
                    ...i,
                    suggestedPriority: suggestions.suggestedPriority ?? i.suggestedPriority,
                    triageNotes: suggestions.triageNotes ?? i.triageNotes,
                    suggestedLabels: suggestions.suggestedLabels != null
                        ? JSON.stringify(suggestions.suggestedLabels)
                        : i.suggestedLabels,
                }
                : i));
        }
    }, [items, onItemsChange]);
    const pendingItems = items.filter((i) => i.status === 'Pending');
    return (_jsxs("div", { className: cn('space-y-4', className), children: [_jsxs("div", { className: "flex items-center justify-between", children: [_jsxs("div", { className: "flex items-center gap-2", children: [_jsx("h3", { className: "text-sm font-semibold", children: "Intake Triage" }), pendingItems.length > 0 && (_jsxs(Badge, { variant: "secondary", children: [pendingItems.length, " pending"] }))] }), _jsxs(Button, { size: "sm", variant: "outline", onClick: () => setShowCreateDialog(true), children: [_jsx(Plus, { className: "mr-1 h-4 w-4" }), "Add Item"] })] }), pendingItems.length === 0 ? (_jsx("p", { className: "text-muted-foreground py-4 text-center text-sm", children: "No pending intake items" })) : (_jsx("div", { className: "space-y-2", children: pendingItems.map((item) => (_jsxs("div", { className: "space-y-2 rounded-lg border p-3", children: [_jsxs("div", { className: "flex items-start justify-between gap-2", children: [_jsxs("div", { className: "min-w-0 flex-1", children: [_jsx("p", { className: "text-sm font-medium", children: item.title }), item.description ? (_jsx("p", { className: "text-muted-foreground mt-1 line-clamp-2 text-xs", children: item.description })) : null] }), _jsx(Badge, { variant: STATUS_VARIANT[item.status] ?? 'outline', className: "shrink-0", children: item.status })] }), item.suggestedPriority || item.triageNotes ? (_jsxs("div", { className: "bg-muted/50 space-y-1 rounded p-2 text-xs", children: [item.suggestedPriority ? (_jsxs("p", { children: [_jsx("span", { className: "font-medium", children: "Priority:" }), " ", item.suggestedPriority] })) : null, item.triageNotes ? (_jsxs("p", { children: [_jsx("span", { className: "font-medium", children: "Notes:" }), " ", item.triageNotes] })) : null, item.suggestedLabels ? (_jsx("div", { className: "flex flex-wrap gap-1", children: JSON.parse(item.suggestedLabels).map((label) => (_jsx(Badge, { variant: "outline", className: "text-xs", children: label }, label))) })) : null] })) : null, _jsxs("div", { className: "flex items-center gap-1.5", children: [_jsxs(Button, { size: "sm", variant: "default", className: "h-7 text-xs", disabled: loading, onClick: () => handleAccept(item.id), children: [_jsx(Check, { className: "mr-1 h-3 w-3" }), "Accept"] }), _jsxs(Button, { size: "sm", variant: "destructive", className: "h-7 text-xs", disabled: loading, onClick: () => setShowDeclineDialog(item.id), children: [_jsx(X, { className: "mr-1 h-3 w-3" }), "Decline"] }), _jsxs(Button, { size: "sm", variant: "outline", className: "h-7 text-xs", disabled: triagingId === item.id, onClick: () => handleAutoTriage(item.id), children: [_jsx(Sparkles, { className: "mr-1 h-3 w-3" }), triagingId === item.id ? 'Triaging...' : 'AI Triage'] })] })] }, item.id))) })), _jsx(Dialog, { open: showCreateDialog, onOpenChange: setShowCreateDialog, children: _jsxs(DialogContent, { children: [_jsx(DialogHeader, { children: _jsx(DialogTitle, { children: "Add Intake Item" }) }), _jsxs("div", { className: "space-y-4 py-4", children: [_jsxs("div", { className: "space-y-2", children: [_jsx(Label, { children: "Title" }), _jsx(Input, { value: newTitle, onChange: (e) => setNewTitle(e.target.value), placeholder: "Brief description of the request" })] }), _jsxs("div", { className: "space-y-2", children: [_jsx(Label, { children: "Description (optional)" }), _jsx(Textarea, { value: newDescription, onChange: (e) => setNewDescription(e.target.value), placeholder: "Additional details...", rows: 3 })] })] }), _jsxs(DialogFooter, { children: [_jsx(Button, { variant: "outline", onClick: () => setShowCreateDialog(false), children: "Cancel" }), _jsx(Button, { onClick: handleCreate, disabled: loading || !newTitle.trim(), children: loading ? 'Creating...' : 'Create' })] })] }) }), _jsx(Dialog, { open: !!showDeclineDialog, onOpenChange: () => setShowDeclineDialog(null), children: _jsxs(DialogContent, { children: [_jsx(DialogHeader, { children: _jsx(DialogTitle, { children: "Decline Intake Item" }) }), _jsx("div", { className: "space-y-4 py-4", children: _jsxs("div", { className: "space-y-2", children: [_jsx(Label, { children: "Reason" }), _jsx(Textarea, { value: declineReason, onChange: (e) => setDeclineReason(e.target.value), placeholder: "Why is this item being declined?", rows: 3 })] }) }), _jsxs(DialogFooter, { children: [_jsx(Button, { variant: "outline", onClick: () => setShowDeclineDialog(null), children: "Cancel" }), _jsx(Button, { variant: "destructive", onClick: handleDecline, disabled: loading || !declineReason.trim(), children: loading ? 'Declining...' : 'Decline' })] })] }) })] }));
}
