package com.ikon.projectmanagement.service.impl;

import com.ikon.projectmanagement.dto.request.GradeRequestDto;
import com.ikon.projectmanagement.dto.response.GradeResponseDto;
import com.ikon.projectmanagement.entity.Grade;
import com.ikon.projectmanagement.mapper.GradeMapper;
import com.ikon.projectmanagement.repository.GradeRepository;
import com.ikon.projectmanagement.service.GradeService;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class GradeServiceImpl implements GradeService {

    private final GradeRepository gradeRepository;
    private final GradeMapper gradeMapper;

    @Override
    public List<GradeResponseDto> getAllGrades() {
        return gradeRepository.findAll().stream()
                .map(gradeMapper::toResponseDto)
                .collect(Collectors.toList());
    }

    @Override
    public GradeResponseDto getGradeById(UUID id) {
        Grade grade = gradeRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Grade not found with ID: " + id));
        return gradeMapper.toResponseDto(grade);
    }

    @Override
    public GradeResponseDto createGrade(GradeRequestDto dto) {
        Grade grade = gradeMapper.toEntity(dto);
        Grade savedGrade = gradeRepository.save(grade);
        return gradeMapper.toResponseDto(savedGrade);
    }

    @Override
    public GradeResponseDto updateGrade(UUID id, GradeRequestDto dto) {
        Grade grade = gradeRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Grade not found with ID: " + id));
        gradeMapper.updateEntity(grade, dto);
        Grade updatedGrade = gradeRepository.save(grade);
        return gradeMapper.toResponseDto(updatedGrade);
    }

    @Override
    public void deleteGrade(UUID id) {
        Grade grade = gradeRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Grade not found with ID: " + id));
        gradeRepository.delete(grade);
    }
}
