﻿using EnterpriseApp.Domain.Blog.Entity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EnterpriseApp.DataAccess.EF.Blog.Mapping
{
    public static class MapPostTermTaxonomy
    {

        public static void Map(EntityTypeBuilder<PostTermTaxonomy> entityBuilder)
        {

            // PRIMARY KEY
            entityBuilder
                .HasKey(ptt => new { ptt.PostId, ptt.TermTaxonomyId });

            // PostId
            entityBuilder
                .Property(ptt => ptt.PostId)
                .HasColumnName("PostId")
                //.HasColumnOrder(10)
                .IsRequired()
                ;

            // TermTaxonomyId
            entityBuilder
                .Property(ptt => ptt.TermTaxonomyId)
                .HasColumnName("TermTaxonomyId")
                //.HasColumnOrder(20)
                .IsRequired()
                ;

            // Order
            entityBuilder
                .Property(ptt => ptt.Order)
                .HasColumnName("Order")
                //.HasColumnOrder(30)
                .IsRequired()
                ;

            // Relations

            entityBuilder
                .HasOne(ptt => ptt.Post)
                .WithMany(tt => tt.TermTaxomomies)
                .HasForeignKey(ptt => ptt.PostId)
                .OnDelete(DeleteBehavior.Cascade)
                ;

            entityBuilder
                .HasOne(ptt => ptt.TermTaxonomy)
                .WithMany(tt => tt.PostTermTaxonomies)
                .HasForeignKey(ptt => ptt.TermTaxonomyId)
                .OnDelete(DeleteBehavior.Cascade)
                ;

            // Ignores

            // schema
            //string schemaName = ConfigurationManager.AppSettings["Reveal.DefaultSchema"];

            // TO TABLE
            entityBuilder
                .ToTable("BlgPostTermTaxonomy", "public");

        }

    }
}
