using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using AyCode.Core.Extensions; using TIAM.Core.Enums; using TIAM.Entities.Profiles; using TIAM.Entities.ServiceProviders; using TIAM.Entities.Users; namespace TIAM.Entities.Products; [Table("Products")] public class Product : ProductBase { [Required] public Guid ServiceProviderId { get; set; } public virtual Company ServiceProvider { get; set; } public virtual List Users { get; } = new(); public virtual List UserProductMappings { get; } = new(); public Product(){} public Product(Guid ownerId, ProductType productType, string name, string description, float price, string jsonDetails) : this(Guid.NewGuid(), ownerId, productType, name, description, price, jsonDetails) { } public Product(Guid id, Guid serviceProviderId, ProductType productType, string name, string description, float price, string jsonDetails) : base(id, productType, name, description, price, jsonDetails) { ServiceProviderId = serviceProviderId; } public Product(Guid ownerId, Company serviceProvider, ProductType productType, string name, string description, float price, string jsonDetails) : this(Guid.NewGuid(), ownerId, serviceProvider, productType, name, description, price, jsonDetails) { } public Product(Guid id, Guid serviceProviderId, Company serviceProvider, ProductType productType, string name, string description, float price, string jsonDetails) : base(id, productType, name, description, price, jsonDetails) { ServiceProviderId = serviceProviderId; ServiceProvider = serviceProvider; } public void SetProfile(Profile profile) { if (profile.Id.IsNullOrEmpty()) profile.Id = Guid.NewGuid(); Profile = profile; ProfileId = profile.Id; } public bool HasUser(Guid userId) => UserProductMappings.Any(x => x.UserId == userId); public void AddUser(Guid userId, int permissions) { if (UserProductMappings.Any(x => x.UserId == userId)) return; UserProductMappings.Add(new UserProductMapping(Guid.NewGuid(), userId, Id) { Permissions = permissions }); } }