﻿using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Security.Claims;
using System.Threading.Tasks;

namespace AstrophotographyBlog.Data.Models.Users
{
    public class User : IdentityUser, IAuditable, IDeletable
    {
        private ICollection<Post> posts;

        public User()
        {
            this.posts = new HashSet<Post>();
            //this.CreatedOn = DateTime.UtcNow;
        }
        [Key]
        public override string Id
        {
            get
            {
                return base.Id;
            } 
            set
            {
                base.Id = value;
            }
        }
        [Required]
        [MaxLength(50)]
        public override string UserName
        {
            get
            {
                return base.UserName;
            } 
            set
            {
                base.UserName = value;
            }
        }

        [Index]
        public bool IsDeleted { get; set; }

        [DataType(DataType.DateTime)]
        public DateTime? DeletedOn { get; set; }

        [DataType(DataType.DateTime)]
        public DateTime CreatedOn { get; set; }

        [DataType(DataType.DateTime)]
        public DateTime? ModifiedOn { get; set; }

        public ICollection<Post> Posts
        {
            get
            {
                return this.posts;
            }
            set
            {
                this.posts = value;
            }
        }

        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    }
}
