package com.example.myapplication.service;

import android.content.Context;

import com.example.myapplication.domain.Comment;

import java.util.List;

public class CommentService {
    public static List<Comment> listComments(Context ctx, long noteId) {
        com.example.myapplication.repository.CommentRepository repo = new com.example.myapplication.repository.CommentRepository(ctx);
        return repo.listByNoteId(noteId);
    }

    public static String validateContent(String content) {
        if (content == null) return "内容不能为空";
        String t = content.trim();
        if (t.isEmpty()) return "内容不能为空";
        if (t.length() > 500) return "内容过长";
        return null;
    }

    public static boolean addComment(Context ctx, String userId, long noteId, String content) {
        String err = validateContent(content);
        if (err != null) return false;
        if (userId == null || userId.trim().isEmpty()) return false;
        com.example.myapplication.repository.CommentRepository repo = new com.example.myapplication.repository.CommentRepository(ctx);
        long r = repo.add(userId, noteId, content.trim());
        return r != -1;
    }

    public static boolean deleteOwn(Context ctx, long id) {
        String userId = com.example.myapplication.service.AuthService.getCurrentUser(ctx);
        com.example.myapplication.repository.CommentRepository repo = new com.example.myapplication.repository.CommentRepository(ctx);
        return repo.delete(id, userId);
    }
}