import { test, expect, fixture, login, api, dbRows, evidence } from './helpers';

test('owner creates assigned/open drafts, publishes to staff/team, edits back to draft and switches tenants', async ({ page, request, browser }) => {
  const week = '2026-10-05';
  const assignedNote = fixture.marker + ' assigned';
  const openNote = fixture.marker + ' open';
  await login(page, 'owner', '/seller/schedule');
  await page.getByLabel('Week containing', { exact: true }).fill(week);
  await expect(page.getByRole('table', { name: /Weekly schedule/i })).toBeVisible();
  for (const [membership, notes, day] of [[fixture.actors.staff.memberships.a, assignedNote, week], ['', openNote, '2026-10-06']]) {
    await page.getByRole('button', { name: 'Add shift', exact: true }).click();
    const dialog = page.getByRole('dialog');
    await dialog.getByLabel('Employee', { exact: true }).selectOption(membership);
    await dialog.getByLabel('Starts (store time)', { exact: true }).fill(`${day}T09:00`);
    await dialog.getByLabel('Ends (store time)', { exact: true }).fill(`${day}T17:00`);
    await dialog.getByLabel('Notes', { exact: true }).fill(notes);
    await dialog.getByRole('button', { name: 'Save shift', exact: true }).click();
    await expect(dialog).toHaveCount(0);
  }
  const drafts = await api(request, 'owner', 'GET', `/schedule/week?week_start=${week}`);
  expect(drafts.shifts).toHaveLength(2);
  expect(drafts.shifts.every((s: any) => s.published_at === null)).toBe(true);
  expect(drafts.shifts.filter((s: any) => s.store_membership_id === null)).toHaveLength(1);
  const assigned = drafts.shifts.find((s: any) => s.notes === assignedNote);
  expect(dbRows('shifts').find((s: any) => s.id === assigned.id).published_at).toBeNull();
  expect((await api(request, 'staff', 'GET', `/my/schedule?week_start=${week}`)).shifts).toHaveLength(0);
  expect((await api(request, 'staff', 'GET', `/schedule/week?week_start=${week}`)).shifts).toHaveLength(0);
  const context = await browser.newContext();
  const staff = await context.newPage();
  try {
    await login(staff, 'staff', '/staff/schedule');
    await staff.getByLabel('Week containing', { exact: true }).fill(week);
    await expect(staff.getByText(assignedNote, { exact: true })).toHaveCount(0);
    await page.getByRole('button', { name: 'Publish week', exact: true }).click();
    await expect.poll(async () => (await api(request, 'staff', 'GET', `/my/schedule?week_start=${week}`)).shifts.length).toBe(1);
    await staff.getByRole('button', { name: 'Reload week', exact: true }).click();
    await expect(staff.getByText(assignedNote, { exact: true })).toBeVisible();
    expect((await api(request, 'staff', 'GET', `/schedule/week?week_start=${week}`)).shifts).toHaveLength(2);
    // Editing a published shift uses its fresh opaque revision and hides it again.
    await page.getByRole('button', { name: new RegExp(fixture.actors.staff.name + '.*09:00') }).click();
    const dialog = page.getByRole('dialog');
    await dialog.getByLabel('Notes', { exact: true }).fill(assignedNote + ' revised');
    await dialog.getByRole('button', { name: 'Save shift', exact: true }).click();
    await expect(dialog).toHaveCount(0);
    const after = await api(request, 'owner', 'GET', `/schedule/week?week_start=${week}`);
    expect(after.shifts.find((s: any) => s.id === assigned.id).published_at).toBeNull();
    await staff.getByRole('button', { name: 'Reload week', exact: true }).click();
    await expect(staff.getByText(assignedNote, { exact: true })).toHaveCount(0);
    expect((await api(request, 'staff', 'GET', `/my/schedule?week_start=${week}`)).shifts).toHaveLength(0);
    await page.getByRole('combobox', { name: 'Store', exact: true }).selectOption(fixture.stores.b.id);
    await page.getByLabel('Week containing', { exact: true }).fill(week);
    await expect(page.getByText(openNote, { exact: true })).toHaveCount(0);
    await expect(page.getByText(assignedNote + ' revised', { exact: true })).toHaveCount(0);
    expect((await api(request, 'owner', 'GET', `/schedule/week?week_start=${week}`, undefined, 200, 'b')).shifts).toHaveLength(0);
    await evidence('schedule-browser-draft-publish-edit-switch', { drafts, after, sql: dbRows('shifts') });
  } finally { await context.close(); }
});
