using Microsoft.AspNet.Identity.EntityFramework;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using TestCasePlanner.Models;

namespace TestCasePlanner.DataAccess
{
    public class AppDbContext : IdentityDbContext<AppUser>
    {
        public AppDbContext() : base("DefaultConnection")
        {
        }

        public DbSet<TestCase> TestCases { get; set; }
        public DbSet<TestPlan> TestPlans { get; set; }       
        public DbSet<TestProject> TestProjects { get; set; }
        public DbSet<StepResultDefinition> StepResultDefinitions { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();

            modelBuilder.Entity<AppUser>().HasMany(m => m.TestCases).WithRequired(a => a.Creator).HasForeignKey(a => a.CreatorID);
            modelBuilder.Entity<TestPlan>().HasMany(tc => tc.TestCases).WithMany(tp => tp.TestPlans).Map(tp =>
                {
                   tp.ToTable("TestCaseTestPlan");
                   tp.MapLeftKey("TestCaseID");
                   tp.MapRightKey("TestProjectID");
               });
        }
    }
}