using Elwig.Helpers;
using Microsoft.EntityFrameworkCore;
using System;
using System.ComponentModel.DataAnnotations.Schema;
using IndexAttribute = Microsoft.EntityFrameworkCore.IndexAttribute;

namespace Elwig.Models.Entities {
    [Table("credit"), PrimaryKey("Year", "TgNr"), Index("Year", "AvNr", "MgNr", IsUnique = true)]
    public class Credit {
        [Column("year")]
        public int Year { get; set; }

        [Column("tgnr")]
        public int TgNr { get; set; }

        [NotMapped]
        public string TgId => $"{Year}/{TgNr:000}";

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

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

        [Column("net_amount")]
        public long NetAmountValue { get; set; }
        [NotMapped]
        public decimal NetAmount {
            get => Utils.DecFromDb(NetAmountValue, 2);
            set => NetAmountValue = Utils.DecToDb(value, 2);
        }

        [Column("prev_net_amount")]
        public long? PrevNetAmountValue { get; set; }
        [NotMapped]
        public decimal? PrevNetAmount {
            get => PrevNetAmountValue != null ? Utils.DecFromDb(PrevNetAmountValue.Value, 2) : null;
            set => PrevNetAmountValue = value != null ? Utils.DecToDb(value.Value, 2) : null;
        }

        [Column("vat")]
        public double VatValue { get; set; }
        [NotMapped]
        public decimal Vat {
            get => (decimal)VatValue;
            set => VatValue = (double)value;
        }

        [Column("vat_amount")]
        public long VatAmountValue { get; private set; }
        [NotMapped]
        public decimal VatAmount {
            get => Utils.DecFromDb(VatAmountValue, 2);
        }

        [Column("gross_amount")]
        public long GrossAmountValue { get; private set; }
        [NotMapped]
        public decimal GrossAmount {
            get => Utils.DecFromDb(GrossAmountValue, 2);
        }

        [Column("modifiers")]
        public long? ModifiersValue { get; set; }
        [NotMapped]
        public decimal? Modifiers {
            get => ModifiersValue != null ? Utils.DecFromDb(ModifiersValue.Value, 2) : null;
            set => ModifiersValue = value != null ? Utils.DecToDb(value.Value, 2) : null;
        }

        [Column("prev_modifiers")]
        public long? PrevModifiersValue { get; set; }
        [NotMapped]
        public decimal? PrevModifiers {
            get => PrevModifiersValue != null ? Utils.DecFromDb(PrevModifiersValue.Value, 2) : null;
            set => PrevModifiersValue = value != null ? Utils.DecToDb(value.Value, 2) : null;
        }

        [Column("amount")]
        public long AmountValue { get; private set; }
        [NotMapped]
        public decimal Amount {
            get => Utils.DecFromDb(AmountValue, 2);
        }

        [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, AvNr, MgNr")]
        public virtual PaymentMember Payment { get; private set; } = null!;

        [ForeignKey("Year, AvNr")]
        public virtual PaymentVar Variant { get; private set; } = null!;

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