// (C) 2021-2023 GoodData Corporation
import {
    createTigerWorkspaceWithPrefix,
    setTigerWorkspaceLayoutFromFixtures,
    setTigerChildWorkspaceLayoutFromFixtures,
    setTigerWorkspaceLayoutFromCustomFile,
    createWorkspaceSettings
} from './fixture_tiger.js';
import { log, logLn } from './logger.mjs';

async function createWorkspaceFromFixture(
    workspacePrefix, fixtureType, dataSource, token, host, backend, metadataExtension, newFeatureFixture
) {
    const workspaceId = await createTigerWorkspaceWithPrefix(workspacePrefix, token, host, backend);

    await createWorkspaceSettings(workspaceId, token, host, backend, 'FISCAL_YEAR', { monthOffset: 2, monthPrefix: 'P', quarterPrefix: 'Q', yearPrefix: 'FY' });

    await createWorkspaceSettings(workspaceId, token, host, backend, 'ACTIVE_CALENDARS', { default: 'STANDARD', standard: true, fiscal: true });

    logLn(
        `Creating fixture. Type: ${fixtureType}, datasource: ${dataSource}, additional md objects: ${metadataExtension}`,
    );
    await setTigerWorkspaceLayoutFromFixtures(
        workspaceId,
        fixtureType,
        dataSource,
        token,
        host,
        backend,
        metadataExtension,
        newFeatureFixture
    );
    return workspaceId;
}

async function createChildTigerReferenceWorkspace(
    workspacePrefix, fixtureType, token, host, backend, testReferenceWorkspaceId, customFixtureFile
) {
    const workspaceId = await createTigerWorkspaceWithPrefix(
        workspacePrefix, token, host, backend, testReferenceWorkspaceId);

    logLn(
        `Creating fixture. Type: ${fixtureType}`,
    );
    await setTigerChildWorkspaceLayoutFromFixtures(
        workspaceId,
        token,
        host,
        backend,
        customFixtureFile,
    );
    return workspaceId;
}

async function createTigerReferenceWorkspace(
    host,
    tigerApiToken,
    testWorkspaceId,
    tigerDatasourceName,
    fixtureType = 'goodsales',
    sdkBackend,
    metadataExtension,
    workspaceNamePrefix,
    newFeatureFixture
) {
    if (testWorkspaceId) {
        log(
            `WORKSPACE_ID ${testWorkspaceId} is already present. Delete the workspace by running delete_tiger_workspace.js or remove the the record in  .env\n`,
        );
        return undefined;
    }

    if (!(host && tigerApiToken)) {
        log('HOST, TIGER_API_TOKEN need to be set\n');
        return undefined;
    }

    if (!(tigerDatasourceName && fixtureType)) {
        log('TIGER_DATASOURCES_NAME, FIXTURE_TYPE need to be set\n');
        return undefined;
    }

    const testReferenceWorkspaceId = await createWorkspaceFromFixture(
        workspaceNamePrefix,
        fixtureType,
        tigerDatasourceName,
        tigerApiToken,
        host,
        sdkBackend,
        metadataExtension,
        newFeatureFixture
    );
    return testReferenceWorkspaceId;
}

async function createWorkspaceFromCustomFixture(
    host,
    tigerApiToken,
    testWorkspaceId,
    tigerDatasourceName,
    fixtureType = 'goodsales',
    sdkBackend,
    metadataExtension,
    workspaceNamePrefix,
) {
    if (testWorkspaceId) {
        log(
            `WORKSPACE_ID ${testWorkspaceId} is already present. Delete the workspace by running delete_tiger_workspace.js or remove the the record in  .env\n`,
        );
        return undefined;
    }

    if (!(host && tigerApiToken)) {
        log('HOST, TIGER_API_TOKEN need to be set\n');
        return undefined;
    }

    if (!(tigerDatasourceName && fixtureType)) {
        log('TIGER_DATASOURCES_NAME, FIXTURE_TYPE need to be set\n');
        return undefined;
    }
    const workspaceId = await createTigerWorkspaceWithPrefix(workspaceNamePrefix, tigerApiToken, host, sdkBackend);

    log(
        `Creating fixture for workspace ${workspaceId}. Type: ${fixtureType}, datasource: ${tigerDatasourceName}, md objects: ${metadataExtension}\n`,
    );
    await setTigerWorkspaceLayoutFromCustomFile(
        workspaceId,
        tigerApiToken,
        host,
        sdkBackend,
        tigerDatasourceName,
        metadataExtension
    );

    return workspaceId;
}

export {
    createWorkspaceFromFixture,
    createTigerReferenceWorkspace,
    createChildTigerReferenceWorkspace,
    createWorkspaceFromCustomFixture
};
