package com.ikon.projectmanagement.controller;

import com.ikon.projectmanagement.api.GradeApi;
import com.ikon.projectmanagement.dto.request.GradeRequestDto;
import com.ikon.projectmanagement.dto.response.GradeResponseDto;
import com.ikon.projectmanagement.service.GradeService;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class GradeController implements GradeApi {

    private final GradeService gradeService;

    @Override
    public ResponseEntity<List<GradeResponseDto>> getAllGrades() {
        return ResponseEntity.ok(gradeService.getAllGrades());
    }

    @Override
    public ResponseEntity<GradeResponseDto> getGradeById(UUID id) {
        try {
            GradeResponseDto grade = gradeService.getGradeById(id);
            return ResponseEntity.ok(grade);
        } catch (RuntimeException e) {
            return ResponseEntity.notFound().build();
        }
    }

    @Override
    public ResponseEntity<GradeResponseDto> createGrade(GradeRequestDto dto) {
        try {
            GradeResponseDto createdGrade = gradeService.createGrade(dto);
            return ResponseEntity.status(HttpStatus.CREATED).body(createdGrade);
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().build();
        }
    }

    @Override
    public ResponseEntity<GradeResponseDto> updateGrade(UUID id, GradeRequestDto dto) {
        try {
            GradeResponseDto updatedGrade = gradeService.updateGrade(id, dto);
            return ResponseEntity.ok(updatedGrade);
        } catch (RuntimeException e) {
            return ResponseEntity.notFound().build();
        }
    }

    @Override
    public ResponseEntity<Void> deleteGrade(UUID id) {
        try {
            gradeService.deleteGrade(id);
            return ResponseEntity.noContent().build();
        } catch (RuntimeException e) {
            return ResponseEntity.notFound().build();
        }
    }
}
