using EDPlayerJournal.Entries;
using System.ComponentModel.DataAnnotations.Schema;

namespace EDPlayerJournal.BGS; 

public class Transaction : IComparable<Transaction> {
    public List<Entry> Entries { get; } = new List<Entry>();

    public string CompletedAt {
        get {
            DateTime? datetime = CompletedAtDateTime;
            if (datetime == null) {
                return "Unknown";
            }

            return datetime.Value.ToString("dd.MM.yyyy HH:mm UTC");
        }
    }

    public virtual DateTime? CompletedAtDateTime {
        get {
            var items = Entries
                .OrderBy(x => x.Timestamp)
                .ToArray()
                ;
            if (items == null || items.Length == 0) {
                return null;
            }

            Entry last = items.Last();
            return last.Timestamp;
        }
    }

    /// <summary>
    /// Whether the transaction helps the entire system.
    /// While all transactions are related to one faction, sometimes their
    /// overall effect on the system as a whole is more important, than to
    /// their faction. For example, rescuing refugees helps the Thargoid war
    /// effort in the system as a whole, not just for the faction.
    /// So this is true for transactions that help the system as a whole.
    /// </summary>
    public virtual bool SystemContribution { get; } = false;

    /// <summary>
    /// Returns true if this transaction was completed in legacy ED
    /// </summary>
    public bool IsLegacy { get; set; } = false;

    /// <summary>
    /// Controlling faction of the station this entry was made/turned into.
    /// </summary>
    public string? ControllingFaction { get; set; }
    public string? Station { get; set; }
    public string? System { get; set; }
    public ulong SystemAddress { get; set; }
    public string? Faction { get; set; }

    /// <summary>
    /// Whether this transaction type only benefits the controlling faction or not, default: no
    /// </summary>
    public virtual bool OnlyControllingFaction {
        get { return false; }
    }

    public virtual int CompareTo(Transaction? other) {
        throw new NotImplementedException("not implemented");
    }

    public string? Name => ToString();
}
