using Microsoft.EntityFrameworkCore;

namespace ImageBoardServerApp.Data.Repository;

public static class ReportsRepository
{
    public static async Task<List<ReportData>> getReportsAsync()
    {
        await using var db = new AppDBContext();
        return await db.Reports
            .Include(report => report.ReportedPost)
            .Include(report => report.ReportedPost.Image)
            .Include(report => report.ReportedPost.Comments)
            .Include(report => report.ReportedComment)
            .Include(report => report.ReportedComment.Image)
            .Include(report => report.UserReported)
            .Include(report => report.UserReporter)
            .ToListAsync();
    }
    
    public static async Task<ReportData> getReportByIdAsync(int reportId)
    {
        await using var db = new AppDBContext();
        return await db.Reports.FirstOrDefaultAsync(report => report.ReportID == reportId);
    }

    public static async Task<int> createReportAsync(ReportData reportData)
    {
        await using var db = new AppDBContext();
        await db.Reports.AddAsync(reportData);
        if (await db.SaveChangesAsync() >= 1)
        {
            return reportData.ReportID;
        }

        return -1;
    }
    
    public static async Task<bool> updateReportAsync(ReportData reportData)
    {
        await using var db = new AppDBContext();
        db.Reports.Update(reportData);
        return await db.SaveChangesAsync() >= 1;
    }
    
    public static async Task<bool> deleteReportAsync(int reportId)
    {
        await using var db = new AppDBContext();
        ReportData reportData = await getReportByIdAsync(reportId);
        db.Remove(reportData);
        return await db.SaveChangesAsync() >= 1;
    }
}