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

namespace DAL.Repositories
{
    public class TagInstanceRepository : ITagInstanceRepository
    {
        private readonly ISqlSugarProvider _provider;

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

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

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

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

        public async Task<List<TagInstanceEntity>> GetByWaybillAsync(string neutralWaybillNumber)
        {
            var db = _provider.GetClient();
            return await db.Queryable<TagInstanceEntity>()
                .Where(x => x.NeutralWaybillNumber == neutralWaybillNumber)
                .ToListAsync();
        }

        public async Task<List<TagInstanceEntity>> GetByTagTypeAsync(string tagType)
        {
            var db = _provider.GetClient();
            return await db.Queryable<TagInstanceEntity>()
                .Where(x => x.TagType == tagType)
                .ToListAsync();
        }
    }
}
