namespace Exam.Data.Migrations
{
    using Exam.Models;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Data.Entity.Validation;
    using System.Linq;
    using System.Text;

    public sealed class Configuration : DbMigrationsConfiguration<ApplicationDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
        }

        protected override void Seed(ApplicationDbContext context)
        {
            try
            {
                if (context.Tickets.Count() > 0)
                {
                    return;
                }

                var user = new ApplicationUser
                {
                    UserName = "Peshkata",
                };

                var commentingUser = new ApplicationUser
                {
                    UserName = "NastyJoe",
                };

                Random rand = new Random();

                for (int i = 0; i < 3; i++)
                {
                    var category = new Category
                    {
                        Title = String.Format("TicketCategory{0}-{0}-{0}", i)
                    };

                    for (int j = 0; j < 5; j++)
                    {
                        var ticket = new Ticket
                        {
                            Title = String.Format("Sad Face Bug -{0}-{0}-{0}-", j),
                            Category = category,
                            Description = "It's a NASTYY Bug!",
                            Author = user,
                            ScreenshotUrl = "http://trac.osgeo.org/mapserver/raw-attachment/ticket/1921/curved-bug-1921-screenshot.jpg"
                        };

                        var commentCount = rand.Next(0, 5);
                        ICollection<Comment> comments = new List<Comment>();

                        for (int k = 0; k < commentCount; k++)
                        {
                            comments.Add(new Comment
                            {
                                Text = "Well, this pretty much sucks BROO!",
                                Author = commentingUser,
                                AuthorId = commentingUser.Id
                            });
                        }

                        ticket.Comments = comments;

                        context.Tickets.Add(ticket);
                        ticket.Author.Points++; 
                    }
                }

                context.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                ThrowEntityValidationErrorsFromException(ex);
            }
        }

        /// <summary>
        /// Easy retrieval in seed mistakes.
        /// </summary>
        /// <param name="ex"></param>
        private static void ThrowEntityValidationErrorsFromException(
            DbEntityValidationException ex)
        {
            StringBuilder sb = new StringBuilder();

            foreach (var failure in ex.EntityValidationErrors)
            {
                sb.AppendFormat("{0} failed validation\n", failure.Entry.Entity.GetType());
                foreach (var error in failure.ValidationErrors)
                {
                    sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage);
                    sb.AppendLine();
                }
            }

            throw new DbEntityValidationException(
                "Entity Validation Failed - errors follow:\n" +
                sb.ToString(), ex
            ); // Add the original exception as the innerException
        }
    }
}
