import { Page } from '@playwright/test';

export const mockStudents = [
  {
    id: 1,
    name: 'Juan Pérez',
    email: 'juan@test.com',
    totalCredits: 6,
    enrollments: [
      {
        id: 1,
        subject: { id: 1, name: 'Matemáticas I', credits: 3, professor: { id: 1, name: 'Dr. García' } },
        enrolledAt: '2024-01-01',
      },
      {
        id: 2,
        subject: { id: 3, name: 'Física I', credits: 3, professor: { id: 2, name: 'Dra. Martínez' } },
        enrolledAt: '2024-01-01',
      },
    ],
  },
];

export const mockAvailableSubjects = [
  {
    subject: { id: 2, name: 'Matemáticas II', credits: 3, professor: { id: 1, name: 'Dr. García' } },
    isAvailable: false,
    unavailableReason: 'Ya tienes una materia con este profesor',
  },
  {
    subject: { id: 4, name: 'Física II', credits: 3, professor: { id: 2, name: 'Dra. Martínez' } },
    isAvailable: false,
    unavailableReason: 'Ya tienes una materia con este profesor',
  },
  {
    subject: { id: 5, name: 'Programación I', credits: 3, professor: { id: 3, name: 'Dr. López' } },
    isAvailable: true,
    unavailableReason: null,
  },
  {
    subject: { id: 6, name: 'Programación II', credits: 3, professor: { id: 3, name: 'Dr. López' } },
    isAvailable: true,
    unavailableReason: null,
  },
];

export const mockClassmates = [
  { subjectName: 'Matemáticas I', students: ['María García', 'Carlos López'] },
  { subjectName: 'Física I', students: ['Ana Rodríguez', 'Pedro Sánchez'] },
];

export async function setupGraphQLMocks(page: Page) {
  await page.route('**/graphql', async (route) => {
    const request = route.request();
    const postData = request.postDataJSON();
    const operationName = postData?.operationName;

    let responseData: object;

    switch (operationName) {
      case 'GetStudents':
        responseData = { data: { students: mockStudents } };
        break;

      case 'GetStudent':
        const studentId = postData?.variables?.id;
        const student = mockStudents.find((s) => s.id === studentId) || mockStudents[0];
        responseData = { data: { student } };
        break;

      case 'CreateStudent':
        const newStudent = {
          id: Date.now(),
          name: postData?.variables?.input?.name,
          email: postData?.variables?.input?.email,
          totalCredits: 0,
          enrollments: [],
        };
        responseData = { data: { createStudent: { student: newStudent, errors: null } } };
        break;

      case 'UpdateStudent':
        responseData = {
          data: {
            updateStudent: {
              student: { ...mockStudents[0], ...postData?.variables?.input },
              errors: null,
            },
          },
        };
        break;

      case 'DeleteStudent':
        responseData = { data: { deleteStudent: { success: true, errors: null } } };
        break;

      case 'GetAvailableSubjects':
        responseData = { data: { availableSubjects: mockAvailableSubjects } };
        break;

      case 'EnrollStudent':
        const subjectId = postData?.variables?.input?.subjectId;
        const subject = mockAvailableSubjects.find((s) => s.subject.id === subjectId)?.subject;
        responseData = {
          data: {
            enrollStudent: {
              enrollment: {
                id: Date.now(),
                subject,
                enrolledAt: new Date().toISOString(),
              },
              errors: null,
            },
          },
        };
        break;

      case 'UnenrollStudent':
        responseData = { data: { unenrollStudent: { success: true, errors: null } } };
        break;

      case 'GetClassmates':
        responseData = { data: { classmates: mockClassmates } };
        break;

      default:
        responseData = { data: null };
    }

    await route.fulfill({
      status: 200,
      contentType: 'application/json',
      body: JSON.stringify(responseData),
    });
  });
}
