package com.ffii.fhsmsc.modules.pet_information.service;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ffii.core.exception.InternalServerErrorException;
import com.ffii.core.support.AbstractBaseEntityService;
import com.ffii.core.support.JdbcDao;
import com.ffii.core.utils.BeanUtils;
import com.ffii.fhsmsc.modules.pet_information.enity.pet_information;
import com.ffii.fhsmsc.modules.pet_information.enity.pet_informationRepository;
import com.ffii.fhsmsc.modules.pet_information.req.pet_informationReq;

import jakarta.validation.Valid;

@Service
public class pet_informationService extends AbstractBaseEntityService<pet_information, Long, pet_informationRepository>{
    private static final Logger logger = LoggerFactory.getLogger(pet_informationService.class);
    
    public pet_informationService(JdbcDao jdbcDao, pet_informationRepository repository) {
		super(jdbcDao, repository);
	}

    public List<Map<String, Object>> search(Map<String, Object> args) {
		logger.info("Search called with args: {}", args);
		StringBuilder sql = new StringBuilder("SELECT"
				+ " pi.id,"
				+ " pi.owner_id,"
				+ " pi.pet_name,"
				+ " pi.gender,"
				+ " pi.age_category,"
				+ " pi.birth_date,"
				+ " pi.breed,"
				+ " pi.weight_kg,"
				+ " pi.sterilization,"
				+ " pi.activity_level,"
				+ " pi.health_issues,"
				+ " pi.family_health_history,"
				+ " pi.health_goal,"
				+ " pi.type"
				+ " FROM pet_information pi"
				+ " WHERE pi.deleted = 0"  // 使用 0，因为是 tinyint(1)
				);
		if (args != null) {
			if (args.containsKey("owner_id")) {
				sql.append(" AND pi.owner_id = :owner_id");
				logger.info("Added owner_id filter: {}", args.get("owner_id"));
			}
		}
	
		sql.append(" ORDER BY pi.id");
		logger.info("Final SQL query: {}", sql.toString());
		
		List<Map<String, Object>> result = jdbcDao.queryForList(sql.toString(), args);
		logger.info("Query returned {} results", result.size());
		return result;
	}

    @Transactional(rollbackFor = Exception.class)
	public pet_information saveOrUpdate(@Valid pet_informationReq req) {
		pet_information instance;

		if (req.getId() != null && req.getId() > 0) {
			// 更新现有记录
			instance = find(req.getId()).orElseThrow(InternalServerErrorException::new);
			
			// 只更新非空字段
			if (req.getOwner_id() != null) {
				instance.setOwner_id(req.getOwner_id());
			}
			if (req.getGender() != null) {
				instance.setGender(req.getGender());
			}
			if (req.getPet_name() != null) {
				instance.setPet_name(req.getPet_name());
			}
			if (req.getBirth_date() != null) {
				instance.setBirth_date(req.getBirth_date());
			}
			if (req.getBreed() != null) {
				instance.setBreed(req.getBreed());
			}
			if (req.getWeight_kg() != null) {
				instance.setWeight_kg(req.getWeight_kg());
			}

			if (req.getHealth_goal() != null) {
				instance.setHealth_goal(req.getHealth_goal());
			}
			if (req.getType() != null) {
				instance.setType(req.getType());
			}
			if (req.getActivity_level() != null) {  
                instance.setActivity_level(req.getActivity_level());
            }
			if (req.getHealth_issues() != null) {
				instance.setHealth_issues(req.getHealth_issues());
			}
			if (req.getFamily_health_history() != null) {
				instance.setFamily_health_history(req.getFamily_health_history());
			}
			if (req.getSterilization() != null) {
				instance.setSterilization(req.getSterilization());
			}
			if (req.getAge_category() != null) {
				instance.setAge_category(req.getAge_category());
			}
			if (req.getGender() != null) {
				instance.setGender(req.getGender());
			}
			if (req.getPet_name() != null) {
				instance.setPet_name(req.getPet_name());
			}
			if (req.getBirth_date() != null) {
				instance.setBirth_date(req.getBirth_date());
			}
		} else {
			// 创建新记录
			instance = new pet_information();
			BeanUtils.copyProperties(req, instance);
		}
	
		saveAndFlush(instance);
		return instance;
	}
}
