import axios from 'src/utils/axios';

// ----------------------------------------------------------------------

/**
 * GET /board/boards/my_boards/ — доски текущего пользователя
 */
export async function getMyBoards() {
  const res = await axios.get('/board/boards/my_boards/');
  return res.data;
}

/**
 * GET /board/boards/shared_with_me/ — доски, к которым есть доступ
 */
export async function getSharedBoards() {
  const res = await axios.get('/board/boards/shared_with_me/');
  return res.data;
}

/**
 * GET /board/boards/get-or-create-mentor-student/?mentor=X&student=Y
 */
export async function getOrCreateMentorStudentBoard(mentorId, studentId) {
  const res = await axios.get(`/board/boards/get-or-create-mentor-student/?mentor=${mentorId}&student=${studentId}`);
  return res.data;
}

/**
 * GET or create board for a lesson.
 * Resolves lesson → mentor/student IDs → getOrCreateMentorStudentBoard
 */
export async function getOrCreateLessonBoard(lessonId) {
  const lessonRes = await axios.get(`/schedule/lessons/${lessonId}/`);
  const lesson = lessonRes.data;
  const mentorId = typeof lesson.mentor === 'object' ? lesson.mentor?.id : lesson.mentor;
  const client = lesson.client;
  let studentId;
  if (client && typeof client === 'object') {
    studentId = client.user?.id ?? client.id;
  } else {
    studentId = client;
  }
  if (!mentorId || !studentId) throw new Error('Не удалось определить ментора и студента из занятия');
  return getOrCreateMentorStudentBoard(mentorId, studentId);
}

/**
 * POST /board/boards/ — создать новую доску
 */
export async function createBoard(data) {
  const res = await axios.post('/board/boards/', data);
  return res.data;
}
