import {getMinimalMe} from '../../../support/helpers';

const SELECTED_CLASS_ID = 'selectedClassId';

const getOperations = ({readOnly, classReadOnly}) => {
  const {me} = getMinimalMe({readOnly, classReadOnly, isTeacher: true});
  return {
    MeQuery: {
      me: {
        ...me,
        readOnly,
        selectedClass: {
          id: SELECTED_CLASS_ID,
          readOnly: classReadOnly,
        },
      },
    },
    RoomEntriesQuery: {
      room: {
        id: 'roomId',
        slug: '',
        title: 'room title',
        entryCount: 3,
        appearance: 'blue',
        description: 'room description',
        schoolClass: {
          id: SELECTED_CLASS_ID,
          name: 'selected class',
        },
        roomEntries: {
          edges: [
            {
              node: {
                id: 'entryId',
                slug: '',
                title: 'entry title',
                contents: [],
                author: {
                  id: btoa('PublicUserNode:authorId'),
                  firstName: 'first',
                  lastName: 'last',
                  avatarUrl: '',
                },
              },
            },
          ],
        },
      },
    },
  };
};

const checkRoomReadOnly = ({editable, readOnly, classReadOnly = false}) => {
  const operations = getOperations({readOnly, classReadOnly});

  cy.mockGraphqlOps({
    operations,
  });

  const exist = editable ? 'exist' : 'not.exist';
  cy.visit('room/some-room');
  cy.get('.room-entry').should('exist');
  cy.getByDataCy('add-room-entry-button').should(exist);
  cy.getByDataCy('room-actions').should(exist);
};

describe('Room Team Management - Read only', () => {
  beforeEach(() => {
    cy.setup();
  });

  it('can edit room', () => {
    checkRoomReadOnly({editable: true, readOnly: false});
  });

  it('can not edit room', () => {
    checkRoomReadOnly({editable: false, readOnly: true});
  });

  it('can not edit room of inactive class', () => {
    checkRoomReadOnly({editable: false, readOnly: false, classReadOnly: true});
  });
});
