package com.atlassian.stash.internal.web;

import com.atlassian.bitbucket.nav.NavBuilder;
import com.atlassian.sal.api.features.DarkFeatureManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/home")
public class HomeController {

    private static final String DASHBOARD_DARK_FEATURE_KEY = "dark.feature.dashboard";
    private final DarkFeatureManager darkFeatureManager;
    private final NavBuilder navBuilder;

    @Autowired
    public HomeController(DarkFeatureManager darkFeatureManager, NavBuilder navBuilder) {
        this.darkFeatureManager = darkFeatureManager;
        this.navBuilder = navBuilder;
    }

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView home() {
        String target;
        if (darkFeatureManager.isFeatureEnabledForCurrentUser(DASHBOARD_DARK_FEATURE_KEY)) {
            target = navBuilder.dashboard().buildRelNoContext();
        } else {
            target = navBuilder.allProjects().buildRelNoContext();
        }
        return new ModelAndView("redirect:" + target);
    }
}
