import { TranslationsService } from './translations.service';
import type { PrismaService } from '@/prisma/prisma.service';

describe('shared profile menu dictionary', () => {
  const owners = ['pswg', 'ministry', 'cdc', 'cefp', 'cdcgpsf'];
  function setup() {
    const rows = [
      { module: 'shared', page: 'profile_menu', translationKey: 'logout', englishText: 'Logout', khmerText: 'ចាកចេញ', isActive: true },
      { module: 'pswg', page: 'profile_menu', translationKey: 'logout', englishText: 'Old copy', khmerText: 'Old copy', isActive: true },
      { module: 'pswg', page: 'dashboard', translationKey: 'title', englishText: 'Dashboard', khmerText: 'ផ្ទាំងគ្រប់គ្រង', isActive: true },
    ];
    const db = { translation: { findMany: jest.fn(async ({ where }: {
      where: { isActive: boolean; OR?: { module: string; page?: string }[] };
    }) => rows.filter(row => row.isActive === where.isActive &&
      (!where.OR || where.OR.some(scope => row.module === scope.module && (!scope.page || row.page === scope.page))))) } };
    return { rows, service: new TranslationsService(db as unknown as PrismaService) };
  }

  it('one shared edit changes the page-specific and all-page dictionaries of every module', async () => {
    const { rows, service } = setup();
    rows[0].englishText = 'Shared Admin edit';
    rows[0].khmerText = 'អត្ថបទរួម';
    for (const module of owners) {
      const english = await service.getDictionary({ module, language: 'en', page: 'profile_menu' });
      expect(english.data.translations).toEqual({ logout: 'Shared Admin edit' });
      const khmer = await service.getDictionary({ module, language: 'km' });
      expect(khmer.data.translations['profile_menu.logout']).toBe('អត្ថបទរួម');
      if (module !== 'pswg') expect(khmer.data.translations['dashboard.title']).toBeUndefined();
    }
    const shared = await service.getDictionary({ module: 'shared', page: 'profile_menu' });
    expect(shared.data.translations.logout).toBe('Shared Admin edit');
  });

  it('disabled shared labels never fall back to a legacy module copy', async () => {
    const { rows, service } = setup();
    rows[0].isActive = false;
    const result = await service.getDictionary({ module: 'pswg', page: 'profile_menu' });
    expect(result.data.translations).toEqual({});
    const dashboard = await service.getDictionary({ module: 'pswg', page: 'dashboard' });
    expect(dashboard.data.translations).toEqual({ title: 'Dashboard' });
  });

  it('all profile destination pages are shared in every module dictionary', async () => {
    const { rows, service } = setup();
    for (const page of ['account_setting', 'notification_setting', 'my_organization']) {
      rows.push({ module: 'shared', page, translationKey: 'page_title', englishText: 'Admin title', khmerText: 'ចំណងជើងរួម', isActive: true });
      rows.push({ module: 'ministry', page, translationKey: 'page_title', englishText: 'Legacy title', khmerText: 'Legacy', isActive: true });
      for (const module of owners) {
        const specific = await service.getDictionary({ module, page, language: 'en' });
        expect(specific.data.translations).toEqual({ page_title: 'Admin title' });
        const all = await service.getDictionary({ module, language: 'km' });
        expect(all.data.translations[`${page}.page_title`]).toBe('ចំណងជើងរួម');
      }
    }
  });
});
