using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Configuration;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Web;

namespace WebApplication1.Models {
  public class Tache {
    //[System.ComponentModel.DataAnnotations.Key]    // OK car nommé "Id"
    public Guid Id { get; set; }
    [MaxLength( 50 ), Required]
    public string Nom { get; set; }
    public DateTime Echeance { get; set; }
    [Required]
    public int NbParticipants { get; set; }
    [ForeignKey( "Outil" )]
    public virtual ICollection<Outil> Outils { get; set; }
    public Tache() {                    // Ctor sans argument obligatoire
      Id = Guid.NewGuid();
    }
  }

  [Table( "Outils" )]
  public class Outil {
    public Guid Id { get; set; }
    public string Nom { get; set; }
    public virtual ICollection<Tache> Taches { get; set; }
    public Outil() {
      Id = Guid.NewGuid();
    }
  }

  public class AFaireContext : DbContext {
    public static readonly string CnxAFaire;
    static AFaireContext() {
      CnxAFaire = ConfigurationManager.ConnectionStrings["CnxAFaire"].ConnectionString;
    }
    public AFaireContext() : base( CnxAFaire ) {
    }
    public DbSet<Tache> Taches { get; set; }
    public DbSet<Outil> Outils { get; set; }
    protected override void OnModelCreating( DbModelBuilder mb ) {
      base.OnModelCreating( mb );
      mb.Entity<Outil>()
        .HasMany<Tache>( outil => outil.Taches ) // Fluent API
        .WithMany( tache => tache.Outils )
        .Map( config => {
          config.MapLeftKey( "OutilId" );
          config.MapRightKey( "TacheId" );
          config.ToTable( "OutilsTaches" );
        } );
    }
  }

  internal sealed class Configuration : DbMigrationsConfiguration<AFaireContext> {
    public Configuration() {
      AutomaticMigrationsEnabled = true;
      ContextKey = "WebApplication1.Models.AFaireContext";
    }

    protected override void Seed( AFaireContext context ) {
      var m = new Outil() { Nom = "Marteau" };
      var b = new Outil() { Nom = "Balai" };
      var c = new Outil() { Nom = "Courage" };
      var p = new Outil() { Nom = "Perceuse" };
      // Pas nécessaire car référencés par des Taches suivies par le DC
      //context.Outils.AddOrUpdate( outil => outil.Nom, m, b, c, p );

      var t1 = new Tache() {
        Nom = "Finir mon support de cours",
        NbParticipants = 1,
        Echeance = DateTime.Today.AddDays( -1 ),
        Outils = new HashSet<Outil>() { c }
      };
      var t2 = new Tache() {
        Nom = "Arrêter le chocolat",
        NbParticipants = 0,
        Echeance = DateTime.Now.AddYears( 100 ),
        Outils = new HashSet<Outil>() { c }
      };
      var t3 = new Tache() {
        Nom = "Faire le ménage...",
        NbParticipants = 2,
        Echeance = DateTime.Today.AddYears( 1 ),
        Outils = new HashSet<Outil>() { b }
      };
      var t4 = new Tache() {
        Nom = "Passer à la banque",
        NbParticipants = 3,
        Echeance = DateTime.Today.AddDays( 1 ),
        Outils = new HashSet<Outil>() { c, m, p }
      };
      context.Taches.AddOrUpdate( t => t.Nom, t1, t2, t3, t4 );
      // Les relations ne seront pas restaurées si elles ont été
      //   supprimées mais les entités supprimées seront restaurées
    }
  }




}