using Elwig.Helpers;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;

namespace Elwig.Models.Entities {
    [Table("area_commitment"), PrimaryKey("FbNr")]
    public class AreaCom {
        [Column("fbnr")]
        public int FbNr { get; set; }

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

        [Column("vtrgid")]
        public required string VtrgId { get; set; }

        [Column("cultid")]
        public string? CultId { get; set; }

        [Column("area")]
        public int Area { get; set; }

        [Column("kgnr")]
        public int KgNr { get; set; }

        [Column("gstnr")]
        public required string GstNr { get; set; }

        [Column("rdnr")]
        public int? RdNr { get; set; }

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

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

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

        [Column("ctime"), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
        public long CTime { get; set; }
        [NotMapped]
        public DateTime CreatedAt {
            get => DateTimeOffset.FromUnixTimeSeconds(CTime).LocalDateTime;
            set => CTime = ((DateTimeOffset)value.ToUniversalTime()).ToUnixTimeSeconds();
        }

        [Column("mtime"), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
        public long MTime { get; set; }
        [NotMapped]
        public DateTime ModifiedAt {
            get => DateTimeOffset.FromUnixTimeSeconds(MTime).LocalDateTime;
            set => MTime = ((DateTimeOffset)value.ToUniversalTime()).ToUnixTimeSeconds();
        }

        [Column("xtime")]
        public long? XTime { get; set; }
        [NotMapped]
        public DateTime? ExportedAt {
            get => XTime == null ? null : DateTimeOffset.FromUnixTimeSeconds(XTime.Value).LocalDateTime;
            set => XTime = value == null ? null : ((DateTimeOffset)value.Value.ToUniversalTime()).ToUnixTimeSeconds();
        }

        [Column("itime")]
        public long? ITime { get; set; }
        [NotMapped]
        public DateTime? ImportedAt {
            get => ITime == null ? null : DateTimeOffset.FromUnixTimeSeconds(ITime.Value).LocalDateTime;
            set => ITime = value == null ? null : ((DateTimeOffset)value.Value.ToUniversalTime()).ToUnixTimeSeconds();
        }

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

        [ForeignKey("VtrgId")]
        public virtual AreaComType AreaComType { get; private set; } = null!;

        [ForeignKey("CultId")]
        public virtual WineCult? WineCult { get; private set; }

        [ForeignKey("KgNr")]
        public virtual WbKg Kg { get; private set; } = null!;

        [ForeignKey("KgNr, RdNr")]
        public virtual WbRd? Rd { get; private set; }

        public int SearchScore(IEnumerable<string> keywords) {
            return Utils.GetSearchScore([
                 WineCult?.Name, Kg.AtKg.Name, Rd?.Name, GstNr, Comment,
            ], keywords);
        }
    }
}
