import { describe, expect, it } from 'vitest';
import {
  canPersistPersonalScopeResults,
  getActiveTenant,
  getScopeTier,
  getTenantInitials,
} from '@/business/scope/selectors';
import type { CurrentUser } from '@/shared/types/auth';

function user(partial: Partial<CurrentUser>): CurrentUser {
  return { id: 'u1', email: 'u@example.com', ...partial };
}

describe('scope selectors', () => {
  describe('getScopeTier', () => {
    it('returns external for no user', () => {
      expect(getScopeTier(null)).toBe('external');
    });

    it('returns global for globalAccess or the system scope', () => {
      expect(getScopeTier(user({ app_role: { globalAccess: true } }))).toBe('global');
      expect(getScopeTier(user({ app_role: { scope: 'system' } }))).toBe('global');
    });

    it('maps the role scope to the tier', () => {
      expect(getScopeTier(user({ app_role: { scope: 'organization' } }))).toBe('organization');
      expect(getScopeTier(user({ app_role: { scope: 'school' } }))).toBe('school');
      expect(getScopeTier(user({ app_role: { scope: 'campus' } }))).toBe('campus');
      expect(getScopeTier(user({ app_role: { scope: 'class' } }))).toBe('class');
      expect(getScopeTier(user({ app_role: { scope: 'guest' } }))).toBe('external');
    });
  });

  describe('getActiveTenant', () => {
    it('is null for a global admin with no organization', () => {
      expect(getActiveTenant(user({ app_role: { globalAccess: true } }))).toBeNull();
    });

    it('returns the tenant at the user scope tier', () => {
      expect(
        getActiveTenant(
          user({ app_role: { scope: 'campus' }, campus: { id: 'c1', name: 'Tigers' } }),
        ),
      ).toEqual({ level: 'campus', id: 'c1', name: 'Tigers', logo: null });

      expect(
        getActiveTenant(
          user({
            app_role: { scope: 'school' },
            school: { id: 's1', name: 'North', logo: 'logo.png' },
          }),
        ),
      ).toEqual({ level: 'school', id: 's1', name: 'North', logo: 'logo.png' });

      expect(
        getActiveTenant(
          user({ app_role: { scope: 'organization' }, organizations: { id: 'o1', name: 'Org' } }),
        ),
      ).toEqual({ level: 'organization', id: 'o1', name: 'Org', logo: null });
    });
  });

  describe('getTenantInitials', () => {
    it('builds initials from the first and last word', () => {
      expect(getTenantInitials('Demo Academy North')).toBe('DN');
    });

    it('takes the first two letters of a single word', () => {
      expect(getTenantInitials('Tigers')).toBe('TI');
    });

    it('falls back for an empty name', () => {
      expect(getTenantInitials(null)).toBe('–');
    });
  });

  describe('canPersistPersonalScopeResults', () => {
    it('allows persistence when the user is in their own scope', () => {
      expect(canPersistPersonalScopeResults({ level: 'school', id: 's1', name: 'North', logo: null }, null)).toBe(true);
    });

    it('allows persistence when selected tenant matches the own tenant', () => {
      const tenant = { level: 'campus' as const, id: 'c1', name: 'Tigers', logo: null };

      expect(canPersistPersonalScopeResults(tenant, tenant)).toBe(true);
    });

    it('blocks persistence for a parent user drilled into a child tenant', () => {
      expect(
        canPersistPersonalScopeResults(
          { level: 'organization', id: 'o1', name: 'Demo', logo: null },
          { level: 'school', id: 's1', name: 'North', logo: null },
        ),
      ).toBe(false);
    });

    it('blocks persistence for a global user drilled into a tenant', () => {
      expect(
        canPersistPersonalScopeResults(
          null,
          { level: 'organization', id: 'o1', name: 'Demo', logo: null },
        ),
      ).toBe(false);
    });
  });
});
