using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AyCode.Database;
using AyCode.Utils.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Identity.Client;
using TIAM.Database.DbContexts.Transfers;
using TIAM.Entities.TransferDestinations;
using TIAM.Entities.Users;

namespace TIAM.Database.DataLayers.TransferDestinations;

public class TransferDestinationDal : DalBase<TransferDestinationDbContext>
{
    public TransferDestinationDal() : base()
    {
    }

    public Task<bool> CreateTransferDestinationAsync(TransferDestination transferDestination)
    {
        if (transferDestination.Id.IsNullOrEmpty())
            transferDestination.Id = Guid.NewGuid();

        Context.TransferDestinations.Add(transferDestination);        
        return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
    }

    public async Task<bool> DeleteTransferDestinationAsync(Guid transferDestinationId)
    {
        var transferDestination = await Context.TransferDestinations.FirstOrDefaultAsync(x => x.Id == transferDestinationId);
        if (transferDestination == null)
            return true;

        Context.TransferDestinations.Remove(transferDestination);
        return await Context.SaveChangesAsync() > 0;
    }

    public Task<bool> UpdateTransferDestinationAsync(TransferDestination transferDestination)
    {
        Context.TransferDestinations.Update(transferDestination);
        return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
    }

    public Task<TransferDestination?> GetTransferDestinationById(Guid transferDestionationId)
    {
        return Context.TransferDestinations.FirstOrDefaultAsync(x => x.Id == transferDestionationId);
    }

    public Task<List<TransferDestination>> GetTransferDestinations()
    {
        return Context.TransferDestinations.ToListAsync();
    }
}