package com.iqser.red.persistence.service.v1.external.api.impl.controller;

import static com.iqser.red.service.persistence.management.v1.processor.roles.ActionRoles.READ_WATERMARK;
import static com.iqser.red.service.persistence.management.v1.processor.roles.ActionRoles.WRITE_WATERMARK;

import java.util.List;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.knecon.fforesight.keycloakcommons.security.KeycloakSecurity;
import com.iqser.red.service.persistence.management.v1.processor.service.WatermarkService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.AuditPersistenceService;
import com.knecon.fforesight.databasetenantcommons.providers.utils.MagicConverter;
import com.iqser.red.service.persistence.service.v1.api.external.resource.WatermarkResource;
import com.iqser.red.service.persistence.service.v1.api.shared.model.AuditCategory;
import com.iqser.red.service.persistence.service.v1.api.shared.model.WatermarkModel;
import com.iqser.red.service.persistence.service.v1.api.shared.model.audit.AuditRequest;
import com.iqser.red.service.persistence.service.v1.api.shared.model.common.JSONPrimitive;

import feign.FeignException;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
public class WatermarkController implements WatermarkResource {

    private final WatermarkService watermarkService;
    private final AuditPersistenceService auditPersistenceService;


    @Override
    @PreAuthorize("hasAuthority('" + WRITE_WATERMARK + "')")
    public WatermarkModel saveWatermark(@RequestBody WatermarkModel watermark) {

        String userId = KeycloakSecurity.getUserId();
        watermark.setCreatedBy(userId);
        WatermarkModel result = MagicConverter.convert(watermarkService.createOrUpdateWatermark(watermark), WatermarkModel.class);
        auditPersistenceService.audit(AuditRequest.builder()
                .userId(userId)
                .objectId(result.getDossierTemplateId())
                .category(AuditCategory.DOSSIER_TEMPLATE.name())
                .message("Watermark has been changed.")
                .build());
        return result;
    }


    @Override
    @PreAuthorize("hasAuthority('" + READ_WATERMARK + "')")
    public WatermarkModel getWatermark(@PathVariable(WATERMARK_ID_PARAMETER_NAME) long watermarkId) {

        return MagicConverter.convert(watermarkService.getWatermark(watermarkId), WatermarkModel.class);
    }


    @Override
    @PreAuthorize("hasAuthority('" + READ_WATERMARK + "')")
    public List<WatermarkModel> getWatermarksForDossierTemplateId(@RequestParam(DOSSIER_TEMPLATE_ID_PARAMETER_NAME) String dossierTemplateId) {

        return MagicConverter.convert(watermarkService.getWatermarksForDossierTemplateId(dossierTemplateId), WatermarkModel.class);
    }


    @Override
    @PreAuthorize("hasAuthority('" + WRITE_WATERMARK + "')")
    public void deleteWatermark(@PathVariable(WATERMARK_ID_PARAMETER_NAME) long watermarkId) {

        String dossierTemplateId = watermarkService.getWatermark(watermarkId).getDossierTemplateId();
        watermarkService.deleteWatermark(watermarkId);
        auditPersistenceService.audit(AuditRequest.builder()
                .userId(KeycloakSecurity.getUserId())
                .objectId(dossierTemplateId)
                .category(AuditCategory.DOSSIER_TEMPLATE.name())
                .message("Watermark has been deleted.")
                .build());

    }


    @PreAuthorize("hasAuthority('" + READ_WATERMARK + "')")
    public JSONPrimitive<Boolean> isWatermarkUsed(@RequestParam(WATERMARK_ID_PARAMETER_NAME) long watermarkId) {

        return JSONPrimitive.of(watermarkService.isWatermarkUsed(watermarkId));
    }

}
