using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using IRaCIS.Core.Domain.Models;
using Microsoft.EntityFrameworkCore.ChangeTracking;

namespace IRaCIS.Core.Infra.EFCore
{
    public interface ICommandRepository<TEntity> : ICommandRepository<TEntity, Guid> where TEntity : Entity
    {

        Task<TEntity> InsertOrUpdateAsync<TFrom>(TFrom from, bool autoSave = false, params EntityVerifyExp<TEntity>[] verify);

        Task<TEntity> InsertFromDTOAsync<TFrom>(TFrom from, bool autoSave = false, params EntityVerifyExp<TEntity>[] verify);

        /// <summary> EF 跟踪方式，先查询出完整的实体</summary>

        Task<TEntity> UpdateFromDTOAsync<TFrom>(TFrom from, bool autoSave = false, bool ignoreDtoNullProperty = true, params EntityVerifyExp<TEntity>[] verify);



        #region EF 跟踪方式删除  (先查询完整实体，再删除)

        /// <summary>批量删除，EF跟踪方式（所有查询出来，再删除 浪费性能,但是稽查 或者触发某些操作时，需要知道数据库实体信息 不可避免用这种）</summary>
        Task<List<TEntity>> DeleteFromQueryAsync(Expression<Func<TEntity, bool>> deleteFilter, bool autoSave = false, bool ignoreQueryFilter = false);

        Task<List<TEntity>> SoftDeleteFromQueryAsync(Expression<Func<TEntity, bool>> deleteFilter, bool autoSave = false, bool ignoreQueryFilter = false);

        Task<TEntity> DeleteFromQueryAsync(Guid id, bool autoSave = false, bool ignoreQueryFilter = false);


        #endregion


        #region EF跟踪方式  部分字段更新   会查询出来完整的实体

        /// <summary> EF跟踪方式 已有查询好的，再更新部分字段 </summary>
        Task<bool> UpdateAsync(TEntity entity, Expression<Func<TEntity, TEntity>> updateFactory,
            bool autoSave = false, CancellationToken cancellationToken = default);

        /// <summary> EF跟踪方式 会去数据库查询完整的实体，再更新部分字段 </summary>
        Task<TEntity> UpdatePartialFromQueryAsync(Guid id, Expression<Func<TEntity, TEntity>> updateFactory,
            bool autoSave = false, CancellationToken cancellationToken = default);

        /// <summary> 稽查用这个 EF跟踪方式 先查询出来所有实体，再更新部分字段 </summary>
        Task UpdatePartialFromQueryAsync(Expression<Func<TEntity, bool>> updateFilter,
            Expression<Func<TEntity, TEntity>> updateFactory,
            bool autoSave = false, bool ignoreQueryFilter = false, CancellationToken cancellationToken = default);

        #endregion

        #region  EF跟踪方式   部分字段更新，不会去数据库查询完整实体

        /// <summary>EF跟踪方式  生成 部分字段立即更新，不会去数据库查询完整的实体</summary>

        Task<bool> UpdatePartialNowNoQueryAsync(Guid id, Expression<Func<TEntity, TEntity>> updateFactory, params EntityVerifyExp<TEntity>[] verify);

        /// <summary> EF跟踪方式  生成 部分字段更新，不会去数据库查询完整的实体</summary>
        Task UpdatePartialNoQueryAsync(Guid id, Expression<Func<TEntity, TEntity>> updateFactory, bool autoSave = false, params EntityVerifyExp<TEntity>[] verify);
        #endregion

        #region 不走EF 跟踪机制，直接生成sql 批量更新或者删除

        /// <summary>批量删除，相当于原生sql，  没用EF跟踪方式（所有查询出来，再删除 浪费性能）</summary>
        Task<bool> BatchDeleteNoTrackingAsync(Expression<Func<TEntity, bool>> deleteFilter);

        /// <summary>批量更新，相当于原生sql，  没用EF跟踪方式（所有查询出来，再更新 浪费性能）</summary>
        Task<bool> BatchUpdateNoTrackingAsync(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TEntity>> updateFactory);

        #endregion




        /// <summary> 不常用  暂时废弃</summary>
        Task<TEntity> UpdatePartialFieldsAsync(TEntity entity, string[] propertyNames, bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp<TEntity>[] verify);


    }

    public interface ICommandRepository<TEntity, TKey> where TEntity : class
    {
        EntityEntry<TEntity> Attach(TEntity entity);

        EntityEntry Entry(TEntity t);

        /// <summary>
        ///不跟踪  查询单个实体，不会出现NUll
        /// </summary>
        /// <param name="exp"></param>
        /// <param name="ignoreQueryFilters"></param>
        /// <returns></returns>
        Task<TEntity> FirstAsync(Expression<Func<TEntity, bool>> exp = null, bool isTracking = false,bool ignoreQueryFilters = false);

        /// <summary>
        ///跟踪  查询单个实体，会出现NUll
        /// </summary>
        /// <param name="exp"></param>
        /// <param name="ignoreQueryFilters"></param>
        /// <returns></returns>
        Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> exp = null, bool ignoreQueryFilters = false);

        Task<TEntity> FirstOrDefaultNoTrackingAsync(Expression<Func<TEntity, bool>> exp = null, bool ignoreQueryFilters = false);

        Task<bool> AnyAsync(Expression<Func<TEntity, bool>> exp, bool ignoreQueryFilters = false);

        bool Any(Expression<Func<TEntity, bool>> exp, bool ignoreQueryFilters = false);

        Task<TResult> MaxAsync<TResult>(Expression<Func<TEntity, TResult>> selector);

        Task<int> CountAsync(Expression<Func<TEntity, bool>> whereLambda = null, bool ignoreQueryFilters = false);

        ValueTask<TEntity> AddAsync(TEntity entity, bool autoSave = false);

        Task<IEnumerable<TEntity>> AddRangeAsync(IEnumerable<TEntity> entities, bool autoSave = false);

        Task<bool> SaveChangesAsync(CancellationToken cancellationToken = default);
        Task<bool> DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default);





    }



}
