// <copyright file="UserConfiguration.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>

namespace Jellyfin.Database.Implementations.ModelConfiguration
{
    using System;
    using Jellyfin.Database.Implementations.Entities;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore.Metadata.Builders;

    /// <summary>
    /// FluentAPI configuration for the User entity.
    /// </summary>
    public class UserConfiguration : IEntityTypeConfiguration<User>
    {
        /// <inheritdoc/>
        public void Configure(EntityTypeBuilder<User> builder)
        {
            ArgumentNullException.ThrowIfNull(builder);
            builder
                .Property(user => user.Username);

            builder
                .HasOne(u => u.ProfileImage)
                .WithOne()
                .OnDelete(DeleteBehavior.Cascade);

            builder
                .HasMany(u => u.Permissions)
                .WithOne()
                .HasForeignKey(p => p.UserId)
                .OnDelete(DeleteBehavior.Cascade);

            builder
                .HasMany(u => u.Preferences)
                .WithOne()
                .HasForeignKey(p => p.UserId)
                .OnDelete(DeleteBehavior.Cascade);

            builder
                .HasMany(u => u.AccessSchedules)
                .WithOne()
                .OnDelete(DeleteBehavior.Cascade);

            builder
                .HasMany(u => u.DisplayPreferences)
                .WithOne()
                .OnDelete(DeleteBehavior.Cascade);

            builder
                .HasMany(u => u.ItemDisplayPreferences)
                .WithOne()
                .OnDelete(DeleteBehavior.Cascade);

            builder
                .HasIndex(entity => entity.Username)
                .IsUnique();
        }
    }
}
