package enea.lecservice.controllers;

import enea.lecservice.entities.Page;
import enea.lecservice.models.Configuration;
import enea.lecservice.models.dto.PageFilter;
import enea.lecservice.models.dto.ServiceResponse;
import enea.lecservice.security.IUserInfoService;
import enea.lecservice.services.PageService;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

import static org.springframework.http.HttpHeaders.CONTENT_LANGUAGE;

@RestController
@RequiredArgsConstructor
public class PageController {
    @NonNull
    protected PageService pageService;
    @NonNull
    protected IUserInfoService userInfoService;

    @GetMapping("/pages")
    public ServiceResponse<List<Page>> pages(@RequestHeader(value=CONTENT_LANGUAGE) String lang, Principal principal) {
        var userInfo = userInfoService.getUserInfo(principal);
        if (userInfo.isEmpty())
            return ServiceResponse.Error("No user specified.");
        ServiceResponse<List<Page>> pages = pageService.getForPermissions(lang, userInfo.get().getPermissions());
        return pages;
    }
    
    @GetMapping("/pagesInCommunity")
    public ServiceResponse<List<Page>> pagesInCommunity(
    		@RequestHeader(value=CONTENT_LANGUAGE) String lang, 
    		@RequestBody String communityid, 
    		Principal principal) {
        var userInfo = userInfoService.getUserInfo(principal);
        if (userInfo.isEmpty())
            return ServiceResponse.Error("No user specified.");
        ServiceResponse<List<Page>> pages = pageService.getForPermissions(lang, userInfo.get().getPermissions());
        return pages;
    }

    @GetMapping("/page-configuration/{slug}")
    public ServiceResponse<Configuration> pageConfiguration(@RequestHeader(value=CONTENT_LANGUAGE) String lang, @PathVariable String slug, Principal principal) {
        var userInfo = userInfoService.getUserInfo(principal);
        if (userInfo.isEmpty())
            return ServiceResponse.Error("No user specified.");
        return pageService.getPageConfiguration(lang, slug, userInfo.get().getPermissions());
    }

    @GetMapping("/admin/pages/all")
    public ServiceResponse<List<Page>> allPages(@RequestHeader(value=CONTENT_LANGUAGE) String lang, Principal principal) {
        var userInfo = userInfoService.getUserInfo(principal);
        if (userInfo.isEmpty())
            return ServiceResponse.Error("No user specified.");
        return pageService.getAllPages();
    }

    @GetMapping("/admin/pages/{id}")
    public ServiceResponse<Page> getPage(@RequestHeader(value=CONTENT_LANGUAGE) String lang, @PathVariable String id, Principal principal) {
        var userInfo = userInfoService.getUserInfo(principal);
        if (userInfo.isEmpty())
            return ServiceResponse.Error("No user specified.");
        var page = pageService.getPage(lang, id);
        return page;
        //return pageService.getPage(lang, id);
    }

    @PostMapping("/admin/pages/list")
    public ServiceResponse getPaginatedList(@RequestBody PageFilter filter) {
        return pageService.getPaginatedData(filter);
    }

    @PostMapping("/admin/pages/save")
    public ServiceResponse<Page> save(@RequestBody Page page) {
        return pageService.save(page);
    }

    @PostMapping("/admin/pages/delete")
    public ServiceResponse delete(@RequestBody Page page) {
        return pageService.delete(page.getId());
    }
}
