/**
 * Global Hospital — happy-path smoke tests.
 *
 * Uses saved auth state from global-setup.ts (same pattern as Ramaiah).
 * Last test signs out to release the agent session.
 */
import { test, expect } from '@playwright/test';
import { loginAs, waitForApp } from './helpers';

const BASE = 'https://global.engage.healix360.net';

test.describe('Global — CC Agent', () => {

    test('landing page loads', async ({ page }) => {
        await page.goto(BASE + '/');
        await waitForApp(page);
        await expect(page.locator('aside').first()).toBeVisible();
    });

    test('Call History page loads', async ({ page }) => {
        await page.goto(BASE + '/call-history');
        await waitForApp(page);
        await expect(page.locator('text="Call History"').first()).toBeVisible();
    });

    test('Patients page loads', async ({ page }) => {
        await page.goto(BASE + '/patients');
        await waitForApp(page);
        const search = page.getByPlaceholder(/search/i).or(page.getByLabel(/search/i));
        await expect(search.first()).toBeVisible();
    });

    test('Appointments page loads', async ({ page }) => {
        await page.goto(BASE + '/appointments');
        await waitForApp(page);
        await expect(
            page.locator('text=/Appointment|Schedule|No appointment/i').first(),
        ).toBeVisible({ timeout: 10_000 });
    });

    test('My Performance page loads', async ({ page }) => {
        await page.goto(BASE + '/my-performance');
        await waitForApp(page);
        await expect(page.getByRole('button', { name: 'Today' })).toBeVisible();
    });

    test('sidebar has CC Agent nav items', async ({ page }) => {
        await page.goto(BASE + '/');
        await waitForApp(page);
        const sidebar = page.locator('aside').first();
        for (const item of ['Call Desk', 'Call History', 'Patients', 'Appointments', 'My Performance']) {
            await expect(sidebar.locator(`text="${item}"`)).toBeVisible();
        }
    });

    // Last test — sign out to release session
    test('sign-out completes', async ({ page }) => {
        await page.goto(BASE + '/');
        await waitForApp(page);

        const sidebar = page.locator('aside').first();
        const accountArea = sidebar.locator('[class*="account"], [class*="avatar"]').last();
        if (await accountArea.isVisible()) await accountArea.click();

        const signOutBtn = page.locator('button, [role="menuitem"]').filter({ hasText: /sign out/i }).first();
        if (await signOutBtn.isVisible({ timeout: 5_000 }).catch(() => false)) {
            await signOutBtn.click();
            const modal = page.locator('[role="dialog"]');
            await expect(modal).toBeVisible({ timeout: 5_000 });
            await modal.getByRole('button', { name: /sign out/i }).click();
            await expect(page).toHaveURL(/\/login/, { timeout: 15_000 });
        }
    });
});

test.describe('Global — Supervisor', () => {
    test.beforeEach(async ({ page }) => {
        await page.goto(BASE + '/login');
        await page.locator('input[type="email"], input[placeholder*="@"]').first().fill('dr.ramesh@globalcare.com');
        await page.locator('input[type="password"]').first().fill('Global@123');
        await page.getByRole('button', { name: 'Sign in', exact: true }).click();
        await expect(page).not.toHaveURL(/\/login/, { timeout: 20_000 });
        await waitForApp(page);
    });

    test('landing page loads', async ({ page }) => {
        await expect(page.locator('aside').first()).toBeVisible();
    });

    test('Patients page loads', async ({ page }) => {
        await page.goto(BASE + '/patients');
        await waitForApp(page);
        const search = page.getByPlaceholder(/search/i).or(page.getByLabel(/search/i));
        await expect(search.first()).toBeVisible();
    });

    test('Appointments page loads', async ({ page }) => {
        await page.goto(BASE + '/appointments');
        await waitForApp(page);
        await expect(
            page.locator('text=/Appointment|Schedule|No appointment/i').first(),
        ).toBeVisible({ timeout: 10_000 });
    });

    test('Campaigns page loads', async ({ page }) => {
        await page.goto(BASE + '/campaigns');
        await waitForApp(page);
        await expect(
            page.locator('text=/Campaign|No campaign/i').first(),
        ).toBeVisible({ timeout: 10_000 });
    });

    test('Settings page loads', async ({ page }) => {
        await page.goto(BASE + '/settings');
        await waitForApp(page);
        await expect(
            page.locator('text=/Settings|Configuration/i').first(),
        ).toBeVisible({ timeout: 10_000 });
    });
});
