import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import request from 'supertest';

import { SettingsModule } from '../../../../../src/modules/free/essentials/settings/settings.module';

describe('SettingsModule (Integration)', () => {
  let app: INestApplication;

  beforeAll(async () => {
    const moduleRef = await Test.createTestingModule({
      imports: [SettingsModule],
    }).compile();

    app = moduleRef.createNestApplication();
    await app.init();
  });

  afterAll(async () => {
    await app.close();
  });

  it('returns metadata for settings module', async () => {
    const server = app.getHttpServer();
    const response = await request(server).get('/settings');

    expect(response.status).toBe(200);
    expect(response.body).toHaveProperty('module', '{{ rapidkit_vendor_module }}');
    expect(response.body).toHaveProperty('version');
    expect(response.body).toHaveProperty('environment');
  });

  it('returns configuration snapshot details', async () => {
    const server = app.getHttpServer();
    const response = await request(server).get('/settings/config');

    expect(response.status).toBe(200);
    expect(response.body).toHaveProperty('config_files');
    expect(Array.isArray(response.body.config_files)).toBe(true);
  });

  it('exposes refresh endpoint', async () => {
    const server = app.getHttpServer();
    const response = await request(server).post('/settings/refresh');

    expect(response.status).toBe(200);
    expect(response.body).toHaveProperty('module', '{{ rapidkit_vendor_module }}');
  });

  it('exposes health endpoint', async () => {
    const server = app.getHttpServer();
    const response = await request(server).get('/settings/health');

    expect(response.status).toBe(200);
    expect(response.body).toHaveProperty('status');
    expect(response.body.status).toBe('ok');
  });
});
