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

namespace Elwig.Models.Entities {
    [Table("payment_variant"), PrimaryKey("Year", "AvNr")]
    public class PaymentVar {
        [Column("year")]
        public int Year { get; set; }

        [Column("avnr")]
        public int AvNr { get; set; }

        [Column("name")]
        public required string Name { get; set; }

        [Column("date")]
        public required string DateString { get; set; }
        [NotMapped]
        public DateOnly Date {
            get => DateOnly.ParseExact(DateString, "yyyy-MM-dd");
            set => DateString = value.ToString("yyyy-MM-dd");
        }

        [Column("transfer_date")]
        public string? TransferDateString { get; set; }
        [NotMapped]
        public DateOnly? TransferDate {
            get => TransferDateString != null ? DateOnly.ParseExact(TransferDateString, "yyyy-MM-dd") : null;
            set => TransferDateString = value?.ToString("yyyy-MM-dd");
        }

        [Column("test_variant")]
        public bool TestVariant { get; set; }

        [Column("calc_time")]
        public long? CalcTimeUnix { get; set; }
        [NotMapped]
        public DateTime? CalcTime => CalcTimeUnix != null ? DateTimeOffset.FromUnixTimeSeconds(CalcTimeUnix.Value).LocalDateTime : null;

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

        [Column("data")]
        public required string Data { 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("Year")]
        public virtual Season Season { get; private set; } = null!;

        [InverseProperty(nameof(PaymentMember.Variant))]
        public virtual ICollection<PaymentMember> MemberPayments { get; private set; } = null!;

        [InverseProperty(nameof(PaymentDeliveryPart.Variant))]
        public virtual ICollection<PaymentDeliveryPart> DeliveryPartPayments { get; private set; } = null!;

        [InverseProperty(nameof(Credit.Variant))]
        public virtual ICollection<Credit> Credits { get; private set; } = null!;
    }
}
