// @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('P4: 주문 접수 관리', () => {
  test('주문 목록 접근', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/shop-orders');
    await expect(page).toHaveURL(/\/admin\/shop-orders/);
  });
  test('주문 접수 폼', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/shop-orders/create');
    await expect(page.locator('select[name="so_ds_idx"]')).toBeVisible();
  });
});

test.describe('P4: 판매/반품 관리', () => {
  test('판매 목록 접근', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/bag-sales');
    await expect(page).toHaveURL(/\/admin\/bag-sales/);
  });
  test('판매 등록 폼', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/bag-sales/create');
    await expect(page.locator('select[name="bs_ds_idx"]')).toBeVisible();
  });
});

test.describe('P4: 무료용 불출 관리', () => {
  test('불출 목록 접근', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/bag-issues');
    await expect(page).toHaveURL(/\/admin\/bag-issues/);
  });
  test('불출 처리 폼', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/bag-issues/create');
    await expect(page.locator('select[name="bi2_bag_code"]')).toBeVisible();
  });
});

test.describe('P4: 지자체관리자 접근', () => {
  test.beforeEach(async ({ page }) => {
    await login(page, 'local');
  });
  test('주문 접수 접근 가능', async ({ page }) => {
    await page.goto('/bag/shop-orders');
    await expect(page).toHaveURL(/\/admin\/shop-orders/);
  });
  test('판매 접근 가능', async ({ page }) => {
    await page.goto('/bag/bag-sales');
    await expect(page).toHaveURL(/\/admin\/bag-sales/);
  });
  test('불출 접근 가능', async ({ page }) => {
    await page.goto('/bag/bag-issues');
    await expect(page).toHaveURL(/\/admin\/bag-issues/);
  });
});
