package com.petshop.backend.service;

import com.petshop.backend.dto.common.BulkDeleteRequest;
import com.petshop.backend.dto.service.ServiceRequest;
import com.petshop.backend.dto.service.ServiceResponse;
import com.petshop.backend.exception.ResourceNotFoundException;
import com.petshop.backend.repository.ServiceRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ServiceService {

    private final ServiceRepository serviceRepository;

    public ServiceService(ServiceRepository serviceRepository) {
        this.serviceRepository = serviceRepository;
    }

    @Transactional(readOnly = true)
    public Page<ServiceResponse> getAllServices(String query, String species, Pageable pageable) {
        String q = (query != null && !query.trim().isEmpty()) ? query.trim() : null;
        String sp = (species != null && !species.trim().isEmpty()) ? species.trim() : null;
        return serviceRepository.searchServices(q, sp, pageable).map(this::mapToResponse);
    }

    @Transactional(readOnly = true)
    public ServiceResponse getServiceById(Long id) {
        com.petshop.backend.entity.Service service = serviceRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("Service not found with id: " + id));
        return mapToResponse(service);
    }

    @Transactional
    public ServiceResponse createService(ServiceRequest request) {
        com.petshop.backend.entity.Service service = new com.petshop.backend.entity.Service();
        service.setServiceName(request.getServiceName());
        service.setServiceDesc(request.getServiceDesc());
        service.setServicePrice(request.getServicePrice());
        service.setServiceDuration(request.getServiceDuration());
        if (request.getSpecies() != null) {
            service.setSpecies(request.getSpecies());
        }

        service = serviceRepository.save(service);
        return mapToResponse(service);
    }

    @Transactional
    public ServiceResponse updateService(Long id, ServiceRequest request) {
        com.petshop.backend.entity.Service service = serviceRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("Service not found with id: " + id));

        service.setServiceName(request.getServiceName());
        service.setServiceDesc(request.getServiceDesc());
        service.setServicePrice(request.getServicePrice());
        service.setServiceDuration(request.getServiceDuration());
        if (request.getSpecies() != null) {
            service.setSpecies(request.getSpecies());
        }

        service = serviceRepository.save(service);
        return mapToResponse(service);
    }

    @Transactional
    public void deleteService(Long id) {
        if (!serviceRepository.existsById(id)) {
            throw new ResourceNotFoundException("Service not found with id: " + id);
        }
        serviceRepository.deleteById(id);
    }

    @Transactional
    public void bulkDeleteServices(BulkDeleteRequest request) {
        serviceRepository.deleteAllById(request.getIds());
    }

    private ServiceResponse mapToResponse(com.petshop.backend.entity.Service service) {
        return new ServiceResponse(
            service.getServiceId(),
            service.getServiceName(),
            service.getServiceDesc(),
            service.getServicePrice(),
            service.getServiceDuration(),
            service.getSpecies(),
            service.getCreatedAt(),
            service.getUpdatedAt()
        );
    }
}
