import React, { useState, useEffect } from 'react';

export default function ProjectDashboard() {
  const [projects, setProjects] = useState<any[]>([]);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    loadProjects();
  }, []);

  const loadProjects = async () => {
    setLoading(true);
    try {
      const result = await window.mcp.callTool('wrike_list_projects', {});
      setProjects(result.projects || []);
    } catch (error) {
      console.error('Failed to load projects:', error);
    } finally {
      setLoading(false);
    }
  };

  const getStatusColor = (status: string) => {
    const colors: Record<string, string> = {
      Green: '#10b981',
      Yellow: '#f59e0b',
      Red: '#ef4444',
      Completed: '#3b82f6',
      OnHold: '#6b7280',
      Cancelled: '#9ca3af',
    };
    return colors[status] || '#6b7280';
  };

  return (
    <div style={{ background: '#111827', color: '#f3f4f6', minHeight: '100vh', padding: '24px' }}>
      <h1 style={{ fontSize: '24px', fontWeight: 'bold', marginBottom: '24px' }}>Project Dashboard</h1>
      
      <button
        onClick={loadProjects}
        style={{
          background: '#3b82f6',
          color: 'white',
          padding: '8px 16px',
          borderRadius: '6px',
          border: 'none',
          cursor: 'pointer',
          marginBottom: '24px',
        }}
      >
        Refresh
      </button>

      {loading ? (
        <div>Loading...</div>
      ) : (
        <div style={{ display: 'grid', gap: '16px' }}>
          {projects.map(project => (
            <div
              key={project.id}
              style={{
                background: '#1f2937',
                padding: '16px',
                borderRadius: '8px',
                border: '1px solid #374151',
              }}
            >
              <h3 style={{ fontSize: '18px', fontWeight: '600', marginBottom: '8px' }}>
                {project.title}
              </h3>
              {project.project && (
                <div style={{ display: 'flex', gap: '12px', fontSize: '14px' }}>
                  <span
                    style={{
                      padding: '4px 8px',
                      borderRadius: '4px',
                      background: getStatusColor(project.project.status) + '20',
                      color: getStatusColor(project.project.status),
                    }}
                  >
                    {project.project.status}
                  </span>
                  {project.project.startDate && (
                    <span style={{ color: '#9ca3af' }}>
                      Start: {new Date(project.project.startDate).toLocaleDateString()}
                    </span>
                  )}
                  {project.project.endDate && (
                    <span style={{ color: '#9ca3af' }}>
                      End: {new Date(project.project.endDate).toLocaleDateString()}
                    </span>
                  )}
                </div>
              )}
            </div>
          ))}
          {projects.length === 0 && (
            <div style={{ textAlign: 'center', padding: '48px', color: '#6b7280' }}>
              No projects found
            </div>
          )}
        </div>
      )}
    </div>
  );
}
