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

test.describe('사이트 메뉴 (/bag/*) 페이지 접근', () => {
  test.beforeEach(async ({ page }) => {
    await login(page, 'local');
  });

  test('기본정보관리 (허브)', async ({ page }) => {
    await page.goto('/bag/basic-info');
    await expect(page).toHaveURL(/\/bag\/basic-info/);
    await expect(page.locator('a[href*="bag/prices"]')).toBeVisible();
    await expect(page.locator('a[href*="bag/packaging-units"]')).toBeVisible();
  });

  test('봉투 단가 (/bag/prices)', async ({ page }) => {
    await page.goto('/bag/prices');
    await expect(page).toHaveURL(/\/bag\/prices/);
    await expect(page.locator('th:has-text("발주단가")')).toBeVisible();
    await expect(page.locator('select[name="bag_kind_e"]')).toBeVisible();
    await expect(page.locator('select[name="bag_code"]')).toBeVisible();
  });

  test('봉투 단가 기간·구분 필터 GET', async ({ page }) => {
    await page.goto('/bag/prices?start_date=2026-01-01&end_date=2026-12-31&bag_kind_e=10');
    await expect(page).toHaveURL(/start_date=/);
    await expect(page).toHaveURL(/bag_kind_e=10/);
  });

  test('봉투 단가 기간 연·월·일 GET', async ({ page }) => {
    await page.goto('/bag/prices?start_y=2026&start_m=1&start_d=1&end_y=2026&end_m=12&end_d=31');
    await expect(page).toHaveURL(/start_y=/);
    await expect(page.locator('select[name="start_m"]')).toHaveValue('1');
    await expect(page.locator('select[name="end_m"]')).toHaveValue('12');
  });

  test('포장 단위 (/bag/packaging-units)', async ({ page }) => {
    await page.goto('/bag/packaging-units');
    await expect(page).toHaveURL(/\/bag\/packaging-units/);
    await expect(page.locator('th:has-text("박스당 팩 수")')).toBeVisible();
  });

  test('기본코드관리 (/bag/code-kinds)', async ({ page }) => {
    await page.goto('/bag/code-kinds');
    await expect(page).toHaveURL(/\/bag\/code-kinds/);
    await expect(page.locator('h3:has-text("기본코드 종류")')).toBeVisible();
  });

  test('발주 입고 관리', async ({ page }) => {
    await page.goto('/bag/purchase-inbound');
    await expect(page).toHaveURL(/\/bag\/purchase-inbound/);
    await expect(page.locator('text=발주 현황')).toBeVisible();
  });

  test('불출 관리', async ({ page }) => {
    await page.goto('/bag/issue');
    await expect(page).toHaveURL(/\/bag\/issue/);
    await expect(page.locator('th:has-text("불출일")')).toBeVisible();
  });

  test('재고 관리', async ({ page }) => {
    await page.goto('/bag/inventory');
    await expect(page).toHaveURL(/\/bag\/inventory/);
    await expect(page.locator('th:has-text("시군구 재고")')).toBeVisible();
  });

  test('판매 관리', async ({ page }) => {
    await page.goto('/bag/sales');
    await expect(page).toHaveURL(/\/bag\/sales/);
    await expect(page.locator('text=주문 접수')).toBeVisible();
  });

  test('판매 현황', async ({ page }) => {
    await page.goto('/bag/sales-stats');
    await expect(page).toHaveURL(/\/bag\/sales-stats/);
    await expect(page.locator('th:has-text("봉투코드")')).toBeVisible();
  });

  test('기간별 봉투 수불 현황', async ({ page }) => {
    await page.goto('/bag/flow?search=1&start_date=2026-01-01&end_date=2026-03-31&agg_mode=period');
    await expect(page).toHaveURL(/\/bag\/flow/);
    await expect(page.getByText('기간별 봉투 수불 현황')).toBeVisible();
    await expect(page.locator('th:has-text("전일재고")')).toBeVisible();
    await expect(page.locator('th:has-text("입고계")')).toBeVisible();
    await expect(page.locator('th:has-text("출고계")')).toBeVisible();
    await expect(page.getByRole('link', { name: '엑셀저장' })).toBeVisible();
  });

  test('통계 분석 — 전년 대비 판매 분석', async ({ page }) => {
    await page.goto('/bag/analytics/year-over-year?search=1&year=2025');
    await expect(page).toHaveURL(/\/bag\/analytics\/year-over-year/);
    await expect(page.getByText('전년 대비 판매 분석').first()).toBeVisible();
  });

  test('통계 분석 — 월별 판매 추이', async ({ page }) => {
    await page.goto('/bag/analytics/monthly-trend?search=1&base_ym=2025-05');
    await expect(page).toHaveURL(/\/bag\/analytics\/monthly-trend/);
    await expect(page.getByText('월별 판매 추이 분석').first()).toBeVisible();
  });

  test('통계 분석 — 계절별 판매 추이', async ({ page }) => {
    await page.goto('/bag/analytics/seasonal-trend?search=1&base_year=2025&season=spring');
    await expect(page).toHaveURL(/\/bag\/analytics\/seasonal-trend/);
    await expect(page.getByText('계절별 판매 추이 분석').first()).toBeVisible();
  });

  test('창', async ({ page }) => {
    await page.goto('/bag/window');
    await expect(page).toHaveURL(/\/bag\/window/);
    await expect(page.locator('text=창 관리')).toBeVisible();
  });

  test('도움말', async ({ page }) => {
    await page.goto('/bag/help');
    await expect(page).toHaveURL(/\/bag\/help/);
    await expect(page.locator('text=시스템 개요')).toBeVisible();
  });
});

test.describe('홈페이지 네비게이션 메뉴 링크', () => {
  test('메뉴 클릭으로 각 페이지 이동', async ({ page }) => {
    await login(page, 'local');
    await page.goto('/');

    // 발주 입고 관리 메뉴 클릭
    await page.click('a:has-text("발주 입고 관리")');
    await expect(page).toHaveURL(/\/bag\/purchase-inbound/);
  });
});
