namespace AAIntegration.SimmonsBank.API.Controllers;

using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using AAIntegration.SimmonsBank.API.Models.Accounts;
using AAIntegration.SimmonsBank.API.Services;
using AAIntegration.SimmonsBank.API.Config;
using System.Collections.Generic;
using AAIntegration.SimmonsBank.API.Entities;
using Microsoft.AspNetCore.Authorization;

[Authorize]
[ApiController]
[Route("[controller]")]
public class AccountsController : ControllerBase
{
    private IAccountService _accountService;
    private IMapper _mapper;
    private readonly AppSettings _appSettings;

    public AccountsController(
        IAccountService accountService,
        IMapper mapper,
        IOptions<AppSettings> appSettings)
    {
        _accountService = accountService;
        _mapper = mapper;
        _appSettings = appSettings.Value;
    }

    [HttpGet]
    public IActionResult GetAll()
    {
        List<AccountDTO> accountDtos = new List<AccountDTO>();

        foreach (Account acc in _accountService.GetAll())
            accountDtos.Add(_mapper.Map<Account, AccountDTO>(acc));

        return Ok(accountDtos);
    }

    [HttpGet("{id}")]
    public IActionResult GetById(int id)
    {
        Account account = _accountService.GetById(id);
        return Ok(_mapper.Map<Account, AccountDTO>(account));
    }

    [HttpPost]
    public IActionResult Create([FromBody]AccountCreateRequest model)
    {
        _accountService.Create(model);
        return Ok(new { message = "account created" });
    }

    [HttpPut("{id}")]
    public IActionResult Update(int id, [FromBody]AccountUpdateRequest model)
    {
        _accountService.Update(id, model);
        return Ok(new { message = "account updated" });
    }

    [HttpDelete("{id}")]
    public IActionResult Delete(int id)
    {
        _accountService.Delete(id);
        return Ok(new { message = "account deleted" });
    }
}