using MileageTraker.Web.Models;

namespace MileageTraker.Web.Migrations
{
    using System.Data.Entity.Migrations;
    
    public partial class AddPurposeType : DbMigration
    {
		private const string InsertPurposeTemplate = @"INSERT INTO [PurposeType]
					([Purpose]
					,[SortOrder])
				VALUES
					('{0}'
					,'{1}')";

		private static string GetPurposeTypeInsertSql(PurposeType purpose)
		{
			return string.Format(InsertPurposeTemplate,
								 purpose.Purpose,
								 purpose.SortOrder);
		}

        public override void Up()
        {
            CreateTable(
                "PurposeType",
                c => new
                    {
                        PurposeTypeId = c.Int(nullable: false, identity: true),
                        Purpose = c.String(),
                        SortOrder = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.PurposeTypeId);
            
            AddColumn("Log", "Purpose_PurposeTypeId", c => c.Int());
            AddForeignKey("Log", "Purpose_PurposeTypeId", "PurposeType", "PurposeTypeId");
			CreateIndex("Log", "Purpose_PurposeTypeId"); 
			
			var purposeTypes =
					new[]
				    {
					    new PurposeType {Purpose = "Audit/Assessment", SortOrder = 1},
					    new PurposeType {Purpose = "Client Assessment/Home Visit", SortOrder = 3},
					    new PurposeType {Purpose = "Delivery Goods/Services", SortOrder = 5},
					    new PurposeType {Purpose = "Meal Delivery", SortOrder = 7},
					    new PurposeType {Purpose = "Meeting", SortOrder = 9},
					    new PurposeType {Purpose = "(ETHRA)Office Visit", SortOrder = 11},
					    new PurposeType {Purpose = "Pick-Up/Delivery Equipment", SortOrder = 13},
					    new PurposeType {Purpose = "Site Visit", SortOrder = 15},
					    new PurposeType {Purpose = "Training",  SortOrder = 17},
					    new PurposeType {Purpose = "Other (see Notes)", SortOrder = 19},
				    };

			foreach (var purposeType in purposeTypes)
			{
				Sql(GetPurposeTypeInsertSql(purposeType));
			}
        }
        
        public override void Down()
        {
            DropIndex("Log", new[] { "Purpose_PurposeTypeId" });
            DropForeignKey("Log", "Purpose_PurposeTypeId", "PurposeType");
            DropColumn("Log", "Purpose_PurposeTypeId");
            DropTable("PurposeType");
        }
    }
}
