"use strict";
/*
 * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH under
 * one or more contributor license agreements. See the NOTICE file distributed
 * with this work for additional information regarding copyright ownership.
 * Licensed under the Camunda License 1.0. You may not use this file
 * except in compliance with the Camunda License 1.0.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteOrganization = void 0;
async function deleteOrganization(page, uuid) {
    console.log(`Deleting organization with UUID: ${uuid}`);
    const tokenResponse = await page.request.post('https://weblogin.cloud.ultrawombat.com/oauth/token', {
        headers: {
            'Content-Type': 'application/json',
        },
        data: {
            grant_type: 'client_credentials',
            audience: 'cloud.ultrawombat.com',
            client_id: process.env.EXTERNAL_CONSOLE_API_CLIENT_ID,
            client_secret: process.env.EXTERNAL_CONSOLE_API_CLIENT_SECRET,
        },
        timeout: 60000,
    });
    if (tokenResponse.status() !== 200) {
        const errorBody = await tokenResponse.text();
        throw new Error(`Failed to fetch access token. Response: ${errorBody}`);
    }
    const tokenJson = await tokenResponse.json();
    const token = tokenJson.access_token;
    console.log('Access Token:', token);
    const deleteUrl = `https://accounts.cloud.ultrawombat.com/external/qa/organizations/${uuid}`;
    console.log(`Delete URL: ${deleteUrl}`);
    const deleteResponse = await page.request.delete(deleteUrl, {
        headers: {
            'Content-Type': 'application/json',
            Authorization: `Bearer ${token}`,
        },
        timeout: 60000,
    });
    if (deleteResponse.status() !== 200) {
        const errorBody = await deleteResponse.text();
        throw new Error(`Failed to delete organization. Status: ${deleteResponse.status()}. Response: ${errorBody}`);
    }
    console.log(`Organization with UUID ${uuid} deleted successfully.`);
    // Verify the organization is actually deleted by checking it returns 404
    const maxRetries = 3;
    const retryDelay = 5000;
    for (let attempt = 1; attempt <= maxRetries; attempt++) {
        await new Promise((resolve) => setTimeout(resolve, retryDelay));
        const verifyResponse = await page.request.delete(deleteUrl, {
            headers: {
                'Content-Type': 'application/json',
                Authorization: `Bearer ${token}`,
            },
        });
        if (verifyResponse.status() === 404) {
            console.log(`Verified: organization ${uuid} is deleted (404 on attempt ${attempt}).`);
            return;
        }
        console.log(`Organization ${uuid} still exists (status ${verifyResponse.status()}), retry ${attempt}/${maxRetries}...`);
    }
    throw new Error(`Organization ${uuid} could not be deleted after ${maxRetries} verification attempts`);
}
exports.deleteOrganization = deleteOrganization;
