import { Box, Typography } from '@mui/material';
import type { GroupWithTasks, TaskWithSteps, StepWithNotes } from '../types';
import { CreateButtons } from '../components/CreateButtons';

interface DesktopLayoutProps {
  groups: GroupWithTasks[];
  selectedGroup?: GroupWithTasks;
  selectedTask?: TaskWithSteps;
  selectedStep?: StepWithNotes;
  onGroupSelect: (group: GroupWithTasks | undefined) => void;
  onTaskSelect: (task: TaskWithSteps | undefined) => void;
  onStepSelect: (step: StepWithNotes | undefined) => void;
}

export function DesktopLayout({
  groups,
  selectedGroup,
  selectedTask,
  selectedStep,
  onGroupSelect,
  onTaskSelect,
  onStepSelect,
}: DesktopLayoutProps) {
  return (
    <Box sx={{ display: 'flex', height: '100vh' }}>
      {/* Groups panel */}
      <Box sx={{ width: 250, borderRight: 1, borderColor: 'divider', p: 2 }}>
        <Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', mb: 2 }}>
          <Typography variant="h6">Groups</Typography>
          <CreateButtons type="group" />
        </Box>

        {/* Groups List */}
        {groups.map((group) => (
          <Box
            key={group.id}
            sx={{
              p: 2,
              cursor: 'pointer',
              bgcolor: selectedGroup?.id === group.id ? 'action.selected' : 'transparent',
              '&:hover': { bgcolor: 'action.hover' },
            }}
            onClick={() => onGroupSelect(group)}
          >
            <Typography>{group.name}</Typography>
          </Box>
        ))}
      </Box>

      {/* Tasks panel */}
      {selectedGroup && (
        <Box sx={{ width: 250, borderRight: 1, borderColor: 'divider', p: 2 }}>
          <Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', mb: 2 }}>
            <Typography variant="h6">Tasks</Typography>
            {selectedGroup.id > 0 && <CreateButtons type="task" parentId={String(selectedGroup.id)} />}
          </Box>
          {selectedGroup.tasks.map((task) => (
            <Box
              key={task.id}
              sx={{
                p: 2,
                cursor: 'pointer',
                bgcolor: selectedTask?.id === task.id ? 'action.selected' : 'transparent',
                '&:hover': { bgcolor: 'action.hover' },
              }}
              onClick={() => onTaskSelect(task)}
            >
              <Typography>{task.name}</Typography>
            </Box>
          ))}
        </Box>
      )}

      {/* Steps panel */}
      {selectedTask && (
        <Box sx={{ width: 250, borderRight: 1, borderColor: 'divider', p: 2 }}>
          <Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', mb: 2 }}>
            <Typography variant="h6">Steps</Typography>
            <CreateButtons type="step" parentId={String(selectedTask.id)} />
          </Box>
          {selectedTask.steps.map((step) => (
            <Box
              key={step.id}
              sx={{
                p: 2,
                cursor: 'pointer',
                bgcolor: selectedStep?.id === step.id ? 'action.selected' : 'transparent',
                '&:hover': { bgcolor: 'action.hover' },
              }}
              onClick={() => onStepSelect(step)}
            >
              <Typography>{step.name}</Typography>
            </Box>
          ))}
        </Box>
      )}

      {/* Step details panel */}
      {selectedStep && (
        <Box sx={{ flex: 1, p: 2 }}>
          <Typography variant="h6" sx={{ mb: 2 }}>
            {selectedStep.name}
          </Typography>
          <Typography>{selectedStep.instructions}</Typography>
        </Box>
      )}
    </Box>
  );
}