package ru.vyatsu.qr_access_admin.unit.mapper;

import org.springframework.stereotype.Component;
import ru.vyatsu.qr_access_admin.unit.entity.UnitEntity;
import ru.vyatsu.qr_access_admin.unit.model.UnitModel;

import java.util.Collection;
import java.util.List;

@Component
public class UnitEntityUnitModelMapper {

    public static final String DEFAULT_CLIENT_SETTINGS = "{\"@class\":\"java.util.Collections$UnmodifiableMap\",\"settings.client.require-proof-key\":false,\"settings.client.require-authorization-consent\":false}";
    private static final String DEFAULT_TOKEN_SETTINGS = """
            {"@class":"java.util.Collections$UnmodifiableMap","settings.token.reuse-refresh-tokens":true,"settings.token.x509-certificate-bound-access-tokens":false,"settings.token.id-token-signature-algorithm":["org.springframework.security.oauth2.jose.jws.SignatureAlgorithm","RS256"],"settings.token.access-token-time-to-live":["java.time.Duration",31540000.000000000],"settings.token.access-token-format":{"@class":"org.springframework.security.oauth2.server.authorization.settings.OAuth2TokenFormat","value":"self-contained"},"settings.token.refresh-token-time-to-live":["java.time.Duration",3600.000000000],"settings.token.authorization-code-time-to-live":["java.time.Duration",300.000000000],"settings.token.device-code-time-to-live":["java.time.Duration",300.000000000]}
            """;

    public UnitEntity mapModelToEntity(UnitModel model) {
        UnitEntity entity = new UnitEntity();
        entity.setId(model.getId());
        entity.setClientId(model.getClientId());
        // TODO: Приделать PasswordEncoder когда заедет security
        entity.setClientSecret(model.getClientSecret());
        entity.setClientSecretExpiresAt(model.getClientSecretExpiresAt());
        entity.setClientName(model.getClientName());
        entity.setScopes("");
        // TODO: Использовать enum, когда заедет oauth2
        entity.setAuthorizationGrantTypes("client_credentials");
        // TODO: Использовать enum, когда заедет oauth2
        entity.setClientAuthenticationMethods("client_secret_post");
        entity.setClientSettings(DEFAULT_CLIENT_SETTINGS);
        entity.setTokenSettings(DEFAULT_TOKEN_SETTINGS);
        entity.setRedirectUris("");
        entity.setPostLogoutRedirectUris("");
        entity.setAdminEditable(true);
        entity.setPartnerId(model.getPartnerId());
        return entity;
    }

    public UnitModel mapEntityToModel(UnitEntity entity) {
        UnitModel unitModel = new UnitModel();
        unitModel.setId(entity.getId());
        unitModel.setClientId(entity.getClientId());
        unitModel.setClientSecret(entity.getClientSecret());
        unitModel.setClientName(entity.getClientName());
        unitModel.setClientSecretExpiresAt(entity.getClientSecretExpiresAt());
        unitModel.setAdminEditable(entity.getAdminEditable());
        unitModel.setPartnerId(entity.getPartnerId());
        return unitModel;
    }

    public List<UnitModel> mapEntityToModel(Collection<UnitEntity> entities) {
        return entities.stream()
                .map(this::mapEntityToModel)
                .toList();
    }
}
