"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FixturesService = void 0;
const common_1 = require("@nestjs/common");
const solid_introspect_service_1 = require("./solid-introspect.service");
const module_metadata_helper_service_1 = require("../helpers/module-metadata-helper.service");
const string_helper_1 = require("../helpers/string.helper");
let FixturesService = class FixturesService {
    constructor(introspectService, moduleMetadataHelper) {
        this.introspectService = introspectService;
        this.moduleMetadataHelper = moduleMetadataHelper;
        this.logger = new common_1.Logger(this.constructor.name);
    }
    async setupFixtures(moduleName, scenarioName) {
        const scenarioFixture = await this.loadScenario(moduleName, scenarioName);
        if (!scenarioFixture?.models || scenarioFixture.models.length === 0) {
            this.logger.warn(`No models found to setup for scenario: ${scenarioName} in module: ${moduleName}`);
            return;
        }
        for (const modelFixture of scenarioFixture.models) {
            const modelServiceInstance = await this.loadModelService(modelFixture.singularName);
            try {
                const createdInstance = await modelServiceInstance.create(modelFixture.data);
                this.logger.log(`Successfully created fixture for model: ${modelFixture.singularName} with ID: ${createdInstance.id}`);
            }
            catch (error) {
                this.logger.error(`Error creating fixture for model: ${modelFixture.singularName} - ${error.message}`);
            }
        }
    }
    async tearDownFixtures(moduleName, scenarioName) {
        const scenarioFixture = await this.loadScenario(moduleName, scenarioName);
        if (!scenarioFixture?.models || scenarioFixture.models.length === 0) {
            this.logger.warn(`No models found to tear down for scenario: ${scenarioName} in module: ${moduleName}`);
            return;
        }
        for (const modelFixture of scenarioFixture.models.reverse()) {
            const modelServiceInstance = await this.loadModelService(modelFixture.singularName);
            try {
                const deleteCriteria = modelFixture.data;
                await modelServiceInstance.delete(deleteCriteria);
                this.logger.log(`Successfully deleted fixture for model: ${modelFixture.singularName}`);
            }
            catch (error) {
                this.logger.error(`Error deleting fixture for model: ${modelFixture.singularName} - ${error.message}`);
            }
        }
    }
    async loadScenario(moduleName, scenarioName) {
        const moduleMetadataConfiguration = await this.moduleMetadataHelper.getModuleMetadataConfiguration(await this.moduleMetadataHelper.getModuleMetadataFilePath(moduleName));
        if (!moduleMetadataConfiguration) {
            this.logger.error(`No module metadata configuration found for module: ${moduleName}`);
            return;
        }
        this.logger.debug(`Module Metadata Configuration loaded for module: ${moduleName}`);
        const allFixtures = moduleMetadataConfiguration?.fixtures;
        this.logger.debug(`All Fixtures: ${JSON.stringify(allFixtures)}`);
        if (!allFixtures?.scenarios.length || allFixtures?.scenarios.length === 0) {
            this.logger.error(`No scenarios defined in the module metadata configuration for module: ${moduleName}`);
            return;
        }
        const scenarioFixture = allFixtures.scenarios.find((scenario) => scenario.name === scenarioName);
        if (!scenarioFixture) {
            this.logger.error(`Scenario: ${scenarioName} not found in module metadata configuration for module: ${moduleName}`);
            return;
        }
        this.logger.log(`Scenario: ${scenarioName} found with fixtures to setup.`);
        return scenarioFixture;
    }
    async loadModelService(singularName) {
        const modelServiceProvider = this.introspectService.getProvider(`${(0, string_helper_1.classify)(singularName)}Service`);
        if (!modelServiceProvider) {
            this.logger.error(`Model service provider not found for model: ${singularName}`);
            return null;
        }
        this.logger.debug(`Model service provider found for model: ${singularName}`);
        return modelServiceProvider.instance;
    }
};
exports.FixturesService = FixturesService;
exports.FixturesService = FixturesService = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [solid_introspect_service_1.SolidIntrospectService,
        module_metadata_helper_service_1.ModuleMetadataHelperService])
], FixturesService);
//# sourceMappingURL=fixtures.service.js.map