package com.weighttracker.service;

import com.weighttracker.model.User;
import com.weighttracker.model.WeightRecord;
import com.weighttracker.repository.WeightRecordRepository;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class WeightService {
    private final WeightRecordRepository repo;

    public WeightService(WeightRecordRepository r) {
        this.repo = r;
    }

    public WeightRecord save(WeightRecord w) {
        return repo.save(w);
    }

    public List<WeightRecord> last10(User u) {
        return repo.findTop10ByUserOrderByDateDesc(u);
    }

    public List<WeightRecord> findByUser(User u) {
        return repo.findByUserOrderByDateDesc(u);
    }

    public List<WeightRecord> recordsAfter(User u, LocalDateTime d) {
        return repo.findByUserAndDateAfterOrderByDateAsc(u, d);
    }

    public WeightRecord findById(Long id) {
        return repo.findById(id).orElseThrow();
    }

    public void deleteById(Long id) {
        repo.deleteById(id);
    }

    public List<WeightRecord> recordsBetween(User user, LocalDateTime start, LocalDateTime end) {
        return repo.findByUserAndDateBetweenOrderByDateAsc(user, start, end);
    }

    public void deleteByUser(User user) {
        repo.deleteAllByUser(user);
    }

}
