using System.Collections.Generic;
using System.Threading.Tasks;
using DAL.Interfaces;
using MDL.Models;
using DB.Database;

namespace DAL.Repositories
{
    public class TagTemplateRepository : ITagTemplateRepository
    {
        private readonly ISqlSugarProvider _provider;

        public TagTemplateRepository(ISqlSugarProvider sqlSugarProvider)
        {
            _provider = sqlSugarProvider;
        }

        public async Task<TagTemplateEntity> CreateAsync(TagTemplateEntity entity)
        {
            var db = _provider.GetClient();
            await db.Insertable(entity).ExecuteCommandAsync();
            return entity;
        }

        public async Task<TagTemplateEntity> UpdateAsync(TagTemplateEntity entity)
        {
            var db = _provider.GetClient();
            await db.Updateable(entity).ExecuteCommandAsync();
            return entity;
        }

        public async Task<bool> DeleteAsync(int id)
        {
            var db = _provider.GetClient();
            var result = await db.Deleteable<TagTemplateEntity>().Where(x => x.Id == id).ExecuteCommandAsync();
            return result > 0;
        }

        public async Task<TagTemplateEntity> GetByIdAsync(int id)
        {
            var db = _provider.GetClient();
            return await db.Queryable<TagTemplateEntity>().Where(x => x.Id == id).FirstAsync();
        }

        public async Task<TagTemplateEntity> GetByTagTypeAsync(string tagType)
        {
            var db = _provider.GetClient();
            return await db.Queryable<TagTemplateEntity>().Where(x => x.TagType == tagType && x.IsActive).FirstAsync();
        }

        public async Task<List<TagTemplateEntity>> GetAllAsync()
        {
            var db = _provider.GetClient();
            return await db.Queryable<TagTemplateEntity>().ToListAsync();
        }
    }
}
