package com.ctsb.my_product_management_svc.product;

import com.ctsb.my_product_management_svc.category.Category;
import com.ctsb.my_product_management_svc.category.CategoryRepository;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import java.time.LocalDateTime;
import java.util.List;

@Service
@AllArgsConstructor
public class ProductService {

    private final ProductRepository productRepository;
    private final CategoryRepository categoryRepository;
    private static final String NOT_FOUND = "Product with %s id is not found!";

    public List<Product> findAll(){
        return productRepository.findAll();
    }

    public Product findById(Long id){
        return productRepository.findById(id).orElseThrow(
                () -> new ResponseStatusException(HttpStatus.NOT_FOUND, String.format(NOT_FOUND,id))
        );
    }

    public Product create(ProductDto productDto){
        Category category = categoryRepository.findById(productDto.categoryId()).orElseThrow(
                () -> new ResponseStatusException(HttpStatus.NOT_FOUND,"Category not found!")
        );
        Product product = mapper(productDto);
        product.setCategory(category);
        product.setCreatedAt(LocalDateTime.now());
        product.setUpdatedAt(null);
        return productRepository.save(product);
    }

    public Product update(Long id, ProductDto productDto){
        Product existingProduct = productRepository.findById(id).orElseThrow(
                () -> new ResponseStatusException(HttpStatus.NOT_FOUND, String.format(NOT_FOUND,id))
        );
        Category category = categoryRepository.findById(productDto.categoryId()).orElseThrow(
                () -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Category not found!")
        );
        if(productDto.categoryId() != null){
            existingProduct.setCategory(category);
        }
        if(productDto.name() != null){
            existingProduct.setName(productDto.name());
        }
        if(productDto.description() != null){
            existingProduct.setDescription(productDto.description());
        }
        if(productDto.price() != null){
            existingProduct.setPrice(productDto.price());
        }
        if(productDto.stockCount() != null){
            existingProduct.setStockCount(productDto.stockCount());
        }
        if(productDto.status() != null){
            existingProduct.setStatus(productDto.status());
        }
        if(existingProduct.getCreatedAt() != null){
            existingProduct.setCreatedAt(LocalDateTime.now());
        }
        existingProduct.setUpdatedAt(LocalDateTime.now());
        return productRepository.save(existingProduct);
    }

    public void delete(Long id){
        Product product = productRepository.findById(id).orElseThrow(
                () -> new ResponseStatusException(HttpStatus.NOT_FOUND, String.format(NOT_FOUND,id))
        );
        productRepository.deleteById(id);
    }

    private Product mapper(ProductDto productDto){
        var product = new Product();
        product.setName(productDto.name());
        product.setDescription(productDto.description());
        product.setPrice(productDto.price());
        product.setStockCount(productDto.stockCount());
        product.setStatus(productDto.status());
        return product;
    }
}
