using e_suite.Database.Audit.Attributes;
using e_suite.Database.Core.Models;
using Microsoft.EntityFrameworkCore;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace e_suite.Database.Core.Tables.Printer
{
    [DisplayName("Organisation")]
    [Table("Organisations", Schema = "Printer")]
    [Index(nameof(Id), IsUnique = true)]
    [Index(nameof(Guid), IsUnique = true)]
    [Index(nameof(Name), IsUnique = true)]
    public class Organisation : IGeneralId, ISoftDeletable, IName
    {
        [Key]
        public long Id { get; set; }

        [Required]
        public Guid Guid { get; set; } = Guid.NewGuid();

        [AuditName]
        [Required]
        public string Name { get; set; } = string.Empty;

        public string Address { get; set; } = string.Empty;

        public OrganisationStatus Status { get; set; }

        [AuditSoftDelete(true)]
        [Required]

        [DefaultValue(false)]
        public bool Deleted { get; set; } = false;

        [AuditLastUpdated]
        public DateTimeOffset LastUpdated { get; set; }

        public ICollection<Site> Sites { get; set; } = (Collection<Site>) [];

        public static void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Organisation>()
                .HasMany(x => x.Sites)
                .WithOne(x => x.Organisation)
                .HasForeignKey(x => x.OrganisationId)
                .HasPrincipalKey(x => x.Id);
        }
    }
}
