using Microsoft.EntityFrameworkCore;
using System;
using System.ComponentModel.DataAnnotations.Schema;

namespace Elwig.Models {
    [Table("contract"), PrimaryKey("VNr")]
    public class Contract {
        [Column("vnr")]
        public int VNr { get; set; }

        [Column("mgnr")]
        public int MgNr { get; set; }

        [Column("date")]
        public string? DateString { get; set; }

        [NotMapped]
        public DateOnly? Date {
            get {
                return DateString != null ? DateOnly.ParseExact(DateString, "yyyy-MM-dd") : null;
            }
            set {
                DateString = value?.ToString("yyyy-MM-dd");
            }
        }

        [Column("year_from")]
        public int YearFrom { get; set; }

        [Column("year_to")]
        public int? YearTo { get; set; }

        [Column("comment")]
        public string? Comment { get; set; }

        [ForeignKey("MgNr")]
        public virtual Member Member { get; private set; }

        [InverseProperty("Contract")]
        public virtual AreaCom? AreaCom { get; private set; }

        [NotMapped]
        public int? Area => AreaCom?.Area;
    }
}