import { describe, expect, it } from 'vitest';
import {
  canZoneCheckIn,
  shouldNudgeZoneCheckIn,
} from '@/business/zone-checkin/selectors';
import type { CurrentUser } from '@/shared/types/auth';

function user(
  permissions: readonly string[],
  globalAccess = false,
): CurrentUser {
  return {
    id: 'user-1',
    email: 'user@example.com',
    app_role: { name: globalAccess ? 'super_admin' : 'teacher', globalAccess },
    permissions,
  };
}

describe('zone check-in selectors', () => {
  it('checks the explicit ZONE_CHECKIN effective permission', () => {
    expect(canZoneCheckIn(user(['ZONE_CHECKIN']))).toBe(true);
    expect(canZoneCheckIn(user(['READ_ZONES']))).toBe(false);
  });

  it('does not infer zone check-in from globalAccess alone', () => {
    expect(canZoneCheckIn(user([], true))).toBe(false);
    expect(canZoneCheckIn(user(['ZONE_CHECKIN'], true))).toBe(true);
  });

  it('nudges only a permitted user that has loaded and not checked in', () => {
    expect(shouldNudgeZoneCheckIn(user(['ZONE_CHECKIN']), false, false)).toBe(true);
    // already checked in
    expect(shouldNudgeZoneCheckIn(user(['ZONE_CHECKIN']), false, true)).toBe(false);
    // still loading
    expect(shouldNudgeZoneCheckIn(user(['ZONE_CHECKIN']), true, false)).toBe(false);
    // missing permission
    expect(shouldNudgeZoneCheckIn(user(['READ_ZONES']), false, false)).toBe(false);
  });
});
