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

namespace e_suite.Database.Core.Tables.Printer
{
    [DisplayName("Site")]
    [Table("Sites", Schema = "Printer")]
    [Index(nameof(Guid), IsUnique = true)]
    [Index(nameof(SigmaId), IsUnique = true)]
    public class Site : 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;

        [Required]
        public long OrganisationId { get; set; }

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

        [AuditParent]
        [Required]
        [ForeignKey(nameof(OrganisationId))]
        public virtual Organisation Organisation { get; set; } = null!;

        public long? SigmaId { get; set; } = null!;
        public ICollection<Specification> Specifications { get; set; } = null!;

        public static void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Site>()
                .HasMany(x => x.Specifications)
                .WithOne(x => x.Site)
                .HasForeignKey(x => x.SiteId)
                .HasPrincipalKey(x => x.Id);
        }
    }
}
