using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

using Monitoring.Core.Domain;

namespace Monitoring.Infrastructure.Data.Configurations;

public class RefreshTokenConfiguration : IEntityTypeConfiguration<RefreshToken>
{
	public void Configure(EntityTypeBuilder<RefreshToken> builder)
	{
		builder.HasKey(rt => rt.Id);

		builder.Property(rt => rt.Token)
			.HasMaxLength(100)
			.IsRequired();

		builder.Property(rt => rt.ExpiryDate)
			.IsRequired();

		builder.Property(rt => rt.IsUsed)
			.IsRequired();

		builder.Property(rt => rt.IsRevoked)
			.IsRequired();

		builder.Property(rt => rt.UserId)
			.HasConversion(x => x.Value, x => new(x))
			.IsRequired();

		builder.HasOne(rt => rt.User)
			.WithMany(rt => rt.RefreshTokens)
			.HasForeignKey(rt => rt.UserId)
			.OnDelete(DeleteBehavior.Cascade);
	}
}
