/*
 * Copyright 2018-2024 adorsys GmbH & Co KG
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version. This program is distributed in the hope that
 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see https://www.gnu.org/licenses/.
 *
 * This project is also available under a separate commercial license. You can
 * contact us at sales@adorsys.com.
 */

package de.adorsys.psd2.sandbox.admin.rest.server.controller;

import de.adorsys.ledgers.middleware.api.domain.general.BbanStructure;
import de.adorsys.ledgers.middleware.api.domain.um.UserTO;
import de.adorsys.ledgers.middleware.client.rest.DataRestClient;
import de.adorsys.ledgers.middleware.client.rest.UserMgmtRestClient;
import de.adorsys.ledgers.middleware.client.rest.UserMgmtStaffRestClient;
import de.adorsys.psd2.sandbox.admin.rest.api.domain.BankCodeStructure;
import de.adorsys.psd2.sandbox.admin.rest.api.domain.User;
import de.adorsys.psd2.sandbox.admin.rest.api.resource.AdminBaseRestApi;
import de.adorsys.psd2.sandbox.admin.rest.server.mapper.UserMapper;
import de.adorsys.psd2.sandbox.admin.rest.server.service.IbanGenerationService;
import de.adorsys.psd2.sandbox.cms.connector.api.service.CmsDbNativeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.iban4j.CountryCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Currency;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.util.Objects.requireNonNull;
import static org.springframework.http.HttpStatus.CREATED;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(AdminBaseRestApi.BASE_PATH)
public class AdminBaseController implements AdminBaseRestApi {
    private final UserMapper userMapper;
    private final UserMgmtStaffRestClient userMgmtStaffRestClient;
    private final UserMgmtRestClient userMgmtRestClient;
    private final DataRestClient dataRestClient;
    private final IbanGenerationService ibanGenerationService;
    private final CmsDbNativeService cmsDbNativeService;

    @Override
    public void login(String login, String pin) {
        //See corresponding Filter
    }

    @Override
    public ResponseEntity<Set<Currency>> getCurrencies() {
        return ResponseEntity.ok(dataRestClient.currencies().getBody());
    }

    @Override
    public ResponseEntity<Map<CountryCode, String>> getSupportedCountryCodes() {
        return ResponseEntity.ok(ibanGenerationService.getCountryCodes());
    }

    @Override
    public ResponseEntity<BankCodeStructure> getBankCodeStructure(String countryCode) {
        return ResponseEntity.ok(ibanGenerationService.getBankCodeStructure(CountryCode.valueOf(countryCode)));

    }

    @Override
    public ResponseEntity<String> getRandomTppId(String countryCode) {
        BankCodeStructure structure = new BankCodeStructure(CountryCode.getByCode(countryCode));
        BbanStructure bbanStructure = new BbanStructure();
        bbanStructure.setCountryPrefix(structure.getCountryCode().name());
        bbanStructure.setLength(structure.getLength());
        bbanStructure.setEntryType(BbanStructure.EntryType.valueOf(structure.getType().name().toUpperCase()));
        return ResponseEntity.ok(dataRestClient.branchId(bbanStructure).getBody());
    }

    @Override
    public ResponseEntity<Void> register(User user) {
        UserTO userTO = userMapper.toUserTO(user);
        userMgmtStaffRestClient.register(user.getId(), userTO);
        return ResponseEntity.status(CREATED).build();
    }

    @Override
    public ResponseEntity<Void> remove() {
        List<String> logins = userMgmtStaffRestClient.getBranchUserLogins().getBody();
        cmsDbNativeService.deleteConsentsByUserIds(logins);
        UserTO user = userMgmtRestClient.getUser().getBody();

        return ResponseEntity.ok(dataRestClient.branch(requireNonNull(user).getBranch()).getBody());
    }

    @Override
    public ResponseEntity<Void> transactions(String accountId) {
        return ResponseEntity.ok(dataRestClient.account(accountId).getBody());
    }

    @Override
    public ResponseEntity<Void> account(String accountId) {
        return ResponseEntity.ok(dataRestClient.depositAccount(accountId).getBody());
    }

    @Override
    public ResponseEntity<Void> user(String userId) {
        return ResponseEntity.ok(dataRestClient.user(userId).getBody());
    }

    @Override
    public ResponseEntity<Void> consumeTan(String tan) {
        log.info("\n***\nReceived message from CoreBanking: {} \n***", tan);
        return ResponseEntity.accepted().build();
    }
}
