// @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 });
}

async function loginAsLocal(page) {
  await login(page, 'local');
}

test.describe('P2-07/08: 판매 대행소 관리', () => {
  test('목록 접근 (Super Admin)', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/sales-agencies');
    await expect(page).toHaveURL(/\/admin\/sales-agencies/);
  });
  test('등록 폼 표시', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/sales-agencies/create');
    await expect(page.locator('input[name="sa_kind"]')).toBeVisible();
    await expect(page.locator('input[name="sa_code"]')).toBeVisible();
    await expect(page.locator('input[name="sa_name"]')).toBeVisible();
  });
  test('지자체관리자 접근', async ({ page }) => {
    await loginAsLocal(page);
    await page.goto('/bag/sales-agencies');
    await expect(page).toHaveURL(/\/admin\/sales-agencies/);
  });
});

test.describe('P2-09/10: 담당자 관리', () => {
  test('목록 접근', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/managers');
    await expect(page).toHaveURL(/\/admin\/managers/);
  });
  test('등록 폼 표시', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/managers/create');
    await expect(page.locator('input[name="mg_name"]')).toBeVisible();
  });
});

test.describe('P2-11/12: 업체 관리', () => {
  test('목록 접근', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/companies');
    await expect(page).toHaveURL(/\/admin\/companies/);
  });
  test('등록 폼 표시', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/companies/create');
    await expect(page.locator('select[name="cp_type"]')).toBeVisible();
    await expect(page.locator('input[name="cp_name"]')).toBeVisible();
  });
});

test.describe('P2-13/14: 무료용 대상자 관리', () => {
  test('목록 접근', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/free-recipients');
    await expect(page).toHaveURL(/\/admin\/free-recipients/);
  });
  test('등록 폼 표시', async ({ page }) => {
    await loginAsAdmin(page);
    await page.goto('/bag/free-recipients/create');
    await expect(page.locator('select[name="fr_type_code"]')).toBeVisible();
    await expect(page.locator('input[name="fr_name"]')).toBeVisible();
  });
});
