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 WindreamSearchToDepartmentRepository : BaseRepository<WindreamSearchToDepartment>
    {
        public WindreamSearchToDepartmentRepository(WebApiContext context) : base(context)
        {
        }

        public async Task<List<WindreamSearchToDepartment>> GetListByFilterAsync(DepartmentFilter filter, bool asNoTracking = true)
        {
            var items = this.RepositoryContext.Set<WindreamSearchToDepartment>().AsQueryable();

            if (filter.DepartmentId != null && filter.DepartmentId != 0)
            {
                items = items.Where(x => x.DepartmentId == filter.DepartmentId);
            }

            items = items.Where(x => x.IsActive);

            return asNoTracking ? await items.OrderBy(x => x.Seq).AsNoTracking().ToListAsync() : await items.OrderBy(x => x.Seq).ToListAsync();
        }

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