package pwc.taxtech.atms.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import pwc.taxtech.atms.dto.vatdto.InvoiceMappingDto;
import pwc.taxtech.atms.dto.vatdto.VoucherMappingDto;
import pwc.taxtech.atms.service.impl.IdentityServiceImpl;
import pwc.taxtech.atms.vat.service.impl.VoucherInvoiceMappingServiceImpl;

import java.util.List;
import java.util.UUID;

@RequestMapping(value = "api/v1/vimapping")
@RestController
public class VoucherInvoiceMappingController {
    private static Logger logger = LoggerFactory.getLogger(VoucherInvoiceMappingController.class);
    @Autowired
    private VoucherInvoiceMappingServiceImpl voucherInvoiceMappingService;
    @Autowired
    private IdentityServiceImpl identityService;

    @RequestMapping(value = "getvmappings/{period}", method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity GetInvoiceMappings(@PathVariable Integer period) {
        return ResponseEntity.ok().body(voucherInvoiceMappingService.getVourcherMappings(period));
    }

    @RequestMapping(value = "getimappings/{period}", method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity ValidDBVoucherMapping(@PathVariable Integer period) {
        return ResponseEntity.ok().body(voucherInvoiceMappingService.getInvoiceMappings(period));
    }

    @RequestMapping(value = "savevmappings/{period}/{IsAddition}", method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity SaveVourcherMappings(@RequestBody List<VoucherMappingDto> list, @PathVariable Integer period
            , @PathVariable Integer IsAddition) {
        logger.info("savevmappings/{period}/{IsAddition}");
        list.forEach(m -> {
            m.id = UUID.randomUUID().toString();
            m.CreatorId = identityService.getIdentityUser().getId();
        });
        return ResponseEntity.ok().body(voucherInvoiceMappingService.saveVourcherMappings(list, period, IsAddition == 2));
    }

    @RequestMapping(value = "saveimappings/{period}/{IsAddition}", method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity SaveInvoiceMappings(@RequestBody List<InvoiceMappingDto> list, @PathVariable Integer period
            , @PathVariable Integer IsAddition) {
        logger.info("savevmappings/{period}/{IsAddition}");
        list.forEach(m -> {
            m.id = UUID.randomUUID().toString();
            m.CreatorId = identityService.getIdentityUser().getId();
        });
        return ResponseEntity.ok().body(voucherInvoiceMappingService.SaveInvoiceMappings(list, period, IsAddition == 2));
    }
}