using DAL.Models.Entities;
using DAL.Models.Filters;
using HRD.WebApi.Repositories;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DAL.Repositories
{
    public class DepartmentRepository : BaseRepository<Department>
    {
        public DepartmentRepository(WebApiContext context) : base(context)
        {
        }

        public async Task<List<Department>> GetDepartmentListAsync(DepartmentFullFilter filter, bool asNoTracking = true)
        {
            var items = this.RepositoryContext.Set<Department>().AsQueryable();

            if (!string.IsNullOrEmpty(filter.DepartmentName))
            {
                items = items.Where(x => EF.Functions.Like(x.DepartmentName, $"%{filter.DepartmentName}%"));
            }

            return asNoTracking ? await items.AsNoTracking().ToListAsync() : await items.ToListAsync();
        }

        public async Task<bool> ReplaceWindreamTiles(int srcDepartmentId, string trgDepartmentIds)
        {
            return await ExecStoredProcedureAsync("webapi.sp_TransferWindreamSettings", $"{srcDepartmentId}, null, \"{trgDepartmentIds}\"");
        }
    }
}