// #region HealthPageIntegrationTest:Module [TYPE Function]
// @SEMANTICS: health-page, integration-test, slug-link, delete-flow, model-contract
// @PURPOSE: Lock dashboard health page contract for slug navigation and model-delegated report deletion.
// @LAYER UI (Tests)
// @RELATION DEPENDS_ON -> [HealthCenterPage]
// @RELATION DEPENDS_ON -> [HealthCenterModel]
// @RATIONALE Tests 2 and 3 now verify the HealthCenterModel instead of the page,
//   since delete/store logic was extracted to the model per model-first architecture.

import { describe, it, expect } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';

const PAGE_PATH = path.resolve(
  process.cwd(),
  'src/routes/dashboards/health/+page.svelte',
);
const MODEL_PATH = path.resolve(
  process.cwd(),
  'src/lib/models/HealthCenterModel.svelte.ts',
);

describe('Dashboard health page contract', () => {
  it('renders slug-first dashboard link bound to environment route context', () => {
    const source = fs.readFileSync(PAGE_PATH, 'utf-8');

    expect(source).toContain("{$t.health?.validation_details || 'Dashboards'}");
    expect(source).toContain("item.dashboard_slug || item.dashboard_id");
    expect(source).toContain("ROUTES.dashboards.detail(item.dashboard_slug || item.dashboard_id, item.environment_id)");
  });

  it('keeps explicit delete report flow with confirm and DELETE request — delegated to HealthCenterModel', () => {
    const source = fs.readFileSync(PAGE_PATH, 'utf-8');
    const modelSource = fs.readFileSync(MODEL_PATH, 'utf-8');

    // Page BINDS_TO model
    expect(source).toContain('BINDS_TO -> [HealthCenterModel]');
    // Page delegates delete to model
    expect(source).toContain('m.handleDeleteReport(item)');
    // Model contains the delete logic
    expect(modelSource).toContain('async handleDeleteReport(item: HealthItem)');
    expect(modelSource).toContain("requestApi(`/health/summary/${item.record_id}`, 'DELETE')");
  });

  it('reuses global health store refresh for all-environment summary loads — delegated to HealthCenterModel', () => {
    const source = fs.readFileSync(PAGE_PATH, 'utf-8');
    const modelSource = fs.readFileSync(MODEL_PATH, 'utf-8');

    // Page BINDS_TO model
    expect(source).toContain('m.healthData');
    // Model imports healthStore
    expect(modelSource).toContain("import { healthStore } from '$lib/stores/health.svelte.js';");
    expect(modelSource).toContain('? getHealthSummary(this.selectedEnvId)');
    expect(modelSource).toContain(': healthStore.refresh();');
  });
});
// #endregion HealthPageIntegrationTest:Module
