import React from 'react';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen, waitFor } from '@testing-library/react';

vi.mock('@/api/calendar', () => ({
  fetchKanbanBoard: vi.fn().mockResolvedValue({
    open: [
      { id: 'e1', calendar_id: 'cal1', entry_type: 'task', subtype: 'normal', title: 'Recherche', all_day: false, priority: 'medium', status: 'open', created_by: 'u1', due_date: '2024-07-01T00:00:00Z' },
    ],
    in_progress: [
      { id: 'e2', calendar_id: 'cal1', entry_type: 'task', subtype: 'normal', title: 'Implementation', all_day: false, priority: 'high', status: 'in_progress', created_by: 'u1', due_date: '2024-07-02T00:00:00Z' },
    ],
    done: [
      { id: 'e3', calendar_id: 'cal1', entry_type: 'task', subtype: 'normal', title: 'Tests', all_day: false, priority: 'low', status: 'done', created_by: 'u1', due_date: '2024-07-03T00:00:00Z' },
    ],
    cancelled: [],
  }),
  updateEntry: vi.fn().mockResolvedValue({ id: 'e1', status: 'in_progress' }),
}));

vi.mock('@/components/calendar/KanbanBoard', () => ({
  KanbanBoard: ({ board, loading }: any) => (
    <div data-testid="kanban-board-stub">
      {loading ? 'Loading...' : `Open: ${board.open.length}, In Progress: ${board.in_progress.length}, Done: ${board.done.length}`}
    </div>
  ),
}));

vi.mock('@/components/calendar/TaskDetailPanel', () => ({
  TaskDetailPanel: () => <div data-testid="task-detail-panel-stub">TaskDetailPanel</div>,
}));

import { CalendarKanbanPage } from '@/pages/CalendarKanban';

describe('CalendarKanbanPage', () => {
  it('renders kanban page', async () => {
    render(<CalendarKanbanPage />);
    expect(screen.getByTestId('kanban-page')).toBeInTheDocument();
  });

  it('renders kanban board component', async () => {
    render(<CalendarKanbanPage />);
    await waitFor(() => {
      expect(screen.getByTestId('kanban-board-stub')).toBeInTheDocument();
    });
  });

  it('renders board with tasks after loading', async () => {
    render(<CalendarKanbanPage />);
    await waitFor(() => {
      expect(screen.getByText(/Open: 1, In Progress: 1, Done: 1/)).toBeInTheDocument();
    });
  });

  it('renders page title', async () => {
    render(<CalendarKanbanPage />);
    expect(screen.getByTestId('kanban-page')).toBeInTheDocument();
  });

  it('does not render error when load succeeds', async () => {
    render(<CalendarKanbanPage />);
    await waitFor(() => {
      expect(screen.queryByTestId('kanban-error')).not.toBeInTheDocument();
    });
  });
});
