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 eSuite.Core.CustomFields;
using Microsoft.EntityFrameworkCore;

namespace e_suite.Database.Core.Tables.CustomFields;

[DisplayName("Custom Field")]
[Table("CustomFields", Schema = "CustomFields")]
[Index(nameof(Name), IsUnique = true)]
[Index(nameof(Guid), IsUnique = true)]
public class CustomField : IGeneralId, ISoftDeletable
{
    [Key]
    public long Id { get; set; }

    [AuditName]
    [Required]
    public string Name { get; set; } = string.Empty;
    
    [AuditSoftDelete(true)]
    [Required]
    [DefaultValue(false)]
    public bool Deleted { get; set; } = false;

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

    [Required] public FieldType FieldType { get; set; } = FieldType.Text;

    [Required]
    [DefaultValue(1)]
    public long MinEntries { get; set; } = 1;

    [DefaultValue(1)]
    public long? MaxEntries { get; set; } = 1;

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

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

    public static void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<CustomField>().HasData(new CustomField
        {
            Id = 1,
            Name = "Print Specification Form Template",
            Deleted = false,
            Guid = new Guid("{8D910089-3079-4A29-ABAD-8DDF82DB6DBB}"),
            FieldType = FieldType.FormTemplate,
            MinEntries = 1,
            MaxEntries = 1,
            DefaultValue = string.Empty,
        });
    }
}