/**
 * Playwright Global Teardown
 *
 * Runs once after all tests. Use this to:
 * - Clean up test data
 * - Dispose singleton API contexts
 *
 * Customize for your project's cleanup needs.
 */
import { cleanupAllTestData, disposeAdminApi } from "./api/helpers/admin-api"
import { deleteAllEmails, disposeMailDev } from "./api/helpers/maildev"

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

    // Clean up test users
    try {
        const result = await cleanupAllTestData(baseURL)
        if (result.users > 0) {
            console.log(`🧹 Cleanup: ${result.users} test users deleted`)
        }
    } catch (err) {
        console.warn("⚠️ Test data cleanup failed:", err)
    }

    // Clean up MailDev emails (optional)
    try {
        await deleteAllEmails()
    } catch {
        // MailDev cleanup is optional
    }

    // Dispose singleton API contexts
    await Promise.all([disposeAdminApi(), disposeMailDev()])
}

export default globalTeardown
