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")]
        public long CTime { get; private set; }
        [NotMapped]
        public DateTime CreatedTimestamp => DateTimeOffset.FromUnixTimeSeconds(CTime).LocalDateTime;

        [Column("mtime")]
        public long MTime { get; private set; }
        [NotMapped]
        public DateTime ModifiedTimestamp => DateTimeOffset.FromUnixTimeSeconds(CTime).LocalDateTime;

        [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!;
    }
}