using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using GreenTree.Strohrmann.ERP.Core.Domain.Rights;
using GreenTree.Strohrmann.ERP.Domain.Model.Rights;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using GreenTree.Strohrmann.ERP.Core.Domain.Business;
using GreenTree.Strohrmann.ERP.Domain.Model.Business;

namespace GreenTree.Strohrmann.ERP.Domain.Model
{
    public class ERPDbContext : DbContext
    {
        #region Tables

        /// <summary>
        /// Crafts table
        /// </summary>
        public DbSet<Craft> Crafts { get; set; }

        /// <summary>
        /// Craft employees trable
        /// </summary>
        public DbSet<CraftEmployee> CraftEmployees { get; set; }

        /// <summary>
        /// Craft materials table
        /// </summary>
        public DbSet<CraftMaterial> CraftMaterials { get; set; }

        /// <summary>
        /// Customer titles table
        /// </summary>
        public DbSet<Title> Titles { get; set; }

        /// <summary>
        /// Customers table
        /// </summary>
        public DbSet<Customer> Customers { get; set; }

        /// <summary>
        /// Employees table
        /// </summary>
        public DbSet<Employee> Employees { get; set; }

        /// <summary>
        /// Employee degrees table
        /// </summary>
        public DbSet<EmployeeDegree> EmployeeDegrees { get; set; }

        /// <summary>
        /// Materials table
        /// </summary>
        public DbSet<Material> Materials { get; set; }

        /// <summary>
        /// Suppliers table
        /// </summary>
        public DbSet<Supplier> Suppliers { get; set; }

        /// <summary>
        /// Taxes table
        /// </summary>
        public DbSet<Tax> Taxes { get; set; }

        /// <summary>
        /// Units table
        /// </summary>
        public DbSet<Unit> Units { get; set; }

        /// <summary>
        /// Users table
        /// </summary>
        public DbSet<User> Users { get; set; }

        /// <summary>
        /// Policies table
        /// </summary>
        public DbSet<Policy> Policies { get; set; }

        /// <summary>
        /// UserPolicies table
        /// </summary>
        public DbSet<UserPolicy> UserPolicies { get; set; }

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the ERPDbContext class
        /// </summary>
        /// <param name="options">Database context options.</param>
        public ERPDbContext(DbContextOptions options) : base(options)
        {

        }

        #endregion

        #region Overrides

        /// <summary>
        /// Model creation override method to initialize the necessary model mappings
        /// </summary>
        /// <param name="modelBuilder">The model builder.</param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ApplyConfiguration(new CraftEmployeeMapping());
            modelBuilder.ApplyConfiguration(new CraftMapping());
            modelBuilder.ApplyConfiguration(new CraftMaterialMapping());
            modelBuilder.ApplyConfiguration(new TitleMapping());
            modelBuilder.ApplyConfiguration(new CustomerMapping());
            modelBuilder.ApplyConfiguration(new EmployeeDegreeMapping());
            modelBuilder.ApplyConfiguration(new EmployeeMapping());
            modelBuilder.ApplyConfiguration(new MaterialMapping());
            modelBuilder.ApplyConfiguration(new SupplierMapping());
            modelBuilder.ApplyConfiguration(new TaxMapping());
            modelBuilder.ApplyConfiguration(new UnitMapping());
            modelBuilder.ApplyConfiguration(new UserMapping());
            modelBuilder.ApplyConfiguration(new PolicyMapping());
            modelBuilder.ApplyConfiguration(new UserPolicyMapping());
        }

        #endregion

        #region Data

        /// <summary>
        /// Create seed data for the model database
        /// </summary>
        /// <typeparam name="T">The entity type gaining the seed data.</typeparam>
        /// <param name="modelBuilder">The basted model builder.</param>
        /// <param name="data">The seeded data.</param>
        private void CreateSeedDate<T>(ModelBuilder modelBuilder, params T[] data)
        {
            modelBuilder.Entity(typeof(T))
                .HasData(data);
        }

        #endregion
    }
}
