/*-
 * ~~~~~~licensing~~~~~~
 * application-api
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.platform.integration.applications;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import ru.entaxy.platform.integration.applications.exceptions.ApplicationException;
import ru.entaxy.platform.integration.applications.exceptions.ProjectAlreadyExistsException;
import ru.entaxy.platform.integration.applications.exceptions.ProjectException;
import ru.entaxy.platform.integration.applications.exceptions.StorageException;
import ru.entaxy.platform.integration.applications.exceptions.StorageNotEnabledException;
import ru.entaxy.platform.integration.applications.exceptions.StorageNotFoundException;
import ru.entaxy.platform.integration.applications.exceptions.ProjectVersionException;

public interface ApplicationManager {

    // storages

    ApplicationStorage getStorage(String storageName);

    default List<ApplicationStorage> getActiveStorages() {
        return getStorages(true);
    };

    default List<ApplicationStorage> getStorages(boolean enabledOnly) {
        if (!enabledOnly)
            return getAllStorages();
        return getAllStorages().stream().filter(s -> s.isEnabled()).collect(Collectors.toList());
    };

    List<ApplicationStorage> getAllStorages();

    void addStorage(ApplicationStorage applicationStorage) throws StorageException;

    void removeStorage(ApplicationStorage applicationStorage) throws StorageException;

    // projects

    default EntaxyApplicationProject createProject(String storageName, String applicationName, String initialVersion)
            throws ProjectException, StorageException {
        ApplicationStorage storage = getStorage(storageName);
        if (storage == null)
            throw new StorageNotFoundException(storageName);
        if (!storage.isEnabled())
            throw new StorageNotEnabledException(storageName);
        EntaxyApplicationProject app = storage.getProject(applicationName);
        if (app != null)
            throw new ProjectAlreadyExistsException(applicationName);
        app = storage.createProject(applicationName);
        app.createVersion(initialVersion);
        return app;
    };

    default List<EntaxyApplicationProject> getProjects() {
        List<EntaxyApplicationProject> result = new ArrayList<>();
        for (ApplicationStorage storage : getActiveStorages()) {
            result.addAll(storage.getProjects());
        }
        return result;
    }

    default List<EntaxyApplicationProject> getProjects(String storageName)
            throws ProjectException, StorageException {
        ApplicationStorage storage = getStorage(storageName);
        if (storage == null)
            throw new StorageNotFoundException(storageName);
        return getProjects(storage);
    }

    default List<EntaxyApplicationProject> getProjects(ApplicationStorage storage)
            throws ProjectException, StorageException {
        if (!storage.isEnabled())
            throw new StorageNotEnabledException(storage.getName());
        return storage.getProjects();
    }

    void exportToFile(EntaxyApplicationProjectVersion version, String filePath) throws ProjectVersionException;

    void exportToRepository(EntaxyApplicationProjectVersion version, String repositoryName) throws ProjectVersionException;

    // applications

    default List<EntaxyApplication> getApplications() {
        List<EntaxyApplication> result = new ArrayList<>();
        for (ApplicationStorage storage : getActiveStorages()) {
            result.addAll(storage.getApplications());
        }
        return result;
    }

    default List<EntaxyApplication> getApplications(String storageName)
            throws ProjectException, StorageException {
        ApplicationStorage storage = getStorage(storageName);
        if (storage == null)
            throw new StorageNotFoundException(storageName);
        return getApplications(storage);
    }

    default List<EntaxyApplication> getApplications(ApplicationStorage storage)
            throws ProjectException, StorageException {
        if (!storage.isEnabled())
            throw new StorageNotEnabledException(storage.getName());
        return storage.getApplications();
    }


    EntaxyApplication importApplicationFromFile(String storageName, String filePath)
            throws ApplicationException, StorageException;

    EntaxyApplication importApplicationFromRepository(String storageName, String mavenUrl)
            throws ApplicationException, StorageException;


}
