using LogCentral.Server.Data;
using LogCentral.Server.Models;
using Microsoft.EntityFrameworkCore;

namespace LogCentral.Server.Services;

public interface ISearchService
{
    Task<List<LogEntry>> SearchAsync(string query, int limit = 100);
}

public class SearchService : ISearchService
{
    private readonly LogCentralDbContext _context;

    public SearchService(LogCentralDbContext context)
    {
        _context = context;
    }

    public async Task<List<LogEntry>> SearchAsync(string query, int limit = 100)
    {
        return await _context.LogEntries
            .Where(l => EF.Functions.Like(l.Message, $"%{query}%") ||
                       EF.Functions.Like(l.Exception ?? "", $"%{query}%"))
            .OrderByDescending(l => l.Timestamp)
            .Take(limit)
            .ToListAsync();
    }
}