package org.cmh.backend.CourseManagement.service;

import org.cmh.backend.CourseManagement.dto.CourseRequest;
import org.cmh.backend.CourseManagement.dto.SearchCourseRequest;
import org.cmh.backend.CourseManagement.model.Course;
import org.cmh.backend.CourseManagement.repository.CourseRepository;
import org.cmh.backend.CourseManagement.specification.CourseSpecification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import jakarta.persistence.EntityNotFoundException;
import java.util.List;

@Service
public class CourseService {

    @Autowired
    private CourseRepository courseRepository;

    public long getCourseCount() {
        return courseRepository.count();
    }

    public List<Course> getCoursesByRange(int start, int end) {
        Pageable pageable = PageRequest.of(start, end - start);
        return courseRepository.findAll(pageable).getContent();
    }

    public Course getCourseById(Long id) {
        return courseRepository.findById(id).orElseThrow(() -> new EntityNotFoundException("Course not found"));
    }

    public void createCourse(CourseRequest request) {
        Course course = new Course();
        course.setTitle(request.getTitle());
        course.setAuthor(request.getAuthor());
        course.setDescription(request.getDescription());
        course.setOrderNo(request.getOrderNo());
        course.setVideoPath(request.getVideoPath()); // 确保保存视频路径
        course.setImagePath(request.getImagePath()); // 确保保存图片路径
        courseRepository.save(course);
    }

    public void updateCourse(Long id, CourseRequest request) {
        Course course = courseRepository.findById(id).orElseThrow(() -> new EntityNotFoundException("Course not found"));
        course.setTitle(request.getTitle());
        course.setAuthor(request.getAuthor());
        course.setDescription(request.getDescription());
        course.setOrderNo(request.getOrderNo());
        course.setVideoPath(request.getVideoPath()); // 确保更新视频路径
        course.setImagePath(request.getImagePath()); // 确保更新图片路径
        courseRepository.save(course);
    }

    public void deleteCourse(Long id) {
        courseRepository.deleteById(id);
    }

    public List<Course> searchCourses(SearchCourseRequest request) {
        Pageable pageable = PageRequest.of(request.getStart(), request.getEnd() - request.getStart());
        return courseRepository.findAll(
                CourseSpecification.searchCourses(
                        request.getTitle(),
                        request.getAuthor(),
                        request.getDescription(),
                        request.getSortOrder()
                ),
                pageable
        ).getContent();
    }
    public long getCourseCountByCriteria(String title, String author, String description, String sortOrder) {
        return courseRepository.count(CourseSpecification.searchCourses(title, author, description, sortOrder));
    }
    public List<Course> sortCourses(String sortField, String sortDirection, int start, int end) {
        Pageable pageable = PageRequest.of(start, end - start);
        return courseRepository.findAll(
                CourseSpecification.sortCourses(sortField, sortDirection),
                pageable
        ).getContent();
    }
}