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

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

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

    public User User { get; set; }

    public int? Price { get; set; }

    public int? BusinessId { 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);
      }
    }
  }
}
