using System.Security.Cryptography.X509Certificates;
using AyCode.Database.DbSets.Users;
using Microsoft.EntityFrameworkCore;
using TIAM.Entities.Users;
using TIAM.Models.Dtos.Users;

namespace TIAM.Database.DbSets.Users;

public static class UserDbSetExtensions
{
    public static IQueryable<User> UsersWithProductRelations(this IUserDbSet ctx, bool autoInclude = true)
        => autoInclude
            ? ctx.Users
                .Include(x => x.UserProductMappings)
                .ThenInclude(x => x.Product)
            : ctx.Users;

    public static User? GetUserById(this IUserDbSet ctx, Guid userId, bool autoInclude)
        => ctx.UsersWithProductRelations(autoInclude).FirstOrDefault(x => x.Id == userId);

    public static User? GetUserByEmail(this IUserDbSet ctx, string email, bool autoInclude)
        => ctx.UsersWithProductRelations(autoInclude).FirstOrDefault(x => x.EmailAddress == email);

    public static UserModelDto? GetUserModelDtoById(this IUserDbSet ctx, Guid userId)
        => ctx.GetUsersById(userId).Select(user => new UserModelDto(user)).FirstOrDefault();

    public static UserModelDto? GetUserModelDtoByEmail(this IUserDbSet ctx, string email)
        => ctx.GetUsersByEmail(email).Select(user => new UserModelDto(user)).FirstOrDefault();

    public static IQueryable<UserModelDto> GetAllUsersModelDto(this IUserDbSet ctx)
        => ctx.Users.Select(user => new UserModelDto(user));

}