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

namespace DAL.Repositories
{
    public class ArrivalHandoverFormRepository : IArrivalHandoverFormRepository
    {
        private readonly ISqlSugarProvider _provider;

        public ArrivalHandoverFormRepository(ISqlSugarProvider provider)
        {
            _provider = provider;
            // 初始化表结构
            var db = _provider.GetClient();
            db.CodeFirst.InitTables(typeof(ArrivalHandoverFormEntity));
        }

        public async Task<int> InsertAsync(ArrivalHandoverFormEntity form)
        {
            var db = _provider.GetClient();
            return await db.Insertable(form).ExecuteCommandAsync();
        }

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

        public async Task<ArrivalHandoverFormEntity> GetByHandoverNumberAsync(string handoverNumber)
        {
            var db = _provider.GetClient();
            return await db.Queryable<ArrivalHandoverFormEntity>().Where(f => f.HandoverNumber == handoverNumber).FirstAsync();
        }

        public async Task<List<ArrivalHandoverFormEntity>> GetAllAsync()
        {
            var db = _provider.GetClient();
            return await db.Queryable<ArrivalHandoverFormEntity>().ToListAsync();
        }

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

        public async Task<int> DeleteAsync(int id)
        {
            var db = _provider.GetClient();
            return await db.Deleteable<ArrivalHandoverFormEntity>().Where(f => f.Id == id).ExecuteCommandAsync();
        }

        public async Task<bool> ExistsByHandoverNumberAsync(string handoverNumber)
        {
            var db = _provider.GetClient();
            return await db.Queryable<ArrivalHandoverFormEntity>().Where(f => f.HandoverNumber == handoverNumber).AnyAsync();
        }

        public async Task<(List<ArrivalHandoverFormEntity> Forms, int TotalCount)> GetArrivalHandoverFormsBatchAsync(
            int page,
            int pageSize,
            string sortBy,
            string sortOrder,
            string handoverNumber,
            string creator,
            DateTime? startDate = null,
            DateTime? endDate = null)
        {
            var db = _provider.GetClient();
            var query = db.Queryable<ArrivalHandoverFormEntity>();

            if (!string.IsNullOrEmpty(handoverNumber))
            {
                query = query.Where(f => f.HandoverNumber.Contains(handoverNumber));
            }
            if (!string.IsNullOrEmpty(creator))
            {
                query = query.Where(f => f.Creator.Contains(creator));
            }
            if (startDate.HasValue)
            {
                query = query.Where(f => f.ReceiptTime >= startDate.Value);
            }
            if (endDate.HasValue)
            {
                query = query.Where(f => f.ReceiptTime <= endDate.Value);
            }

            var totalCount = await query.CountAsync();

            if (!string.IsNullOrEmpty(sortBy))
            {
                query = sortOrder.ToLower() == "asc"
                    ? query.OrderBy(sortBy)
                    : query.OrderBy($"{sortBy} desc");
            }
            else
            {
                query = query.OrderBy("CreatedAt desc");
            }

            var forms = await query.Skip((page - 1) * pageSize).Take(pageSize).ToListAsync();

            return (forms, totalCount);
        }
    }
}