using BankDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using BankContracts.BindingModels;
using BankContracts.ViewModels;

namespace BankDatabaseImplement.Models
{
    public class Currency : ICurrencyModel
    {
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        public int BankOperatorId { get; set; }
        public virtual BankOperator BankOperator { get; set; } = new();
        [ForeignKey("CurrencyId")]
        public virtual List<CreditProgramCurrency> CreditProgramCurrency { get; set; } = new();
        public static Currency? Create(BankDatabase context, CurrencyBindingModel model)
        {
            if (model == null)
            {
                return null;
            }
            return new Currency()
            {
                Id = model.Id,
                Name = model.Name,
                BankOperatorId = model.BankOperatorId,
                BankOperator = context.BankOperators.First(x => x.Id == model.BankOperatorId),
            };

        }
        public static Currency? Create(BankDatabase context, CurrencyViewModel model)
        {
            return new Currency()
            {
                Id = model.Id,
                Name = model.Name,
                BankOperatorId = model.BankOperatorId,
                BankOperator = context.BankOperators.First(x => x.Id == model.BankOperatorId),
            };
        }

        public void Update(CurrencyBindingModel? model)
        {
            if (model == null)
            {
                return;
            }
            Name = model.Name;
        }
        public CurrencyViewModel GetViewModel => new()
        {
            Id = Id,
            Name = Name,
            BankOperatorName = BankOperator.LastName + " "
            + BankOperator.FirstName + " "
            + BankOperator.MiddleName,
        };
    }
}
