using FoodsharingSiegen.Contracts.Entity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;

namespace FoodsharingSiegen.Server.Data
{
    /// <summary>
    ///     The fs context class (a. beging, 21.05.2022)
    /// </summary>
    /// <seealso cref="DbContext" />
    public sealed class FsContext : DbContext
    {
        #region Public Properties

        /// <summary>
        ///     Gets or sets the value of the audits (ab)
        /// </summary>
        public DbSet<Audit>? Audits { get; set; }

        /// <summary>
        ///     Gets or sets the value of the interactions (ab)
        /// </summary>
        public DbSet<Interaction>? Interactions { get; set; }

        /// <summary>
        ///     Gets or sets the value of the prospects (ab)
        /// </summary>
        public DbSet<Prospect>? Prospects { get; set; }

        /// <summary>
        ///     Gets or sets the uploaded verification images mapping.
        /// </summary>
        public DbSet<ProspectImage>? ProspectImages { get; set; }

        /// <summary>
        ///     Gets or sets the value of the users (ab)
        /// </summary>
        public DbSet<User>? Users { get; set; }

        #endregion

        #region Setup/Teardown

        /// <summary>
        ///     Initializes a new instance of the <see cref="FsContext" /> class
        /// </summary>
        /// <param name="options">The options (ab)</param>
        public FsContext(DbContextOptions<FsContext> options) : base(options)
        {
            if (Database.IsRelational())
            {
                Database.Migrate();
            }
        }

        #endregion

        #region Override OnConfiguring

        /// <summary>
        ///     Configures the database context options.
        /// </summary>
        /// <param name="optionsBuilder">A builder used to create or modify options for the context.</param>
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.ConfigureWarnings(builder => { builder.Ignore(RelationalEventId.PendingModelChangesWarning); });

            base.OnConfiguring(optionsBuilder);
        }

        #endregion

        #region Public Method HasChanges

        /// <summary>
        ///     Describes whether this instance has changes
        /// </summary>
        /// <returns>The bool</returns>
        public bool HasChanges()
        {
            return ChangeTracker.Entries().Any(e => e.State is EntityState.Added or EntityState.Modified or EntityState.Deleted);
        }

        #endregion
    }
}