// @ts-check
const { test, expect } = require('@playwright/test');
const { login } = require('./helpers/auth');

async function loginAsAdmin(page) {
  await login(page, 'admin');
  await page.locator('input[name="lg_idx"]').first().check();
  await page.click('button[type="submit"]');
  await page.waitForURL(url => !url.pathname.includes('select-local-government'), { timeout: 30000 });
}

test.describe('P5: 판매 대장', () => {
  test('일자별 판매 대장 접근', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/reports/sales-ledger');
    await expect(page).toHaveURL(/\/bag\/reports\/sales-ledger/);
    await expect(page.getByText('지정 판매소 판매 대장').first()).toBeVisible();
    await expect(page.getByText('판매소명').first()).toBeVisible();
    await expect(page.getByText('[지정판매소] 일자별 판매대장').first()).toBeVisible();
  });
  test('기간별 판매 대장', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/reports/sales-ledger?mode=period&start_date=2026-01-01&end_date=2026-12-31');
    await expect(page).toHaveURL(/mode=period/);
    await expect(page.getByText('[지정판매소] 기간별 판매대장').first()).toBeVisible();
    await expect(page.getByRole('columnheader', { name: '일자' })).toHaveCount(0);
    await expect(page.getByRole('columnheader', { name: '지정번호' })).toBeVisible();
  });
});

test.describe('P5: 일계표', () => {
  test('일계표 접근', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/reports/daily-summary');
    await expect(page).toHaveURL(/\/bag\/reports\/daily-summary/);
    await expect(page.getByRole('heading', { name: '일계표' }).first()).toBeVisible();
    await expect(page.getByRole('button', { name: '조회' })).toBeVisible();
    await expect(page.getByRole('link', { name: '엑셀저장' })).toBeVisible();
    await expect(page.getByRole('button', { name: '인쇄' })).toBeVisible();
    await expect(page.locator('#daily-summary-table')).toBeVisible();
    await expect(page.getByRole('columnheader', { name: '구분' })).toBeVisible();
    await expect(page.getByRole('columnheader', { name: '봉투종류' })).toBeVisible();
  });
});

test.describe('P5: 기간별 판매현황', () => {
  test('기간별 판매현황 접근', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/reports/period-sales');
    await expect(page).toHaveURL(/\/bag\/reports\/period-sales/);
    await expect(page.getByRole('heading', { name: /기간별 판매현황/ }).first()).toBeVisible();
    await expect(page.getByRole('button', { name: '조회' })).toBeVisible();
    await expect(page.getByRole('link', { name: '엑셀저장' })).toBeVisible();
    await expect(page.getByRole('button', { name: '인쇄' })).toBeVisible();
    await expect(page.locator('#period-sales-table')).toBeVisible();
    await expect(page.getByRole('columnheader', { name: '품목' })).toBeVisible();
    await expect(page.locator('select[name="mode"]')).toBeVisible();
  });
});

test.describe('P5: 봉투 수급 계획', () => {
  test('수급 계획 조회', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/reports/supply-demand?search=1');
    await expect(page).toHaveURL(/\/bag\/reports\/supply-demand/);
    await expect(page.getByText('쓰레기봉투 수급 계획').first()).toBeVisible();
    await expect(page.getByRole('columnheader', { name: '발주예정일' })).toBeVisible();
    await expect(page.getByRole('columnheader', { name: '소진일수' })).toBeVisible();
  });
});

test.describe('P5: 지자체관리자 접근', () => {
  test('리포트 접근 가능', async ({ page }) => {
    await login(page, 'local');
    await page.goto('/bag/reports/sales-ledger');
    await expect(page).toHaveURL(/\/bag\/reports\/sales-ledger/);
    await page.goto('/bag/reports/daily-summary');
    await expect(page).toHaveURL(/\/bag\/reports\/daily-summary/);
  });
});
