// @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('P3: 발주 관리', () => {
  test('발주 현황 접근', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/bag-orders');
    await expect(page).toHaveURL(/\/admin\/bag-orders/);
  });

  test('발주 등록 폼', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/bag-orders/create');
    await expect(page.locator('input[name="bo_order_date"]')).toBeVisible();
  });

  test('발주 변경 허브', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/order/change');
    await expect(page).toHaveURL(/\/bag\/order\/change/);
    await expect(page.locator('select[name="month"]')).toBeVisible();
    await expect(page.locator('legend:has-text("변경 구분")')).toBeVisible();
  });

  test('기간 필터 조회', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/bag-orders?start_date=2026-01-01&end_date=2026-12-31');
    await expect(page).toHaveURL(/start_date/);
  });
});

test.describe('P3: 입고 관리', () => {
  test('입고 현황 접근', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/bag-receivings');
    await expect(page).toHaveURL(/\/admin\/bag-receivings/);
  });

  test('입고 처리 폼', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/bag-receivings/create');
    await expect(page.locator('select[name="br_bo_idx"]')).toBeVisible();
  });
});

test.describe('P3: 재고 현황', () => {
  test('재고 현황 접근', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/bag-inventory');
    await expect(page).toHaveURL(/\/admin\/bag-inventory/);
  });
});

test.describe('P3: 지자체관리자 접근', () => {
  test('발주/입고/재고 접근 가능', async ({ page }) => {
    await login(page, 'local');
    await page.goto('/bag/bag-orders');
    await expect(page).toHaveURL(/\/admin\/bag-orders/);
    await page.goto('/bag/bag-receivings');
    await expect(page).toHaveURL(/\/admin\/bag-receivings/);
    await page.goto('/bag/bag-inventory');
    await expect(page).toHaveURL(/\/admin\/bag-inventory/);
  });
});
