namespace TestCasePlanner.DataAccess.UsersMigrations
{
    using Microsoft.AspNet.Identity;
    using Microsoft.AspNet.Identity.EntityFramework;
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Data.Entity.Validation;
    using System.Linq;
    using TestCasePlanner.Models;

    internal sealed class Configuration : DbMigrationsConfiguration<TestCasePlanner.DataAccess.AppDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
            MigrationsDirectory = @"DataAccess\Migrations";
            ContextKey = "TestCasePlanner.DataAccess.AppDbContext";  
        }

        protected override void Seed(TestCasePlanner.DataAccess.AppDbContext context)
        {
            try
            {
                context.Configuration.LazyLoadingEnabled = true;
                context.Roles.Add(new IdentityRole("Admin"));
                context.Roles.Add(new IdentityRole("Manager"));
                context.Roles.Add(new IdentityRole("Tester"));
                context.SaveChanges();

                var admin = new AppUser() { UserName = "administrator", Email = "joe@admin.web" };
                var manager = new AppUser() { UserName = "manager", Email = "joe@manager.web" };
                var tester = new AppUser() { UserName = "tester", Email = "joe@tester.web" };
                var userManager = new UserManager<AppUser>(new UserStore<AppUser>(context));

                userManager.Create(admin);
                userManager.AddPassword(admin.Id, "administrator");

                userManager.Create(manager);
                userManager.AddPassword(manager.Id, "manager");

                userManager.Create(tester, "tester");
                userManager.AddPassword(tester.Id, "tester");
                context.SaveChanges();

                userManager.AddToRole(admin.Id, "Admin");
                userManager.AddToRole(manager.Id, "Manager");
                userManager.AddToRole(tester.Id, "Tester");
                context.SaveChanges();

                var exampleProject = new TestCasePlanner.Models.TestProject()
                {
                    Name = "WHD",
                    Description = "Example project"
                };

                var exampleTestCase = new TestCasePlanner.Models.TestCase()
                {
                    Title = "Hello!",
                    CreatorID = admin.Id,
                    Description = "Example test case",
                    Prerequsity = "Example prerequisties",
                    ProjectID = exampleProject.ID,
                    DateCreated = Convert.ToDateTime("12-4-2014 12:00:00")
                };

                var exampleTestPlan = new TestCasePlanner.Models.TestPlan()
                {
                    Name = "Regression 1/1/2014",
                    CreatorID = admin.Id,
                    ProjectID = exampleProject.ID,
                    DateCreated = Convert.ToDateTime("12-4-2014 12:00:00")
                };

                exampleTestCase.TestPlans = new List<TestCasePlanner.Models.TestPlan>() { exampleTestPlan };
                exampleTestPlan.TestCases = new List<TestCasePlanner.Models.TestCase>() { exampleTestCase };

                context.TestProjects.Add(exampleProject);
                context.TestCases.Add(exampleTestCase);
                context.TestPlans.Add(exampleTestPlan);
                context.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
        }
    }
}