import React, { useState, useEffect } from 'react' import { X, Sparkles, BarChart3, Clock, CheckCircle2, PieChart, Target, Flame, ListOrdered, Layers, Tag, } from 'lucide-react' import type { DashboardV2Card, BiMetricType, BiPeriodFilter, BiCardScope } from '../../lib/userPreferences' import type { Project, Category } from '../../types/task' import { CustomSelect } from '../ui/Select' interface CreateBiCardModalProps { isOpen: boolean onClose: () => void onSave: (card: DashboardV2Card) => void editingCard?: DashboardV2Card | null projects: Project[] categories: Category[] } const METRIC_OPTIONS: { type: BiMetricType label: string desc: string icon: React.FC<{ className?: string }> }[] = [ { type: 'HOURS_TRACKED', label: 'Horas Dedicadas', desc: 'Total de tempo cronometrado nas tarefas com os filtros selecionados.', icon: Clock, }, { type: 'TASK_COUNT', label: 'Quantidade de Tarefas', desc: 'Total numérico de tarefas criadas ou filtradas.', icon: Layers, }, { type: 'COMPLETION_RATE', label: 'Taxa de Conclusão (%)', desc: 'Percentual de demandas entregues em relação ao total.', icon: CheckCircle2, }, { type: 'PROJECT_BREAKDOWN', label: 'Distribuição por Projeto', desc: 'Comparativo visual de horas e tarefas por iniciativa.', icon: BarChart3, }, { type: 'CATEGORY_BREAKDOWN', label: 'Distribuição por Categoria', desc: 'Divisão de horas dedicadas por categoria de tarefa.', icon: Tag, }, { type: 'STATUS_DISTRIBUTION', label: 'Distribuição por Status', desc: 'Divisão entre A Fazer, Em Andamento e Concluído.', icon: PieChart, }, { type: 'WORK_GOAL', label: 'Meta de Trabalho & Jornada', desc: 'Progresso frente à meta diária, semanal ou mensal de horas.', icon: Target, }, { type: 'ACTIVE_TIMER', label: 'Cronômetro & Foco Atual', desc: 'Monitor do tempo ativo com botão de controle direto no card.', icon: Flame, }, { type: 'TOP_TASKS', label: 'Demandas Mais Dedicadas', desc: 'Top 3 tarefas com maior volume de horas acumuladas.', icon: ListOrdered, }, ] const COLOR_OPTIONS: { id: DashboardV2Card['colorTheme']; label: string; bg: string }[] = [ { id: 'brand', label: 'Teal (Brand)', bg: 'bg-teal-500' }, { id: 'emerald', label: 'Esmeralda', bg: 'bg-emerald-500' }, { id: 'accent', label: 'Âmbar', bg: 'bg-amber-500' }, { id: 'indigo', label: 'Índigo', bg: 'bg-indigo-500' }, { id: 'cyan', label: 'Ciano', bg: 'bg-cyan-500' }, { id: 'rose', label: 'Rose', bg: 'bg-rose-500' }, ] export const CreateBiCardModal: React.FC = ({ isOpen, onClose, onSave, editingCard, projects, categories, }) => { const [title, setTitle] = useState('') const [metricType, setMetricType] = useState('HOURS_TRACKED') const [period, setPeriod] = useState('WEEK') const [projectId, setProjectId] = useState(undefined) const [categoryId, setCategoryId] = useState(undefined) const [statusFilter, setStatusFilter] = useState<'ALL' | 'COMPLETED' | 'IN_PROGRESS' | 'NOT_STARTED'>('ALL') const [colorTheme, setColorTheme] = useState('brand') const [scope, setScope] = useState('INHERIT') useEffect(() => { if (editingCard) { setTitle(editingCard.title) setMetricType(editingCard.metricType) setPeriod(editingCard.period) setProjectId(editingCard.projectId) setCategoryId(editingCard.categoryId) setStatusFilter(editingCard.statusFilter || 'ALL') setColorTheme(editingCard.colorTheme || 'brand') setScope(editingCard.scope || 'INHERIT') } else { setTitle('') setMetricType('HOURS_TRACKED') setPeriod('WEEK') setProjectId(undefined) setCategoryId(undefined) setStatusFilter('ALL') setColorTheme('brand') setScope('INHERIT') } }, [editingCard, isOpen]) if (!isOpen) return null const handleSubmit = (e: React.FormEvent) => { e.preventDefault() const finalTitle = title.trim() || getDefaultTitleForMetric(metricType) const card: DashboardV2Card = { id: editingCard?.id || `card-${Date.now()}`, title: finalTitle, metricType, period, projectId, categoryId, statusFilter, colorTheme, scope, } onSave(card) onClose() } function getDefaultTitleForMetric(type: BiMetricType): string { const found = METRIC_OPTIONS.find((m) => m.type === type) return found ? found.label : 'Novo Indicador' } return (
{/* Header */}

{editingCard ? 'Editar Card de BI' : 'Criar Card de BI do Zero'}

Combine métricas, filtros de projeto e períodos no mesmo card.

{/* Form Body */}
{/* Card Title */}
setTitle(e.target.value)} placeholder={getDefaultTitleForMetric(metricType)} className="w-full px-4 py-2.5 rounded-xl border border-border-soft bg-surface text-xs text-foreground focus:outline-hidden focus:border-brand focus:ring-1 focus:ring-brand shadow-xs" />
{/* Metric Type Selector */}
{METRIC_OPTIONS.map((opt) => { const IconComponent = opt.icon const isSelected = metricType === opt.type return ( ) })}
{/* Combined Filters (BI Dimensions) */}

Filtros Combináveis (Dimensões de BI)

{/* Period Filter */}
setPeriod(val as BiPeriodFilter)} options={[ { value: 'INHERIT', label: '🔗 Seguir Período da Página (Recomendado)' }, { value: 'TODAY', label: '📅 Apenas Hoje' }, { value: 'YESTERDAY', label: 'Ontem' }, { value: 'WEEK', label: 'Esta Semana' }, { value: 'LAST_WEEK', label: 'Semana Passada' }, { value: 'MONTH', label: 'Este Mês' }, { value: 'LAST_MONTH', label: 'Mês Passado' }, { value: 'LAST_3_MONTHS', label: 'Últimos 3 Meses (Trimestre)' }, { value: 'LAST_6_MONTHS', label: 'Últimos 6 Meses (Semestre)' }, { value: 'YEAR', label: 'Este Ano' }, { value: 'ALL', label: 'Todo o Histórico' }, ]} />
{/* Status Filter */}
setStatusFilter(val as any)} options={[ { value: 'ALL', label: 'Todos os Status' }, { value: 'COMPLETED', label: 'Apenas Concluídas', color: '#10b981' }, { value: 'IN_PROGRESS', label: 'Apenas Em Andamento', color: '#14b8a6' }, { value: 'NOT_STARTED', label: 'Apenas A Fazer', color: '#71717a' }, ]} />
{/* Project Filter */}
setProjectId(Number(val) === 0 ? undefined : Number(val))} placeholder="Todos os Projetos" options={[ { value: 0, label: 'Todos os Projetos' }, ...projects.map((p) => ({ value: p.id, label: p.name, color: p.color, })), ]} />
{/* Category Filter */}
setCategoryId(Number(val) === 0 ? undefined : Number(val))} placeholder="Todas as Categorias" options={[ { value: 0, label: 'Todas as Categorias' }, ...categories.map((c) => ({ value: c.id, label: c.name, color: c.color, })), ]} />
{/* Data Scope Filter */}
setScope(val as BiCardScope)} options={[ { value: 'INHERIT', label: '🔗 Seguir filtro global da página (Padrão)', description: 'Acompanha a alternância da tela', }, { value: 'MINE', label: '👤 Sempre Apenas Minhas Tarefas (Individual)', description: 'Fixo nas suas demandas', }, { value: 'TEAM', label: '👥 Sempre Toda a Equipe (Workspace Geral)', description: 'Fixo em todo o time', }, ]} />
{/* Color Theme Selector */}
{COLOR_OPTIONS.map((c) => { const isSelected = colorTheme === c.id return ( ) })}
{/* Footer */}
) }