import { afterEach, describe, mock, test } from 'node:test';
import assert from 'node:assert/strict';

import db from '@/db/models';
import ContentCatalogService from '@/services/content_catalog';
import { createGlobalAccessUser } from '@/test-utils';

function catalogRecord(payload: unknown) {
  return {
    get: () => ({
      id: 'catalog-1',
      content_type: 'dashboard-encouraging-quotes',
      payload,
      updatedAt: new Date('2026-06-16T00:00:00Z'),
    }),
  };
}

afterEach(() => {
  mock.restoreAll();
});

describe('ContentCatalogService tenant scoping', () => {
  test('reads dashboard content at an organization drill-down scope', async () => {
    let capturedWhere: Record<string, unknown> | null = null;
    mock.method(db.content_catalog, 'findOne', (async (options: { where?: Record<string, unknown> }) => {
      capturedWhere = options.where ?? null;
      return catalogRecord([]);
    }) as typeof db.content_catalog.findOne);

    await ContentCatalogService.findByType(
      'dashboard-encouraging-quotes',
      createGlobalAccessUser({
        activeScope: {
          level: 'organization',
          organizationId: 'org-1',
          schoolId: null,
          campusId: null,
          classId: null,
        },
      }),
    );

    assert.deepEqual(capturedWhere, {
      content_type: 'dashboard-encouraging-quotes',
      active: true,
      organizationId: 'org-1',
      schoolId: null,
      campusId: null,
      classId: null,
    });
  });

  test('reads dashboard content at a school drill-down scope', async () => {
    let capturedWhere: Record<string, unknown> | null = null;
    mock.method(db.content_catalog, 'findOne', (async (options: { where?: Record<string, unknown> }) => {
      capturedWhere = options.where ?? null;
      return catalogRecord([]);
    }) as typeof db.content_catalog.findOne);

    await ContentCatalogService.findByType(
      'dashboard-encouraging-quotes',
      createGlobalAccessUser({
        activeScope: {
          level: 'school',
          organizationId: 'org-1',
          schoolId: 'school-1',
          campusId: null,
          classId: null,
        },
      }),
    );

    assert.deepEqual(capturedWhere, {
      content_type: 'dashboard-encouraging-quotes',
      active: true,
      organizationId: 'org-1',
      schoolId: 'school-1',
      campusId: null,
      classId: null,
    });
  });

  test('reads dashboard content at a campus drill-down scope', async () => {
    let capturedWhere: Record<string, unknown> | null = null;
    mock.method(db.content_catalog, 'findOne', (async (options: { where?: Record<string, unknown> }) => {
      capturedWhere = options.where ?? null;
      return catalogRecord([]);
    }) as typeof db.content_catalog.findOne);

    await ContentCatalogService.findByType(
      'dashboard-encouraging-quotes',
      createGlobalAccessUser({
        activeScope: {
          level: 'campus',
          organizationId: 'org-1',
          schoolId: 'school-1',
          campusId: 'campus-1',
          classId: null,
        },
      }),
    );

    assert.deepEqual(capturedWhere, {
      content_type: 'dashboard-encouraging-quotes',
      active: true,
      organizationId: 'org-1',
      campusId: 'campus-1',
      classId: null,
    });
  });
});
