package com.petshop.backend.service;

import com.petshop.backend.dto.common.BulkDeleteRequest;
import com.petshop.backend.dto.supplier.SupplierRequest;
import com.petshop.backend.dto.supplier.SupplierResponse;
import com.petshop.backend.entity.Supplier;
import com.petshop.backend.exception.ResourceNotFoundException;
import com.petshop.backend.repository.SupplierRepository;
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 SupplierService {

    private final SupplierRepository supplierRepository;

    public SupplierService(SupplierRepository supplierRepository) {
        this.supplierRepository = supplierRepository;
    }

    public Page<SupplierResponse> getAllSuppliers(String query, Pageable pageable) {
        Page<Supplier> suppliers;
        if (query != null && !query.trim().isEmpty()) {
            suppliers = supplierRepository.searchSuppliers(query, pageable);
        } else {
            suppliers = supplierRepository.findAll(pageable);
        }
        return suppliers.map(this::mapToResponse);
    }

    public SupplierResponse getSupplierById(Long id) {
        Supplier supplier = supplierRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("Supplier not found with id: " + id));
        return mapToResponse(supplier);
    }

    @Transactional
    public SupplierResponse createSupplier(SupplierRequest request) {
        Supplier supplier = new Supplier();
        supplier.setSupCompany(request.getSupCompany());
        supplier.setSupContactFirstName(request.getSupContactFirstName());
        supplier.setSupContactLastName(request.getSupContactLastName());
        supplier.setSupEmail(request.getSupEmail());
        supplier.setSupPhone(request.getSupPhone());

        supplier = supplierRepository.save(supplier);
        return mapToResponse(supplier);
    }

    @Transactional
    public SupplierResponse updateSupplier(Long id, SupplierRequest request) {
        Supplier supplier = supplierRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("Supplier not found with id: " + id));

        supplier.setSupCompany(request.getSupCompany());
        supplier.setSupContactFirstName(request.getSupContactFirstName());
        supplier.setSupContactLastName(request.getSupContactLastName());
        supplier.setSupEmail(request.getSupEmail());
        supplier.setSupPhone(request.getSupPhone());

        supplier = supplierRepository.save(supplier);
        return mapToResponse(supplier);
    }

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

    @Transactional
    public void bulkDeleteSuppliers(BulkDeleteRequest request) {
        supplierRepository.deleteAllById(request.getIds());
    }

    private SupplierResponse mapToResponse(Supplier supplier) {
        return new SupplierResponse(
            supplier.getSupId(),
            supplier.getSupCompany(),
            supplier.getSupContactFirstName(),
            supplier.getSupContactLastName(),
            supplier.getSupEmail(),
            supplier.getSupPhone(),
            supplier.getCreatedAt(),
            supplier.getUpdatedAt()
        );
    }
}
