import { BadRequestException } from '@nestjs/common';
import { ClassroomStatus } from '../entities/classroom.entity';
import { ClassroomsService } from './classrooms.service';

function createService(options?: { rentals?: number; schedules?: number }) {
  const repo = {
    findOne: jest.fn().mockResolvedValue({ id: 1, name: 'A101', status: ClassroomStatus.ARCHIVED }),
    update: jest.fn(),
  };
  const rentalRepo = { count: jest.fn().mockResolvedValue(options?.rentals ?? 0) };
  const scheduleRepo = {
    createQueryBuilder: jest.fn().mockReturnValue({
      where: jest.fn().mockReturnThis(),
      andWhere: jest.fn().mockReturnThis(),
      getCount: jest.fn().mockResolvedValue(options?.schedules ?? 0),
    }),
  };
  return {
    service: new ClassroomsService(repo as never, rentalRepo as never, scheduleRepo as never),
    repo,
  };
}

describe('ClassroomsService — persisted classroom status', () => {
  it('restores an archived classroom to available', async () => {
    const { service, repo } = createService();

    await service.restore(1);

    expect(repo.update).toHaveBeenCalledWith(1, { status: ClassroomStatus.AVAILABLE });
  });

  it('rejects archiving a classroom with active allocations', async () => {
    const { service, repo } = createService({ rentals: 1 });

    await expect(service.remove(1)).rejects.toBeInstanceOf(BadRequestException);
    expect(repo.update).not.toHaveBeenCalled();
  });
});
