package com.ecep.contract.ds.company.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ecep.contract.IEntityService;
import com.ecep.contract.QueryService;
import com.ecep.contract.SpringApp;
import com.ecep.contract.ds.company.repository.InvoiceRepository;
import com.ecep.contract.ds.company.model.Company;
import com.ecep.contract.ds.other.model.Invoice;
import com.ecep.contract.service.VoableService;
import com.ecep.contract.util.SpecificationUtils;
import com.ecep.contract.vo.InvoiceVo;
import com.fasterxml.jackson.databind.JsonNode;

@Lazy
@Service
@CacheConfig(cacheNames = "invoice")
public class InvoiceService implements IEntityService<Invoice>, QueryService<InvoiceVo>,
        VoableService<Invoice, InvoiceVo> {
    private static final Logger logger = LoggerFactory.getLogger(InvoiceService.class);

    @Autowired
    private InvoiceRepository repository;

    @Override
    public Invoice getById(Integer id) {
        return repository.findById(id).orElse(null);
    }

    @Cacheable(key = "#p0")
    @Override
    public InvoiceVo findById(Integer id) {
        return repository.findById(id).map(Invoice::toVo).orElse(null);
    }

    @Override
    public Specification<Invoice> getSpecification(String searchText) {
        if (!StringUtils.hasText(searchText)) {
            return null;
        }

        return (root, query, builder) -> {
            return builder.or(
                    builder.like(root.get("code"), "%" + searchText + "%"),
                    builder.like(root.get("description"), "%" + searchText + "%"));

        };
    }

    @Override
    public Page<Invoice> findAll(Specification<Invoice> spec, Pageable pageable) {
        return repository.findAll(spec, pageable);
    }

    @CacheEvict(key = "#p0.id")
    @Override
    public void delete(Invoice entity) {
        repository.delete(entity);
    }

    public List<Invoice> findAll(Specification<Invoice> spec, Sort by) {
        return repository.findAll(spec, by);
    }

    public Invoice findByCode(String invoiceNumber) {
        return repository.findByCode(invoiceNumber);
    }

    @CacheEvict(key = "#p0.id")
    @Override
    public Invoice save(Invoice invoice) {
        return repository.save(invoice);
    }

    @Override
    public Page<InvoiceVo> findAll(JsonNode paramsNode, Pageable pageable) {
        Specification<Invoice> spec = null;
        if (paramsNode.has("searchText")) {
            spec = getSpecification(paramsNode.get("searchText").asText());
        }

        // field
        spec = SpecificationUtils.andParam(spec, paramsNode, "company");
        return findAll(spec, pageable).map(Invoice::toVo);
    }

    @Override
    public void updateByVo(Invoice model, InvoiceVo vo) {
        if (model == null || vo == null) {
            throw new IllegalArgumentException("Model or VO cannot be null");
        }
        model.setCode(vo.getCode());
        if (vo.getCompanyId() == null) {
            model.setCompany(null);
        } else {
            Company company = SpringApp.getBean(CompanyService.class).getById(vo.getCompanyId());
            model.setCompany(company);
        }
        model.setInvoiceDate(vo.getInvoiceDate());
        model.setDescription(vo.getDescription());
        
    }
}
