/**
 * Copyright (c) 2025 SoftCorporation LLC. All rights reserved.
 *
 * The Software License, Version 1.0
 *
 * SoftCorporation LLC. grants you ("Licensee") a non-exclusive, royalty free,
 * license to use, modify and redistribute this software in source and binary
 * code form, provided that the following conditions are met:
 *
 * 1. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        SoftCorporation LLC. (http://www.softcorporation.com)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 2. This software and SoftCorporation name must not be used to
 *    promote products derived from the software without prior
 *    written permission. For written permission, please contact
 *    info@softcorporation.com.
 *
 * This software is provided "AS IS," without a warranty of any kind.
 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
 * IN NO EVENT SHALL THE SOFTCORPORATION BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION).
 *
 */
package com.softcorporation.sandi.index.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.softcorporation.sandi.base.model.Client;
import com.softcorporation.sandi.base.model.Collection;
import com.softcorporation.sandi.base.service.ClientService;
import com.softcorporation.sandi.base.service.CollectionService;
import com.softcorporation.sandi.base.service.ConfigurationService;
import com.softcorporation.sandi.base.service.SynonymService;
import com.softcorporation.sandi.base.util.Constants;
import com.softcorporation.sandi.index.model.AdminResponse;
import com.softcorporation.sandi.index.model.ClientRequest;
import com.softcorporation.sandi.index.service.TypeAheadIndexerService;
import com.softcorporation.util.StringUtil;

@Controller
@RequestMapping("/admin")
public class AdminController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdminController.class);

    @Autowired
    private ClientService clientService;

    @Autowired
    private CollectionService collectionService;

    @Autowired
    private ConfigurationService configurationService;

    @Autowired
    private SynonymService synonymService;

    @Autowired
    private TypeAheadIndexerService typeAheadIndexerService;

    /**
     * Validates the request
     * @param requestId to validate
     * @return false if validation fails, true if valid
     */
    private boolean validateRequest(String requestId) {
        if (requestId == null || requestId.trim().isEmpty()) {
            LOGGER.warn("Admin request missing requestId");
            return false;
        }
        return true;
    }

    @GetMapping("/clients")
    @ResponseBody
    public ResponseEntity<List<Client>> listClients(@RequestParam String requestId) {
        if (!validateRequest(requestId)) {
        	return ResponseEntity.badRequest().build();
        }
        LOGGER.debug("[{}] Received get all clients request", requestId);
        
        try {
            List<Client> clients = clientService.listClients();
            LOGGER.debug("[{}] Returning {} clients via admin API", requestId, clients.size());            
            return ResponseEntity.ok(clients);
        } catch (Exception e) {
            LOGGER.error("[{}] Failed to retrieve clients via admin interface: error={}", 
            		requestId, e.getMessage(), e);
            return ResponseEntity.internalServerError().build();
        }
    }

    @GetMapping("/clients/{clientId}")
    @ResponseBody
    public ResponseEntity<Client> getClient(@PathVariable String clientId, @RequestParam String requestId) {
        if (!validateRequest(requestId)) {
        	return ResponseEntity.badRequest().build();
        }
        LOGGER.debug("[{}] Received get client request: clientId={}", requestId, clientId);
        
        try {
            Client client = clientService.getClient(clientId);
            if (client != null) {
                LOGGER.debug("[{}] Client found via admin API: clientId={}, name='{}'", 
                		requestId, clientId, client.getName());
                return ResponseEntity.ok(client);
            } else {
                LOGGER.warn("[{}] Client not found via admin API: clientId={}", requestId, clientId);
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            LOGGER.error("[{}] Failed to retrieve client via admin interface: clientId={}, error={}", 
            		requestId, clientId, e.getMessage(), e);
            return ResponseEntity.internalServerError().build();
        }

    }

    @PostMapping("/clients")
    @ResponseBody
    public ResponseEntity<AdminResponse> saveClient(@RequestBody ClientRequest clientRequest, @RequestParam String requestId) {
        if (!validateRequest(requestId)) {
        	return ResponseEntity.badRequest().build();
        }
        LOGGER.debug("[{}] Received save client request: clientId={}, name='{}', collection='{}', active={}", 
        		requestId, clientRequest.getClientId(), clientRequest.getName(), 
        		clientRequest.getCollection(), clientRequest.isActive());
        
    	// Validate client
        Client client = clientRequest.getClient();
        String validationError = validateClient(client);
        if (validationError != null) {
        	AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_ERROR, validationError);
            return ResponseEntity.badRequest().body(adminResponse);
        }
        boolean createCollection = false;
    	if (clientRequest.isCreateCollection() != null && clientRequest.isCreateCollection()) {
    		createCollection = true;
    		if (client.getCollection() == null) {
                LOGGER.warn("[{}] Requested to create collection but no collection specified", requestId);
            	AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_ERROR,
            			"Requested to create collection but no collection specified");
                return ResponseEntity.badRequest().body(adminResponse);
    		}
    	}
    	
        try {
        	if (createCollection) {
                clientService.saveClient(client, clientRequest.getConfiguration());
        	} else {
                clientService.saveClient(client);
        	}            
            LOGGER.info("[{}] Client saved successfully via admin interface: clientId={}, name='{}'", 
            		requestId, client.getClientId(), client.getName());

            AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_SUCCESS, "Client saved successfully");
            return ResponseEntity.ok().body(adminResponse);
        } catch (Exception e) {
            LOGGER.error("[{}] Failed to save client via admin interface: clientId={}, error={}", 
            		requestId, client.getClientId(), e.getMessage(), e);
        	AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_ERROR, "Failed to save client: " + e.getMessage());
            return ResponseEntity.internalServerError().body(adminResponse);
        }
    }

    @DeleteMapping("/clients/{clientId}")
    @ResponseBody
    public ResponseEntity<AdminResponse> deleteClient(@PathVariable String clientId, @RequestParam String requestId) {
        if (!validateRequest(requestId)) {
        	return ResponseEntity.badRequest().build();
        }
        LOGGER.debug("[{}] Received delete client request: clientId={}", requestId, clientId);
        
        try {
            boolean deleted = clientService.deleteClient(clientId);
            if (deleted) {
                LOGGER.info("[{}] Client deleted successfully via admin interface: clientId={}", 
                		requestId, clientId);
                AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_SUCCESS, "Client deleted successfully");
                return ResponseEntity.ok().body(adminResponse);
            } else {
                LOGGER.warn("[{}] Client not found for deletion via admin interface: clientId={}",
                		requestId, clientId);
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            LOGGER.error("[{}] Failed to delete client: clientId={}, error={}", 
            		requestId, clientId, e.getMessage(), e);
            AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_ERROR, "Failed to delete client. " + e.getMessage());
            return ResponseEntity.internalServerError().body(adminResponse);
        }
    }

    @GetMapping("/collections")
    @ResponseBody
    public ResponseEntity<List<String>> listCollections(@RequestParam String requestId) {
        if (!validateRequest(requestId)) {
        	return ResponseEntity.badRequest().build();
        }
        LOGGER.debug("[{}] Received get all collections request", requestId);
        
        try {
            List<String> collections = collectionService.listCollections();
            LOGGER.debug("[{}] Returning {} collections via admin API", requestId, collections.size());            
            return ResponseEntity.ok(collections);
        } catch (Exception e) {
            LOGGER.error("[{}] Failed to retrieve collections via admin interface: , error={}", 
            		requestId, e.getMessage(), e);
            return ResponseEntity.internalServerError().build();
        }
    }

    @GetMapping("/collections/{name}")
    @ResponseBody
    public ResponseEntity<Collection> getCollection(@PathVariable String name, @RequestParam String requestId) {
        if (!validateRequest(requestId)) {
        	return ResponseEntity.badRequest().build();
        }
        LOGGER.debug("[{}] Received get collection request: name={}", requestId, name);
        
        try {
            Collection collection = collectionService.getCollection(name);
            if (collection != null) {
                LOGGER.debug("[{}] Collection found via admin API: name={}, configuration='{}'", 
                		requestId, name, collection.getConfiguration());
                return ResponseEntity.ok(collection);
            } else {
                LOGGER.warn("[{}] Collection not found via admin API: name={}", requestId, name);
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            LOGGER.error("[{}] Failed to retrieve collection via admin interface: name={}, error={}", 
            		requestId, name, e.getMessage(), e);
            return ResponseEntity.internalServerError().build();
        }

    }

    @PostMapping("/collections")
    @ResponseBody
    public ResponseEntity<AdminResponse> createCollection(@RequestBody Collection collection, @RequestParam String requestId) {
        if (!validateRequest(requestId)) {
        	return ResponseEntity.badRequest().build();
        }        
        LOGGER.debug("[{}] Received create collection request: name='{}', configuration='{}'", 
        		requestId, collection.getName(), collection.getConfiguration());
        
        try {
            collectionService.createCollection(collection);
            
            LOGGER.info("[{}] Collection created successfully via admin interface: name='{}', configuration='{}', ", 
            		requestId, collection.getName(), collection.getConfiguration());
            AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_SUCCESS, "Collection created successfully");
            return ResponseEntity.ok().body(adminResponse);
        } catch (Exception e) {
            LOGGER.error("[{}] Failed to create collection via admin interface: name={}, error={}", 
            		requestId, collection.getName(), e.getMessage(), e);
            AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_ERROR, "Failed to create collection: " + e.getMessage());
            return ResponseEntity.internalServerError().body(adminResponse);
        }
    }

    @PutMapping("/collections/{name}")
    @ResponseBody
    public ResponseEntity<AdminResponse> updateCollection(@PathVariable String name, @RequestBody Collection collection, @RequestParam String requestId) {
        if (!validateRequest(requestId)) {
        	return ResponseEntity.badRequest().build();
        }
        LOGGER.debug("[{}] Received update collection request: name='{}', configuration='{}'",
        		requestId, name, collection.getConfiguration());
        if (collection.getName() == null) {
        	collection.setName(name);
        }        
        if (!name.equals(collection.getName())) {
                AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_ERROR, "Collection name is not matching");
                return ResponseEntity.badRequest().body(adminResponse);
        }        

        try {
            // Check if collection exists
            if (!collectionService.hasCollection(name)) {
                LOGGER.warn("[{}] Collection not found for update: collection={}", requestId, name);
                AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_ERROR, "Collection not found");
                return ResponseEntity.badRequest().body(adminResponse);
            }

            collectionService.updateCollection(collection);

            LOGGER.info("[{}] Collection updated successfully via admin interface: name='{}', configuration='{}'",
            		requestId, collection.getName(), collection.getConfiguration());
            AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_SUCCESS, "Collection updated successfully");
            return ResponseEntity.ok().body(adminResponse);
        } catch (Exception e) {
            LOGGER.error("[{}] Failed to update collection via admin interface: name={}, error={}",
            		requestId, name, e.getMessage(), e);
            AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_ERROR, "Failed to update collection: " + e.getMessage());
            return ResponseEntity.internalServerError().body(adminResponse);
        }
    }

    @DeleteMapping("/collections/{name}")
    @ResponseBody
    public ResponseEntity<AdminResponse> deleteCollection(@PathVariable String name, @RequestParam String requestId) {
        if (!validateRequest(requestId)) {
        	return ResponseEntity.badRequest().build();
        }
        LOGGER.debug("[{}] Received delete collection request: collection={}", requestId, name);

        try {
            if (!collectionService.hasCollection(name)) {
                LOGGER.warn("[{}] Collection not found for deletion: collection={}",
                		requestId, name);
                return ResponseEntity.notFound().build();
            }

            boolean deleted = collectionService.deleteCollection(name);
            if (deleted) {
                LOGGER.info("[{}] Collection deleted successfully via admin interface: collection={}",
                		requestId, name);
                AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_SUCCESS, "Collection deleted successfully");
                return ResponseEntity.ok().body(adminResponse);
            } else {
                LOGGER.warn("[{}] Collection {} not found for deletion", requestId, name);
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            LOGGER.error("[{}] Failed to delete collection via admin interface: collection={}, error={}",
            		requestId, name, e.getMessage(), e);
            AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_ERROR, "Failed to delete collection. " + e.getMessage());
            return ResponseEntity.internalServerError().body(adminResponse);
        }
    }

    @GetMapping("/configurations")
    @ResponseBody
    public ResponseEntity<List<String>> listConfigurations(@RequestParam String requestId) {
        if (!validateRequest(requestId)) {
        	return ResponseEntity.badRequest().build();
        }
        LOGGER.debug("[{}] Received get all configurations request", requestId);
        
        try {
            List<String> configurations = configurationService.listConfigurations();
            LOGGER.debug("[{}] Returning {} configurations via admin API", requestId, configurations.size());            
            return ResponseEntity.ok(configurations);
        } catch (Exception e) {
            LOGGER.error("[{}] Failed to retrieve configurations via admin interface: , error={}", 
            		requestId, e.getMessage(), e);
            return ResponseEntity.internalServerError().build();
        }
    }

    @PostMapping("/configurations/{name}")
    @ResponseBody
    public ResponseEntity<AdminResponse> uploadConfiguration(@PathVariable String name, @RequestParam String requestId) {
        if (!validateRequest(requestId)) {
        	return ResponseEntity.badRequest().build();
        }
        LOGGER.debug("[{}] Received upload configuration request: name='{}'", requestId, name);
        
        try {
        	configurationService.uploadConfiguration(name);
            
            LOGGER.info("[{}] Configuration uploaded successfully via admin interface: name='{}'", 
            		requestId, name);
            AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_SUCCESS, "Configuration uploaded successfully");
            return ResponseEntity.ok().body(adminResponse);
        } catch (Exception e) {
            LOGGER.error("[{}] Failed to upload configuration via admin interface: name={}, error={}", 
            		requestId, name, e.getMessage(), e);
            AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_ERROR, "Failed to upload configuration: " + e.getMessage());
            return ResponseEntity.internalServerError().body(adminResponse);
        }
    }

    @DeleteMapping("/configurations/{name}")
    @ResponseBody
    public ResponseEntity<AdminResponse> deleteConfiguration(@PathVariable String name, @RequestParam String requestId) {
        if (!validateRequest(requestId)) {
        	return ResponseEntity.badRequest().build();
        }
        LOGGER.debug("[{}] Received delete configuration request: configuration={}", requestId, name);
        
        try {
            if (!configurationService.hasConfiguration(name)) {
                LOGGER.warn("[{}] Configuration not found for deletion via admin interface: configuration={}", 
                		requestId, name);
                return ResponseEntity.notFound().build();
            }

            boolean deleted = configurationService.deleteConfiguration(name);
            if (deleted) {
                LOGGER.info("[{}] Configuration deleted successfully via admin interface: configuration={}", 
                		requestId, name);
                AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_SUCCESS, "Configuration deleted successfully");
                return ResponseEntity.ok().body(adminResponse);
            } else {
                LOGGER.warn("[{}] Configuration {} not found for deletion",	requestId, name);
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            LOGGER.error("[{}] Failed to delete configuration via admin interface: configuration={}, error={}", 
            		requestId, name, e.getMessage(), e);
            AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_ERROR, "Failed to delete configuration: " + e.getMessage());
            return ResponseEntity.internalServerError().body(adminResponse);
        }
    }

    /**
     * List synonym sets
     * 
     * @param requestId
     * @return list of synonym sets
     */
    @GetMapping("/synonyms")
    @ResponseBody
    public ResponseEntity<List<String>> listSynonymSets(@RequestParam String requestId) {
        if (!validateRequest(requestId)) {
        	return ResponseEntity.badRequest().build();
        }
        LOGGER.debug("[{}] Received list all synonyms sets request", requestId);
        
        try {
            List<String> synonymSets = synonymService.listSynonymSets();
            LOGGER.debug("[{}] Returning {} synonym sets via admin API", requestId, synonymSets.size());            
            return ResponseEntity.ok(synonymSets);
        } catch (Exception e) {
            LOGGER.error("[{}] Failed to retrieve synonym sets via admin interface: , error={}", 
            		requestId, e.getMessage(), e);
            return ResponseEntity.internalServerError().build();
        }
    }

    @PostMapping("/synonyms/{name}")
    @ResponseBody
    public ResponseEntity<AdminResponse> uploadSynonyms(@PathVariable String name, 
    		@RequestParam String fileName, @RequestParam String language, @RequestParam String requestId) {
        if (!validateRequest(requestId)) {
        	return ResponseEntity.badRequest().build();
        }
        LOGGER.debug("[{}] Received upload synonyms request: name='{}'", requestId, name);
        
        try {
        	synonymService.uploadSynonyms(name, fileName, language);
            
            LOGGER.info("[{}] Synonyms uploaded successfully via admin interface: name='{}'", 
            		requestId, name);
            AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_SUCCESS, "Synonyms uploaded successfully");
            return ResponseEntity.ok().body(adminResponse);
        } catch (Exception e) {
            LOGGER.error("[{}] Failed to upload synonyms via admin interface: name={}, error={}", 
            		requestId, name, e.getMessage(), e);
            AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_ERROR, "Failed to upload synonyms: " + e.getMessage());
            return ResponseEntity.internalServerError().body(adminResponse);
        }
    }

    @DeleteMapping("/synonyms/{name}")
    @ResponseBody
    public ResponseEntity<AdminResponse> deleteSynonyms(@PathVariable String name, @RequestParam String requestId) {
        if (!validateRequest(requestId)) {
        	return ResponseEntity.badRequest().build();
        }
        LOGGER.debug("[{}] Received delete synonyms request: synonyms={}", requestId, name);
        
        try {
            boolean deleted = synonymService.deleteSynonyms(name);
            if (deleted) {
                LOGGER.info("[{}] Synonyms deleted successfully via admin interface: synonyms={}", 
                		requestId, name);
                AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_SUCCESS, "synonyms deleted successfully");
                return ResponseEntity.ok().body(adminResponse);
            } else {
                LOGGER.warn("[{}] Synonyms {} not found for deletion",	requestId, name);
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            LOGGER.error("[{}] Failed to delete synonyms via admin interface: configuration={}, error={}", 
            		requestId, name, e.getMessage(), e);
            AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_ERROR, "Failed to delete synonyms: " + e.getMessage());
            return ResponseEntity.internalServerError().body(adminResponse);
        }
    }

    @GetMapping("/typeahead/status")
    @ResponseBody
    public ResponseEntity<AdminResponse> getTypeAheadStatus(@RequestParam String requestId) {
        if (!validateRequest(requestId)) {
            return ResponseEntity.badRequest().build();
        }
        String status = typeAheadIndexerService.isRunning() ? "running" : "idle";
        AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_SUCCESS, status);
        return ResponseEntity.ok(adminResponse);
    }

    @PostMapping("/typeahead/start")
    @ResponseBody
    public ResponseEntity<AdminResponse> startTypeAheadIndexing(@RequestParam String requestId) {
        if (!validateRequest(requestId)) {
            return ResponseEntity.badRequest().build();
        }
        if (typeAheadIndexerService.isRunning()) {
            AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_ERROR, "Type-ahead indexing is already running");
            return ResponseEntity.status(409).body(adminResponse);
        }
        new Thread(() -> typeAheadIndexerService.indexAllTypeAhead(), "typeahead-indexing").start();
        LOGGER.info("[{}] Type-ahead indexing started manually", requestId);
        AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_SUCCESS, "Type-ahead indexing started");
        return ResponseEntity.ok(adminResponse);
    }

    @PostMapping("/typeahead/stop")
    @ResponseBody
    public ResponseEntity<AdminResponse> stopTypeAheadIndexing(@RequestParam String requestId) {
        if (!validateRequest(requestId)) {
            return ResponseEntity.badRequest().build();
        }
        if (!typeAheadIndexerService.isRunning()) {
            AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_ERROR, "Type-ahead indexing is not running");
            return ResponseEntity.status(409).body(adminResponse);
        }
        typeAheadIndexerService.stop();
        LOGGER.info("[{}] Type-ahead indexing stop requested", requestId);
        AdminResponse adminResponse = new AdminResponse(requestId, Constants.STATUS_SUCCESS, "Stop requested");
        return ResponseEntity.ok(adminResponse);
    }

    /**
     * Validates the Client
     * @param request the request to validate
     * @return IndexResponse with error if validation fails, null if valid
     */
    private String validateClient(Client client) {
        if (client.getClientId() == null || client.getClientId().trim().isEmpty()) {
            LOGGER.warn("Client is missing clientId");
            return "ClientId is required";
        }
        
        if (StringUtil.containsNonWordChars(client.getClientId())) {
            LOGGER.warn("ClientId has invalid characters");
            return "ClientId has invalid characters";
        }
        
        if (client.getName() == null || client.getName().trim().isEmpty()) {
            LOGGER.warn("Client is missing client name");
            return "Client name is required";
        }
        
        boolean emptyFields = true;
        if (client.getFieldsHigh() != null) {
        	String[] fields = client.getFieldsHigh();
        	if (fields.length == 0) {
                LOGGER.warn("Empty high relevancy fields list");
            } else {
            	emptyFields = false;
            }
        }
        
        if (client.getFieldsLow() != null) {
        	String[] fields = client.getFieldsLow();
        	if (fields.length == 0) {
                LOGGER.warn("Empty low relevancy fields list");
            } else {
            	emptyFields = false;
            }
        }
        
        if (client.getFieldsContent() != null) {
        	String[] fields = client.getFieldsContent();
        	if (fields.length == 0) {
                LOGGER.warn("Empty content fields list");
            } else {
            	emptyFields = false;
            }
        }
        
       	if (emptyFields) {
            LOGGER.warn("Empty fields list");
            return "Empoty fields list";
        }
        
        return null;
    }
    
}