using Lactose.Context;
using Lactose.Models;

namespace Lactose.Repositories;

public class TagRepository(LactoseDbContext context) : ITagRepository {
    public void Dispose() => context.Dispose();

    public List<Tag> Search(string searchQuery, int page = 0, int pageSize = 200) =>
        context.Tags.Where(t => EF.Functions.Like(t.Name, $"%{searchQuery}%"))
                      .OrderByDescending(t => t.Name)
                      .Skip((page) * pageSize)
                      .Take(pageSize)
                      .ToList();
    

    public IEnumerable<Tag> GetAll() => context.Tags;

    public Tag? Find(Guid id) => context.Tags.Find(id);

    public Tag? FindByName(string name) =>context.Tags.FirstOrDefault(t => t.Name == name);

    public List<Tag> GetChildren(Guid id) => context.Tags.Where(t=>t.ParentId==id).ToList();

    public void Delete(Tag tag) => context.Tags.Remove(tag);

    public void Insert(Tag tag) => context.Tags.Add(tag);

    public void Save() => context.SaveChanges();
}
