using ReallifeGamemode.Server.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;

/**
* @overview Life of German Reallife - Entities UserVehicle (UserVehicle.cs)
* @author VegaZ
* @copyright (c) 2008 - 2018 Life of German
*/

namespace ReallifeGamemode.Server.Entities
{
    [Table("UserVehicles")]
    public class UserVehicle : ServerVehicle
    {
        [ForeignKey("User")]
        public int UserId { get; set; }
        public User User { get; set; }

        public override string ToString()
        {
            return "Spieler Fahrzeug | Besitzer: " + GetOwner().Name;
        }

        public User GetOwner(DatabaseContext dbContext = null)
        {
            if (dbContext == null)
            {
                using (dbContext = new DatabaseContext())
                {
                    return dbContext.Users.FirstOrDefault(u => u.Id == UserId);
                }
            }
            else
            {
                return dbContext.Users.FirstOrDefault(u => u.Id == UserId);
            }
        }
    }
}
