package com.petshop.backend.service;

import com.petshop.backend.dto.common.BulkDeleteRequest;
import com.petshop.backend.dto.store.StoreRequest;
import com.petshop.backend.dto.store.StoreResponse;
import com.petshop.backend.entity.StoreLocation;
import com.petshop.backend.exception.ResourceNotFoundException;
import com.petshop.backend.repository.StoreRepository;
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 StoreService {

    private final StoreRepository storeRepository;

    public StoreService(StoreRepository storeRepository) {
        this.storeRepository = storeRepository;
    }

    public Page<StoreResponse> getAllStores(String query, Pageable pageable) {
        Page<StoreLocation> stores;
        if (query != null && !query.trim().isEmpty()) {
            stores = storeRepository.searchStores(query, pageable);
        } else {
            stores = storeRepository.findAll(pageable);
        }
        return stores.map(this::mapToResponse);
    }

    public StoreResponse getStoreById(Long id) {
        StoreLocation store = storeRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("Store not found with id: " + id));
        return mapToResponse(store);
    }

    @Transactional
    public StoreResponse createStore(StoreRequest request) {
        StoreLocation store = new StoreLocation();
        store.setStoreName(request.getStoreName());
        store.setAddress(request.getAddress());
        store.setPhone(request.getPhone());
        store.setEmail(request.getEmail());
        store.setImageUrl(request.getImageUrl());

        store = storeRepository.save(store);
        return mapToResponse(store);
    }

    @Transactional
    public StoreResponse updateStore(Long id, StoreRequest request) {
        StoreLocation store = storeRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("Store not found with id: " + id));

        store.setStoreName(request.getStoreName());
        store.setAddress(request.getAddress());
        store.setPhone(request.getPhone());
        store.setEmail(request.getEmail());
        store.setImageUrl(request.getImageUrl());

        store = storeRepository.save(store);
        return mapToResponse(store);
    }

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

    @Transactional
    public void bulkDeleteStores(BulkDeleteRequest request) {
        storeRepository.deleteAllById(request.getIds());
    }

    private StoreResponse mapToResponse(StoreLocation store) {
        return new StoreResponse(
            store.getStoreId(),
            store.getStoreName(),
            store.getAddress(),
            store.getPhone(),
            store.getEmail(),
            store.getImageUrl(),
            store.getCreatedAt()
        );
    }
}
