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

namespace e_suite.Database.Core.Tables.Forms;

[DisplayName("Form Instance")]
[Table("FormInstances", Schema = "Forms")]
[Index(nameof(Guid), IsUnique = true)]
public class FormInstance : IGeneralId, ISoftDeletable
{
    [Key]
    public long Id { get; set; }

    [Required]
    public long FormTemplateVersionId { get; set; }
    
    [AuditSoftDelete(true)]
    [Required]
    [DefaultValue(false)]
    public bool Deleted { get; set; } = false;

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

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

    [ForeignKey(nameof(FormTemplateVersionId))]
    public virtual FormTemplateVersion FormTemplateVersion { get; set; } = null!;

    public ICollection<FormFieldInstance> FormFields { get; set; } = null!;

    public static void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<FormInstance>()
            .HasMany(x => x.FormFields)
            .WithOne(x => x.FormInstance)
            .HasForeignKey(x => x.FormInstanceId)
            .HasPrincipalKey(x => x.Id);
    }
}