import { test, expect } from '@playwright/test';

const REPO_URL = process.env.REPO_URL ?? `${process.env.BASE_URL ?? 'https://pbx.local:4443'}/downloads`;

test.describe('core-repository', () => {
  test.use({ ignoreHTTPSErrors: true });

  test('core-repository-check-presence_of_release_notes_and_changelog', async ({ page }, testInfo) => {
    testInfo.annotations.push({ type: 'automationStatus', description: 'automated' });
    testInfo.annotations.push({ type: 'testType', description: 'check' });
    testInfo.annotations.push({ type: 'description', description: 'Verify Release Notes and Changelog are present in the software repository' });

    await test.step('Navigate to the software repository / download portal', async () => {
      await page.goto(REPO_URL);
    });

    await test.step('Verify Release Notes file or link is visible', async () => {
      const link = page.locator('a, li, td').filter({ hasText: /release[\s_-]?notes?/i });
      await expect(link.first()).toBeVisible();
    });

    await test.step('Verify Changelog file or link is visible', async () => {
      const link = page.locator('a, li, td').filter({ hasText: /change[\s_-]?log/i });
      await expect(link.first()).toBeVisible();
    });
  });

  test('core-repository-check-presence_of_trouble_shooting_guide', async ({ page }, testInfo) => {
    testInfo.annotations.push({ type: 'automationStatus', description: 'automated' });
    testInfo.annotations.push({ type: 'testType', description: 'check' });
    testInfo.annotations.push({ type: 'description', description: 'Verify Troubleshooting Guide is present in the repository' });

    await test.step('Navigate to the documentation / download portal', async () => {
      await page.goto(REPO_URL);
    });

    await test.step('Verify Troubleshooting Guide link or file is present', async () => {
      const link = page.locator('a, li, td').filter({ hasText: /troubleshoot/i });
      await expect(link.first()).toBeVisible();
    });
  });

  test('core-repository-check-presence_of_all_mandatory_core-documentation_files', async ({ page }, testInfo) => {
    testInfo.annotations.push({ type: 'automationStatus', description: 'automated' });
    testInfo.annotations.push({ type: 'testType', description: 'check' });
    testInfo.annotations.push({ type: 'description', description: 'Verify all mandatory core documentation files are listed in the repository' });

    const mandatoryDocs: [string, RegExp][] = [
      ['Release Notes', /release[\s_-]?notes?/i],
      ['Installation Guide', /install/i],
      ['Configuration Guide', /configur/i],
      ['Troubleshooting Guide', /troubleshoot/i],
      ['Changelog', /change[\s_-]?log/i],
    ];

    await test.step('Navigate to the documentation portal', async () => {
      await page.goto(REPO_URL);
    });

    for (const [name, pattern] of mandatoryDocs) {
      await test.step(`Verify "${name}" is present`, async () => {
        const el = page.locator('a, li, td').filter({ hasText: pattern });
        await expect(el.first()).toBeVisible();
      });
    }
  });

  test('core-repository-check-presence_of_main_software_version_all_hypervisors', async ({ page }, testInfo) => {
    testInfo.annotations.push({ type: 'automationStatus', description: 'automated' });
    testInfo.annotations.push({ type: 'testType', description: 'check' });
    testInfo.annotations.push({ type: 'description', description: 'Verify main software version packages are available for all supported hypervisors (VMware, Hyper-V, KVM, etc.)' });

    const hypervisors = ['vmware', 'vsphere', 'hyper-v', 'hyperv', 'kvm', 'proxmox', 'xen'];

    await test.step('Navigate to the download portal', async () => {
      await page.goto(REPO_URL);
    });

    await test.step('Verify at least one hypervisor package is listed', async () => {
      const bodyText = (await page.locator('body').textContent())?.toLowerCase() ?? '';
      const found = hypervisors.some((hv) => bodyText.includes(hv));
      expect(found, `Expected at least one hypervisor entry (${hypervisors.join(', ')})`).toBe(true);
    });
  });

  test('core-repository-check-presence_of_patch_version_and_its_compatibility', async ({ page }, testInfo) => {
    testInfo.annotations.push({ type: 'automationStatus', description: 'automated' });
    testInfo.annotations.push({ type: 'testType', description: 'check' });
    testInfo.annotations.push({ type: 'description', description: 'Verify patch version packages and compatibility matrices are present' });

    await test.step('Navigate to the download portal', async () => {
      await page.goto(REPO_URL);
    });

    await test.step('Verify patch / compatibility information is present', async () => {
      const el = page.locator('body').filter({ hasText: /patch|hotfix|compatibility|compat/i });
      await expect(el).toBeVisible();
    });
  });

  test('core-repository-check-presence_of_companion_tools_and_supporting_scripts', async ({ page }, testInfo) => {
    testInfo.annotations.push({ type: 'automationStatus', description: 'automated' });
    testInfo.annotations.push({ type: 'testType', description: 'check' });
    testInfo.annotations.push({ type: 'description', description: 'Verify companion tools and supporting scripts are available in the repository' });

    await test.step('Navigate to the download portal', async () => {
      await page.goto(REPO_URL);
    });

    await test.step('Verify tools / scripts section is present', async () => {
      const el = page.locator('a, li, td, h2, h3').filter({ hasText: /tool|script|util|companion|support/i });
      await expect(el.first()).toBeVisible();
    });
  });
});
