import supertest from 'supertest';
import {
  server,
  createTestUser,
  logInUser,
  id,
  createRole,
  createRider,
} from '../helpers';

describe('GET /auth/users/{{staff||rider}}', () => {
  it('should return 403 if user is not logged in', async () => {
    const res = await supertest(server).get('/api/v1/auth/users/staff').send();

    expect(res.status).toBe(403);
  });

  it('should return 400 if user does not pass the necessary details', async () => {
    const res = await supertest(server)
      .get('/api/v1/auth/users/invalid-userType')
      .set('Authorization', await logInUser())
      .send();

    expect(res.status).toBe(400);
  });

  it('should return 403 if user does not have permission', async () => {
    const roleId = id();
    const userID = id();
    const companyId = id();
    await createTestUser({ _id: userID, company_id: companyId });
    const res = await supertest(server)
      .get(`/api/v1/auth/users/staff`)
      .set(
        'Authorization',
        await logInUser({ role_id: roleId, company_id: companyId }),
      )
      .send();
    expect(res.status).toBe(403);
  });

  it('should return 404 if user does not exist', async () => {
    const roleId = id();
    const userID = id();
    const companyId = id();

    await createRole({
      _id: roleId,
      company_id: companyId,
      permissions: ['view_users'],
    });

    const res = await supertest(server)
      .get(`/api/v1/auth/users/staff`)
      .set(
        'Authorization',
        await logInUser({ role_id: roleId, company_id: companyId }),
      )
      .query({ userID })
      .send();

    expect(res.status).toBe(404);
    expect(res.body).toMatchObject({
      success: false,
      message: 'User not Found',
    });
  });

  it('should return 200 if has permissions', async () => {
    const roleId = id();
    const userID = id();
    const companyId = id();
    await createRole({
      _id: roleId,
      company_id: companyId,
      permissions: ['view_users'],
    });

    await createTestUser({
      _id: userID,
      company_id: companyId,
    });

    const res = await supertest(server)
      .get(`/api/v1/auth/users/staff`)
      .set(
        'Authorization',
        await logInUser({ role_id: roleId, company_id: companyId }),
      )
      .query({ userID })
      .send();

    expect(res.status).toBe(200);
    expect(res.body).toMatchObject({
      success: true,
      message: 'Get staff successful',
      data: expect.any(Object),
    });
  });

  it('should return 403 if user does not have permission', async () => {
    const roleId = id();
    const userID = id();
    const companyId = id();
    await createRider({ _id: userID, company_id: companyId });
    const res = await supertest(server)
      .get(`/api/v1/auth/users/rider`)
      .set(
        'Authorization',
        await logInUser({ role_id: roleId, company_id: companyId }),
      )
      .send({
        firstName: 'firstName',
        lastName: 'lastName',
        email: 'abc@email.com',
        phoneNumber: '+2348012345690',
      });

    expect(res.status).toBe(403);
  });

  it('should return 200 and correct response', async () => {
    const roleId = id();
    const userID = id();
    const companyId = id();
    await createRider({
      _id: userID,
      company_id: companyId,
      email: 'abc@email',
      phoneNumber: '+2348012345690',
    });
    await createRole({
      _id: roleId,
      company_id: companyId,
      permissions: ['view_dispatchers'],
    });

    await createTestUser({
      company_id: companyId,
    });

    const res = await supertest(server)
      .get(`/api/v1/auth/users/rider`)
      .set(
        'Authorization',
        await logInUser({ role_id: roleId, company_id: companyId }),
      )
      .send({
        email: 'abcd@email.com',
      });

    expect(res.status).toBe(200);
    expect(res.body).toMatchObject({
      success: true,
      message: 'Get all staff successful',
      data: expect.any(Object),
    });
  });
});
