// (C) 2021-2023 GoodData Corporation
const fs = require('fs');
const path = require('path');

const {
    createFixture
} = require('./fixture_bear.cjs');
const { log } = require('./logger.cjs');

const BEAR_FIXTURE_METADATA_EXTENSIONS = {
    goodsales: path.resolve('../../fixtures/goodsales/GoodSales/5/bear_metadata_extension.json'),
    complex_model: path.resolve('../../fixtures/goodsales/ComplexModel/bear_metadata_extension.json')
};

const BEAR_FIXTURE_PATHS = {
    goodsales: 'GoodSales/5',
    complex_model: 'ComplexModel'
};

async function createBearReferenceWorkspace(fixtureName, userName, password, host, authToken) {
    let extensionExists = true;
    const extensionFile = BEAR_FIXTURE_METADATA_EXTENSIONS[fixtureName];
    if (!fs.existsSync(extensionFile)) {
        log(`There is no extension for ${fixtureName}. File ${extensionFile} does not exist.`);
        extensionExists = false;
    }

    const dateString = new Intl.DateTimeFormat('en', {
        dateStyle: 'medium',
        timeStyle: 'short',
        hour12: false
    }).format(new Date());
    return createFixture(
        path.resolve(`../../fixtures/${BEAR_FIXTURE_PATHS[fixtureName]}`),
        userName,
        password,
        host,
        authToken,
        `Test reference workspace ${dateString}`,
        extensionExists ? path.resolve(extensionFile) : undefined,
    );
}

module.exports = {
    createBearReferenceWorkspace
};
