namespace AAIntegration.SimmonsBank.API.Services;

using AutoMapper;
using BCrypt.Net;
using AAIntegration.SimmonsBank.API.Entities;
using AAIntegration.SimmonsBank.API.Config;
using AAIntegration.SimmonsBank.API.Models.Accounts;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System;
using Internal;
using Microsoft.EntityFrameworkCore;

public interface IAccountService
{
    IEnumerable<Account> GetAll();
    Account GetById(int accountId);
    void Create(AccountCreateRequest model);
    void Update(int accountId, AccountUpdateRequest model);
    void Delete(int accountId);
}

public class AccountService : IAccountService
{
    private DataContext _context;
    private readonly IMapper _mapper;
    private IUserService _userService;

    public AccountService(
        DataContext context,
        IMapper mapper,
        IUserService userService)
    {
        _context = context;
        _mapper = mapper;
        _userService = userService;
    }

    public IEnumerable<Account> GetAll()
    {
        return _context.Accounts;
    }

    public Account GetById(int accountId)
    {
        return getAccount(accountId);
    }

    public void Create(AccountCreateRequest model)
    {
        // Check that account with same name or same external number doesn't exist
        IEnumerable<Account> accountsWithSameName = _context.Accounts.Where(a => a.Name.ToUpper() == model.Name.ToUpper());

        if (accountsWithSameName.Count() > 0)
            throw new AppException("Account with name '" + model.Name + "' already exists");

        if (!string.IsNullOrWhiteSpace(model.ExternalAccountNumber))
        {
            IEnumerable<Account> matches = _context.Accounts.Where(a => a.ExternalAccountNumber == model.ExternalAccountNumber);

            if (matches.Count() > 0)
                throw new AppException("Account with external account number '" + model.ExternalAccountNumber + "' already exists under account named '" + matches.First().Name + "'");
        }

        // map model to new account object
        //var account = _mapper.Map<Account>(model);
        
        Account account = new Account {
            Name = model.Name,
            Balance = Convert.ToDecimal(model.InitialBalance),
            ExternalAccountNumber = model.ExternalAccountNumber
        };

        _context.Accounts.Add(account);
        _context.SaveChanges();
    }

    public void Update(int accountId, AccountUpdateRequest model)
    {
        Account account = getAccount(accountId);

        // validate
        if (model.Name != account.Name && _context.Accounts.Any(x => x.Name == model.Name))
            throw new AppException("Account with the name '" + model.Name + "' already exists");

        // Name
        if (!string.IsNullOrWhiteSpace(model.Name))
            account.Name = model.Name;

        // External Account Number
        if (!string.IsNullOrWhiteSpace(model.ExternalAccountNumber))
            account.ExternalAccountNumber = model.ExternalAccountNumber;

        _context.Accounts.Update(account);
        _context.SaveChanges();
    }

    public void Delete(int accountId)
    {
        var account = getAccount(accountId);
        _context.Accounts.Remove(account);
        _context.SaveChanges();
    }

    // helper methods

    private Account getAccount(int id)
    {
        var account = _context.Accounts.Find(id);
        if (account == null) throw new KeyNotFoundException("Account not found");
        return account;
    }
}