import { test, expect } from '@playwright/test';
import { RoleAuthHelper } from '../../helpers/helpers';
import { ApiHelper, DEPT_ID, sessionPost } from '../../helpers/api-helper';

const STAFF_UUID = '1877e4d8-9579-45b3-8190-19becf3f14f2';

test.describe('Send to Staff Tests', () => {
  test.setTimeout(120000);

  test('send complaint to staff and staff sees it', async ({ page }) => {
    const api = new ApiHelper(page);
    await api.authenticate('px_admin');
    const { body: cb } = await api.createComplaint();
    const complaintId = cb?.id;
    if (!complaintId) { test.skip(); return; }
    await api.activateComplaint(complaintId);

    const auth = new RoleAuthHelper(page);
    await auth.login('hospital_admin');
    await page.waitForTimeout(800);

    await page.goto(`/complaints/${complaintId}/`);
    await page.waitForLoadState('domcontentloaded');
    await page.waitForTimeout(2500);

    const sendResp = await sessionPost(page, `/complaints/${complaintId}/send-to/`, {
      recipient_type: 'person',
      person_id: STAFF_UUID,
      note: 'E2E test - sending complaint to staff',
    });
    expect([200, 302].includes(sendResp.status())).toBeTruthy();
    await page.waitForTimeout(800);

    await auth.logout();
    await auth.login('staff');
    await page.waitForTimeout(800);

    await page.goto('/complaints/?status=in_progress');
    await page.waitForLoadState('domcontentloaded');
    await page.waitForTimeout(2500);

    const pageText = (await page.textContent('body')) || '';
    expect(pageText.length).toBeGreaterThan(0);
    await page.waitForTimeout(800);
  });

  test('send inquiry to staff', async ({ page }) => {
    const api = new ApiHelper(page);
    await api.authenticate('px_admin');
    const { body: ib } = await api.createInquiry();
    const inquiryId = ib?.id;
    if (!inquiryId) { test.skip(); return; }

    const auth = new RoleAuthHelper(page);
    await auth.login('hospital_admin');
    await page.waitForTimeout(800);

    await page.goto(`/inquiries/${inquiryId}/`);
    await page.waitForLoadState('domcontentloaded');
    await page.waitForTimeout(2500);

    await sessionPost(page, `/inquiries/${inquiryId}/activate/`, {});
    await page.waitForTimeout(800);

    const sendResp = await sessionPost(page, `/inquiries/${inquiryId}/send-to/`, {
      recipient_type: 'person',
      person_id: STAFF_UUID,
      note: 'E2E test - sending inquiry to staff for response',
    });
    expect([200, 302].includes(sendResp.status())).toBeTruthy();
    await page.waitForTimeout(800);
  });
});
