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

test('owner creates assigned/open drafts, publishes, edits, copies and switches tenants using real UI', async ({ page, request }) => {
  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.getByRole('combobox', { name: '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);
  await page.getByRole('button', { name: 'Publish week', exact: true }).click();
  await page.getByRole('button', { name: 'Confirm publish', exact: true }).click();
  await expect(page.getByRole('dialog')).toHaveCount(0);
  expect((await api(request, 'staff', 'GET', `/my/schedule?week_start=${week}`)).shifts).toHaveLength(1);
  expect((await api(request, 'staff', 'GET', `/schedule/week?week_start=${week}`)).shifts).toHaveLength(2);
  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();
  expect((await api(request, 'staff', 'GET', `/my/schedule?week_start=${week}`)).shifts).toHaveLength(0);
  await page.getByRole('button', { name: 'Copy week', exact: true }).click();
  await dialog.getByLabel('Target week').fill('2026-10-12');
  const beforeCopy = dbRows('shifts').length;
  await dialog.getByRole('button', { name: 'Preview copy', exact: true }).click();
  await expect(dialog.getByRole('region', { name: 'Copy preview' })).toBeVisible();
  expect(dbRows('shifts')).toHaveLength(beforeCopy);
  await dialog.getByRole('button', { name: 'Commit copy', exact: true }).click();
  await expect(dialog).toHaveCount(0);
  const copy = await api(request, 'owner', 'GET', '/schedule/week?week_start=2026-10-12');
  expect(copy.shifts).toHaveLength(2);
  expect(copy.shifts.every((s: any) => s.published_at === null)).toBe(true);
  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-copy-switch', { drafts, after, copy, sql: dbRows('shifts') });
});

test('staff browser excludes drafts, refreshes published own/team view and switches stores', async ({ page, request }) => {
  const week = '2026-11-02', notes = fixture.marker + ' staff browser';
  await api(request, 'owner', 'POST', '/schedule/shifts', { store_membership_id: fixture.actors.staff.memberships.a, starts_at: week + 'T17:00:00Z', ends_at: week + 'T19:00:00Z', notes }, 201);
  await login(page, 'staff', '/staff/schedule');
  await page.getByLabel('Week containing', { exact: true }).fill(week);
  await expect(page.getByText('No published shifts this week.', { exact: true })).toBeVisible();
  await expect(page.getByText(notes, { exact: true })).toHaveCount(0);
  await api(request, 'owner', 'POST', '/schedule/publish', { week_start: week });
  await page.getByRole('button', { name: 'Reload week', exact: true }).click();
  await expect(page.getByText(notes, { exact: true })).toBeVisible();
  await page.getByRole('button', { name: 'Team Schedule', exact: true }).click();
  await expect(page.getByRole('table', { name: 'Weekly schedule', exact: true })).toBeVisible();
  await expect(page.getByText(notes, { exact: true })).toBeVisible();
  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(notes, { exact: true })).toHaveCount(0);
  await evidence('staff-browser-visibility-switch', { week, notes, store: fixture.stores.b.id });
});
