package com.example.demo.directions.service;

import java.util.List;
import java.util.stream.StreamSupport;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.demo.core.error.NotFoundException;
import com.example.demo.directions.model.DirectionsEntity;
import com.example.demo.directions.model.DirectionsGrouped;
import com.example.demo.directions.repository.DirectionsRepository;

@Service
public class DirectionsService {
    private final DirectionsRepository repository;

    private void checkName(String name) {
        if (repository.findByNameIgnoreCase(name).isPresent()) {
            throw new IllegalArgumentException(
                    String.format("Type with name %s is already exists", name));
        }
    }

    public DirectionsService(DirectionsRepository repository) {
        this.repository = repository;
    }

    @Transactional(readOnly = true)
    public List<DirectionsEntity> getAll(Long departmentId) {
        if (departmentId <= 0L) {
            return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
        } else {
            return repository.findByDepartmentId(departmentId);
        }
    }

    @Transactional(readOnly = true)
    public Page<DirectionsEntity> getAll(Long departmentId, int page, int size) {
        final Pageable pageRequest = PageRequest.of(page, size);
        if (departmentId <= 0L) {
            return repository.findAll(pageRequest);
        } else {
            return repository.findByDepartmentId(departmentId, pageRequest);
        }
    }

    @Transactional(readOnly = true)
    public DirectionsEntity get(Long id) {
        return repository.findById(id)
                .orElseThrow(() -> new NotFoundException(DirectionsEntity.class, id));
    }

    @Transactional
    public DirectionsEntity create(DirectionsEntity entity) {
        if (entity == null) {
            throw new IllegalArgumentException("Entity is null");
        }
        checkName(entity.getName());
        return repository.save(entity);
    }

    @Transactional
    public DirectionsEntity update(Long id, DirectionsEntity entity) {
        final DirectionsEntity existsEntity = get(id);
        checkName(entity.getName());
        existsEntity.setCode(entity.getCode());
        existsEntity.setName(entity.getName());
        existsEntity.setDepartment(entity.getDepartment());
        existsEntity.setThings(entity.getThings());
        return repository.save(existsEntity);
    }

    @Transactional
    public DirectionsEntity delete(Long id) {
        final DirectionsEntity existsEntity = get(id);
        repository.delete(existsEntity);
        return existsEntity;
    }

    @Transactional(readOnly = true)
    public Page<DirectionsGrouped> getAllWithDepartment(int page, int size, String nameS) {
        final Pageable pageRequest = PageRequest.of(page, size, Sort.by("id"));
        if(nameS == null || nameS.isEmpty()){
            return repository.findAllWithDepartment(pageRequest);
        }
        else{
            return repository.findAllWithDepartmentSearch(pageRequest, nameS);
        }
    }
}
