package com.atlassian.bitbucket.internal.mirroring.upstream.rest;

import com.atlassian.bitbucket.AuthorisationException;
import com.atlassian.bitbucket.auth.AuthenticationContext;
import com.atlassian.bitbucket.i18n.I18nService;
import com.atlassian.bitbucket.internal.mirroring.rest.RestAnalyticsSettings;
import com.atlassian.bitbucket.internal.mirroring.upstream.InternalMirrorService;
import com.atlassian.bitbucket.license.LicenseService;
import com.atlassian.bitbucket.mirroring.upstream.MirrorServer;
import com.atlassian.bitbucket.plugin.OptionalPluginInformationProvider;
import com.atlassian.bitbucket.rest.util.ResponseFactory;
import com.atlassian.bitbucket.rest.util.RestUtils;
import com.atlassian.extras.api.bitbucket.BitbucketServerLicense;
import com.atlassian.plugins.rest.common.security.AnonymousAllowed;
import com.sun.jersey.spi.resource.Singleton;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@AnonymousAllowed
@Path("analyticsSettings")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({RestUtils.APPLICATION_JSON_UTF8})
@Singleton
public class AnalyticsRestResource {
    private final AuthenticationContext authenticationContext;
    private final I18nService i18nService;
    private final LicenseService licenseService;
    private final InternalMirrorService mirrorServerService;
    private final OptionalPluginInformationProvider pluginInformationProvider;

    public AnalyticsRestResource(AuthenticationContext authenticationContext, I18nService i18nService,
                                 LicenseService licenseService,
                                 InternalMirrorService mirrorServerService,
                                 OptionalPluginInformationProvider pluginInformationProvider) {
        this.authenticationContext = authenticationContext;
        this.i18nService = i18nService;
        this.licenseService = licenseService;
        this.mirrorServerService = mirrorServerService;
        this.pluginInformationProvider = pluginInformationProvider;
    }

    /**
     * @response.representation.200.mediaType application/json
     * @response.representation.200.example {@link RestAnalyticsSettings#RESPONSE_EXAMPLE}
     * @response.representation.200.doc The analytics settings from upstream
     */
    @GET
    public Response analyticsSettings() {
        MirrorServer mirror = mirrorServerService.getForUser(authenticationContext.getCurrentUser());
        if (mirror == null || !mirror.isEnabled()) {
            throw new AuthorisationException(
                    i18nService.createKeyedMessage("bitbucket.mirroring.authentication.analytics.permission.denied"));
        }
        boolean canCollectAnalytics = pluginInformationProvider.canCollectAnalytics();
        BitbucketServerLicense bitbucketServerLicense = licenseService.get();
        String sen = bitbucketServerLicense == null ? "" : bitbucketServerLicense.getSupportEntitlementNumber();

        return ResponseFactory.ok()
                .entity(new RestAnalyticsSettings(canCollectAnalytics, sen))
                .build();
    }
}