using Microsoft.EntityFrameworkCore;
using ReallifeGamemode.Server.Models;
using ReallifeGamemode.Server.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;

namespace ReallifeGamemode.Server.Entities
{
    public class Group : IBankAccountOwner
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        public string Name { get; set; }

        public IBankAccount GetBankAccount(DatabaseContext databaseContext = null)
        {
            GroupBankAccount bankAccount = null;
            if (databaseContext == null)
            {
                using (databaseContext = new DatabaseContext())
                {
                    bankAccount = databaseContext.GroupBankAccounts.Include(g => g.Group).Where(g => g.Group == this).FirstOrDefault();
                }
            }
            else
            {
                bankAccount = databaseContext.GroupBankAccounts.Include(g => g.Group).Where(g => g.Group == this).FirstOrDefault();
            }

            if (bankAccount == null)
            {
                bankAccount = new GroupBankAccount
                {
                    Group = this,
                    Balance = 0
                };

                databaseContext.GroupBankAccounts.Add(bankAccount);
                databaseContext.SaveChanges();
            }

            return bankAccount;
        }
    }
}
