using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using AyCode.Interfaces.Addresses;
using AyCode.Interfaces.Addresses.Dtos;
using AyCode.Interfaces.Profiles;
using Microsoft.EntityFrameworkCore;

namespace AyCode.Database.DbSets.Addresses
{
    public static class AcAddressDbSetExtensions
    {
        public static IQueryable<TAddress> GetAddresses<TAddress>(this IAcAddressDbSetBase<TAddress> ctx) where TAddress : class, IAcAddressDtoBase
            => ctx.Addresses;

        public static TAddress? GetAddressById<TAddress>(this IAcAddressDbSetBase<TAddress> ctx, Guid addressId) where TAddress : class, IAcAddressDtoBase
            => ctx.GetAddresses().FirstOrDefault(x => x.Id == addressId);

        public static bool UpdateAddress<TAddress>(this IAcAddressDbSetBase<TAddress> ctx, TAddress address) where TAddress : class, IAcAddressDtoBase
            => ctx.Addresses.Update(address).State == EntityState.Modified;

        public static bool RemoveAddress<TAddress>(this IAcAddressDbSetBase<TAddress> ctx, TAddress address) where TAddress : class, IAcAddressDtoBase
            => ctx.Addresses.Remove(address).State == EntityState.Deleted;

        public static bool RemoveAddress<TAddress>(this IAcAddressDbSetBase<TAddress> ctx, Guid addressId) where TAddress : class, IAcAddressDtoBase
        {
            var address = ctx.GetAddressById(addressId);

            return address != null && ctx.RemoveAddress(address);
        }
    }

}
