package com.example.demo.entrysData.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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.demo.core.error.NotFoundException;
import com.example.demo.entrysData.model.EntrysDataEntity;
import com.example.demo.entrysData.model.EntrysDataGrouped;
import com.example.demo.entrysData.model.EntrysDataGroupedDepartment;
import com.example.demo.entrysData.repository.EntrysDataRepository;

@Service
public class EntrysDataService {
    private final EntrysDataRepository repository;

    public EntrysDataService(EntrysDataRepository repository) {
        this.repository = repository;
    }

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

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

    @Transactional(readOnly = true)
    public Page<EntrysDataEntity> 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 EntrysDataEntity get(Long id) {
        return repository.findById(id)
                .orElseThrow(() -> new NotFoundException(EntrysDataEntity.class, id));
    }

    @Transactional
    public EntrysDataEntity create(EntrysDataEntity entity) {
        if (entity == null) {
            throw new IllegalArgumentException("Entity is null");
        }
        checklogin(entity.getLogin());
        return repository.save(entity);
    }

    @Transactional
    public EntrysDataEntity update(Long id, EntrysDataEntity entity) {
        final EntrysDataEntity existsEntity = get(id);
        checklogin(entity.getLogin());
        existsEntity.setLogin(entity.getLogin());
        existsEntity.setPassword(entity.getPassword());
        existsEntity.setDepartment(entity.getDepartment());
        return repository.save(existsEntity);
    }

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

    @Transactional
    public Boolean AuthPassword(Long id, String pas) {
        final EntrysDataEntity existsEntity = get(id);
        String truePas = existsEntity.getPassword();
        if (truePas.equals(pas)) {
            return true;
        }
        return false;
    }

    @Transactional
    public EntrysDataEntity updatePassword(Long id, String newPas, String oldPas) {
        final EntrysDataEntity existsEntity = get(id);
        String truePas = existsEntity.getPassword();
        if (truePas.equals(oldPas) && !oldPas.equals(newPas)) {
            existsEntity.setPassword(newPas);
        }
        return repository.save(existsEntity);
    }

    @Transactional
    public List<EntrysDataGrouped> getCount() {
        return repository.getCountEntrysInDepartment();
    }

    @Transactional
    public Page<EntrysDataGroupedDepartment> getAllWithDepartment(int page, int size) {
        final Pageable pageRequest = PageRequest.of(page, size);
        return repository.findAllWithDepartment(pageRequest);
    }
}
