package marketing.heyday.hartmann.fotodocumentation.core.service;

import java.util.List;
import java.util.Optional;

import org.apache.commons.lang3.StringUtils;

import jakarta.annotation.security.PermitAll;
import jakarta.ejb.LocalBean;
import jakarta.ejb.Stateless;
import jakarta.persistence.TypedQuery;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import marketing.heyday.hartmann.fotodocumentation.core.model.Customer;
import marketing.heyday.hartmann.fotodocumentation.core.model.Picture;
import marketing.heyday.hartmann.fotodocumentation.core.query.Param;
import marketing.heyday.hartmann.fotodocumentation.rest.vo.CustomerListValue;
import marketing.heyday.hartmann.fotodocumentation.rest.vo.CustomerPictureValue;
import marketing.heyday.hartmann.fotodocumentation.rest.vo.CustomerValue;

/**
 * 
 * <p>Copyright: Copyright (c) 2024</p>
 * <p>Company: heyday Marketing GmbH</p>
 * @author <a href="mailto:p.verboom@heyday.marketing">Patrick Verboom</a>
 * @version 1.0
 * 
 * created: 19 Jan 2026
 */
@Stateless
@LocalBean
@PermitAll
public class CustomerPictureService extends AbstractService {

	public boolean addCustomerPicture(CustomerPictureValue customerPictureValue) {
		Optional<Customer> customerOpt = queryService.callNamedQuerySingleResult(Customer.FIND_BY_NUMBER, new Param(Customer.PARAM_NUMBER, customerPictureValue.customerNumber()));
		Customer customer = customerOpt.orElseGet(() -> new Customer.Builder().customerNumber(customerPictureValue.customerNumber()).name(customerPictureValue.pharmacyName()).build());
		customer = entityManager.merge(customer);

		Picture picture = new Picture.Builder().customer(customer).username(customerPictureValue.username())
				.category(customerPictureValue.category())
				.comment(customerPictureValue.comment())
				.image(customerPictureValue.base64String())
				.pictureDate(customerPictureValue.date()).build();
		customer.getPictures().add(picture);

		entityManager.persist(picture);
		entityManager.flush();
		return true;
	}

	// query = search for name, number and date
	public List<CustomerListValue> getAll(String queryStr, String startsWith) {
		// FIXME: do query on date

		CriteriaBuilder builder = entityManager.getCriteriaBuilder();
		CriteriaQuery<Customer> criteriaQuery = builder.createQuery(Customer.class);
		Root<Customer> customerRoot = criteriaQuery.from(Customer.class);

		criteriaQuery = criteriaQuery.select(customerRoot);

		if (StringUtils.isNotBlank(startsWith)) {
			String param = startsWith.toLowerCase() + "%";
			criteriaQuery = criteriaQuery.where(builder.like(builder.lower(customerRoot.get("name")), param));
		}

		if (StringUtils.isNotBlank(queryStr)) {
			String param = "%" + StringUtils.trimToEmpty(queryStr).toLowerCase() + "%";
			var predicateName = builder.like(builder.lower(customerRoot.get("name")), param);
			var predicateNr = builder.like(builder.lower(customerRoot.get("customerNumber")), param);

			var predicate = builder.or(predicateName, predicateNr);
			criteriaQuery = criteriaQuery.where(predicate);
		}

		TypedQuery<Customer> typedQuery = entityManager.createQuery(criteriaQuery);
		List<Customer> customers = typedQuery.getResultList();
		customers.forEach(c -> c.getPictures().size());
		return customers.parallelStream().map(CustomerListValue::builder).toList();
	}

	public CustomerValue get(Long id) {
		Customer customer = entityManager.find(Customer.class, id);
		if (customer == null) {
			return null;
		}

		return CustomerValue.builder(customer);
	}
}
