using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;
using AyCode.Interfaces.Addresses;
using AyCode.Interfaces.Addresses.Dtos;
using AyCode.Interfaces.Profiles;
using AyCode.Interfaces.Profiles.Dtos;
using AyCode.Interfaces.ServiceProviders;
using AyCode.Interfaces.Users;
using AyCode.Interfaces.Users.Dtos;

namespace AyCode.Models.Users;

public abstract class AcUserModelDtoBase<TUserDto, TProfileDto, TCompany, TUserToCompany> : AcUserModelDtoMinBase<TUserDto, TProfileDto>, IAcCompanyRelation<TCompany, TUserToCompany>
    where TUserDto : class, IAcUserDtoBase
    where TProfileDto : class, IAcProfileDtoBase
    where TCompany : class, IAcCompanyBase
    where TUserToCompany : class, IAcUserToCompanyBase
//where TAddressDto : class, IAcAddressDtoBase
{
    public List<TCompany> ServiceProviders { get; set; } = [];
    public List<TUserToCompany> UserToServiceProviders { get; set; } = [];


    protected AcUserModelDtoBase()
    {
    }

    protected AcUserModelDtoBase(IAcUserDtoBase<TProfileDto, TCompany, TUserToCompany> user) : base(user)
    {
        ProfileDto.AddressId = user.Profile.AddressId;
        
        if (user.ServiceProviders.Count == 0) return;

        //így proxy error lesz... - J.
        //ServiceProviders = new List<TCompany>(user.ServiceProviders);
        //UserToServiceProviders = new List<TUserToServiceProvider>(user.UserToServiceProviders);

        ServiceProviders = new List<TCompany>(user.ServiceProviders.Count);
        UserToServiceProviders = new List<TUserToCompany>(user.UserToServiceProviders.Count);

        foreach (var serviceProvider in user.ServiceProviders)
        {
            var newProvider = Activator.CreateInstance<TCompany>();

            newProvider.Id = serviceProvider.Id;
            newProvider.Name = serviceProvider.Name;
            newProvider.OwnerId = serviceProvider.OwnerId;

            ServiceProviders.Add(newProvider);
        }

        foreach (var userToServiceProvider in user.UserToServiceProviders)
        {
            var newUserToProvider = Activator.CreateInstance<TUserToCompany>();

            newUserToProvider.Id = userToServiceProvider.Id;
            newUserToProvider.UserId = userToServiceProvider.UserId;
            newUserToProvider.ServiceProviderId = userToServiceProvider.ServiceProviderId;

            UserToServiceProviders.Add(newUserToProvider);
        }
    }
}