/**
 * Playwright Global Setup
 *
 * Runs once before all tests. Use this to:
 * - Seed test data via API action hooks
 * - Create test users
 * - Verify the dev environment is accessible
 *
 * Customize the setup_testing action call for your project,
 * or remove it if not needed.
 */
import { request } from "@playwright/test"
import { API_BASE } from "./fixtures/test-constants"

async function globalSetup() {
    const baseURL = process.env.CODING_URL || "https://localhost:3000"

    // Verify dev environment is reachable
    const ctx = await request.newContext({
        baseURL,
        ignoreHTTPSErrors: true,
        extraHTTPHeaders: {
            "User-Agent": "Playwright",
        },
    })

    try {
        const res = await ctx.get("/")
        if (!res.ok()) {
            console.warn(`⚠️ Dev environment at ${baseURL} returned ${res.status()}`)
        } else {
            console.log(`✅ Dev environment reachable at ${baseURL}`)
        }

        // Uncomment and adapt for project-specific test data seeding:
        // const seedRes = await ctx.post(`${API_BASE}/action`, {
        //     params: { cmd: "setup_testing" },
        //     data: { scope: "all" },
        //     headers: { Token: ADMIN_TOKEN },
        // })
        // if (seedRes.ok()) {
        //     console.log("✅ Test data seeded")
        // }
    } finally {
        await ctx.dispose()
    }
}

export default globalSetup
