using VentaUniformes.Persistencia.Configurations;
using VentaUniformes.Application.Interfaces;
using VentaUniformes.Domain.Entities.User;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VentaUniformes.Domain.Entities.Customer;
using VentaUniformes.Domain.Entities.Sale;
using VentaUniformes.Domain.Enums;

namespace VentaUniformes.Persistencia.Services
{
    public class PersistenciaService : DbContext, IPersistenciaService
    {
        public PersistenciaService(DbContextOptions options): base(options) 
        {
        }
        public DbSet<UserEntity> User { get; set; }
        public DbSet<ProductEntity> Product { get; set; }
        public DbSet<SaleEntity> Sales { get; set; }
        public DbSet<SaleBillEntity> SaleBill { get; set; }
        public DbSet<SaleLineEntity> SalesLine { get; set; }



        #region user
        public async Task<bool> AddUser(UserEntity user)
        {
            User.Add(user);
            return await BoolAsync();
        }
        public async Task<bool> UpdateUser(UserEntity user)
        {
            User.Update(user);
            return await BoolAsync();
        }
        public async Task<bool> DeleteUser(int idUser)
        {
           var enty= await User.FirstOrDefaultAsync(x => x.Id == idUser);
            if (enty == null)
                return false;
            User.Remove(enty);
            return await BoolAsync();
        }
        public async Task<bool> UpdateNoNullUser(UserEntity user)
        {
            var enty = await User.FirstOrDefaultAsync(x => x.Id == user.Id);
            if (enty == null)
                return false;
            if(user.Password!=null)
                enty.Password= user.Password;

            if (user.UserName != null)
                enty.UserName = user.UserName;

            return await BoolAsync();
        }


        public async Task<List<UserEntity>> GetAllUser()
        {
            return await  User.ToListAsync();
        }


        public async Task<UserEntity> GetUserById(int idUser)
        {
            var enty = await User.FirstOrDefaultAsync(x => x.Id == idUser);
            return enty;
        }

        public async Task<UserEntity> GetUserByPassAndName(string pass, string name)
        {
            var enty = await User.FirstOrDefaultAsync(x => (x.Password == pass) && (x.UserName==name));
            return enty;
        }

        #endregion
        #region producto
        public async Task<List<ProductEntity>> GetAllProduct()
        {
            return await Product.ToListAsync();
        }


        public async Task<ProductEntity> GetProductById(int idProduct)
        {
            var enty = await Product.FirstOrDefaultAsync(x => x.Id == idProduct);
            return enty;
        }

        #endregion
        #region venta

        public async Task<List<SaleEntity>> GetSale(int top, string alumnoFilter )
        {
            var res=Sales.Where(s =>!s.IsDelete&&( String.IsNullOrEmpty(alumnoFilter) || s.Alumno.ToLower().Contains(alumnoFilter.ToLower())))
                .OrderByDescending(e => e.Id)
                .Take(top);
            if (res == null)
                return new List<SaleEntity>();
            return res.ToList();
        }

        public async Task<int> AddSale(SaleEntity e)
        {
            Sales.Add(e);
            if (await BoolAsync())
                return e.Id;
            else return -1;
        }
        public async Task<bool> DeleteSale(int idSale)
        {
            var sale= Sales.FirstOrDefault(e=>!e.IsDelete && e.Id==idSale );
            if(sale==null)
                return false;
            sale.IsDelete=true;

            return await BoolAsync();
        }
        public async Task<bool> AddSaleBill(SaleBillEntity e)
        {
            SaleBill.Add(e);
            return await BoolAsync();
        }
        public async Task<bool> AddLineSales(List<SaleLineEntity> e)
        {
            SalesLine.AddRange(e);
            return await BoolAsync();
        }
        #endregion

        private async Task<bool> BoolAsync()
        {
            return await SaveChangesAsync()>0;
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            EntityConfigration(modelBuilder);
        }
        private void EntityConfigration(ModelBuilder model)
        {
            //configuras entidades relaccionadas con bbdd
            new UserConfiguration(model.Entity<UserEntity>());
            new ProductConfiguration(model.Entity<ProductEntity>());
            new SaleLineConfiguration(model.Entity<SaleLineEntity>());
            new SaleConfiguration(model.Entity<SaleEntity>());
            new SaleBillConfiguration(model.Entity<SaleBillEntity>());


        }
    }
}
