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

namespace DAL.Repositories
{
    public class TagTemplateRepository : ITagTemplateRepository
    {
        private readonly SqlSugarScope _db;

        public TagTemplateRepository(ISqlSugarProvider sqlSugarProvider)
        {
            _db = sqlSugarProvider.GetClient();
        }

        public async Task<TagTemplateEntity> CreateAsync(TagTemplateEntity entity)
        {
            await _db.Insertable(entity).ExecuteCommandAsync();
            return entity;
        }

        public async Task<TagTemplateEntity> UpdateAsync(TagTemplateEntity entity)
        {
            await _db.Updateable(entity).ExecuteCommandAsync();
            return entity;
        }

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

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

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

        public async Task<List<TagTemplateEntity>> GetAllAsync()
        {
            return await _db.Queryable<TagTemplateEntity>().ToListAsync();
        }
    }
}
