using AyCode.Core.Logger;
using Microsoft.EntityFrameworkCore;
using TIAM.Database.DbContexts.Auctions;
using TIAM.Entities.Auctions;

namespace TIAM.Database.DataLayers.Auctions
{
    public class AuctionDal : DalBase<AuctionDbContext>
    {

        public AuctionDal() : base()
        {
        }

        public Task<List<AuctionBid>> GetBids()
        {
            return Context.AuctionBids.ToListAsync();
        }

        public Task<List<AuctionBid>> GetBidsByEmail(string email)
        {
            Logger.Info($"Getting bid from db {email}");
            var emailLower = email.ToLower();
            return Context.AuctionBids.Where(x => x.Email.ToLower() == emailLower).ToListAsync();
        }

        public AuctionBid? GetBidById(Guid id)
        {
            Logger.Info($"Getting bid from db {id}");
            
            return Context.AuctionBids.FirstOrDefault(x => x.Id == id);
        }

        public Task<bool> CreateBidAsync(AuctionBid auctionBid)
        {
            auctionBid.Created = DateTime.UtcNow;
            auctionBid.Modified = DateTime.UtcNow;
            Context.AuctionBids.Add(auctionBid);
            Logger.Info($"Saving user to db {auctionBid.Id}, {auctionBid.Email}, {auctionBid.PhoneNumber}");
            return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
        }

        public Task<bool> UpdateBidAsync(AuctionBid auctionBid)
        {
            auctionBid.Modified = DateTime.UtcNow;
            Context.AuctionBids.Update(auctionBid);
            return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
        }
    }
}
