import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
  createCampus,
  createClass,
  createSchoolWithFirstCampus,
  deleteCampus,
  deleteClass,
  deleteOrganization,
  deleteSchool,
  updateCampus,
  updateClass,
  updateOrganization,
  updateSchool,
} from '@/shared/api/tenants';
import { apiRequest } from '@/shared/api/httpClient';

vi.mock('@/shared/api/httpClient', () => ({
  apiRequest: vi.fn(),
}));

describe('location management API', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('creates school with first campus', async () => {
    vi.mocked(apiRequest).mockResolvedValueOnce({});

    await createSchoolWithFirstCampus(
      { name: 'School', code: 'school' },
      { name: 'Campus', timezone: 'America/New_York' },
    );

    expect(apiRequest).toHaveBeenCalledWith('/schools/with-first-campus', {
      method: 'POST',
      body: {
        data: { name: 'School', code: 'school' },
        firstCampus: { name: 'Campus', timezone: 'America/New_York' },
      },
    });
  });

  it('creates and updates child locations', async () => {
    vi.mocked(apiRequest).mockResolvedValue({});

    await createCampus({ name: 'Campus', code: 'campus', timezone: 'America/New_York' });
    await createClass({ name: 'Class' });
    await updateOrganization('org-1', { name: 'Org' });
    await updateSchool('school-1', { name: 'School' });
    await updateCampus('campus-1', { name: 'Campus' });
    await updateClass('class-1', { name: 'Class' });

    expect(apiRequest).toHaveBeenNthCalledWith(1, '/campuses', {
      method: 'POST',
      body: { data: { name: 'Campus', code: 'campus', timezone: 'America/New_York' } },
    });
    expect(apiRequest).toHaveBeenNthCalledWith(2, '/classes', {
      method: 'POST',
      body: { data: { name: 'Class' } },
    });
    expect(apiRequest).toHaveBeenNthCalledWith(3, '/organizations/org-1', {
      method: 'PUT',
      body: { id: 'org-1', data: { name: 'Org' } },
    });
    expect(apiRequest).toHaveBeenNthCalledWith(4, '/schools/school-1', {
      method: 'PUT',
      body: { id: 'school-1', data: { name: 'School' } },
    });
    expect(apiRequest).toHaveBeenNthCalledWith(5, '/campuses/campus-1', {
      method: 'PUT',
      body: { id: 'campus-1', data: { name: 'Campus' } },
    });
    expect(apiRequest).toHaveBeenNthCalledWith(6, '/classes/class-1', {
      method: 'PUT',
      body: { id: 'class-1', data: { name: 'Class' } },
    });
  });

  it('deletes locations', async () => {
    vi.mocked(apiRequest).mockResolvedValue({});

    await deleteOrganization('org-1');
    await deleteSchool('school-1');
    await deleteCampus('campus-1');
    await deleteClass('class-1');

    expect(apiRequest).toHaveBeenNthCalledWith(1, '/organizations/org-1', { method: 'DELETE' });
    expect(apiRequest).toHaveBeenNthCalledWith(2, '/schools/school-1', { method: 'DELETE' });
    expect(apiRequest).toHaveBeenNthCalledWith(3, '/campuses/campus-1', { method: 'DELETE' });
    expect(apiRequest).toHaveBeenNthCalledWith(4, '/classes/class-1', { method: 'DELETE' });
  });
});
