using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using System.Threading.Tasks;
using WebVentaCoche.DataBase;
using WebVentaCoche.Helpers;
using WebVentaCoche.ViewModels;
using Microsoft.EntityFrameworkCore;

namespace WebVentaCoche.Controllers
{
    [Authorize]
    public class AccountController : Controller
    {
        private readonly IUserHelper _userHelper;
        private readonly ApplicationDbContext _context;

        public AccountController(IUserHelper userHelper, ApplicationDbContext context)
        {
            _userHelper = userHelper;
            _context = context;
        }

        //GET:/Account/Details/{id}
        [HttpGet]
        public async Task<IActionResult> Details(string id)
        {
            if (string.IsNullOrEmpty(id))
                return BadRequest();

            var user = await _userHelper.GetUserByIdAsync(id);
            if (user == null)
                return NotFound();

            var addresses = await _context.Addresses.Where(a => a.UserId == id).ToListAsync();

            var vm = new AccountDetailsViewModel
            {
                Id = user.Id,
                Name = user.Name,
                Surname = user.Surname,
                Email = user.Email,
                PhoneNumber = user.PhoneNumber,
                UserType = user.UserType,
                Addresses = addresses.Select(a => new AddressViewModel
                {
                    Id = a.Id,
                    Street = a.Street,
                    City = a.City,
                    State = a.State,
                    ZipCode = a.ZipCode,
                    Country = a.Country
                }).ToList()
            };

            return View(vm);
        }

        //GET:/Account/Settings
        [HttpGet]
        public async Task<IActionResult> Settings()
        {
            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
            var user = await _userHelper.GetUserByIdAsync(userId);
            if (user == null) return NotFound();

            var addresses = await _context.Addresses.Where(a => a.UserId == userId).ToListAsync();

            var vm = new AccountDetailsViewModel
            {
                Id = user.Id,
                Name = user.Name,
                Surname = user.Surname,
                Email = user.Email,
                PhoneNumber = user.PhoneNumber,
                UserType = user.UserType,
                Addresses = addresses.Select(a => new AddressViewModel
                {
                    Id = a.Id,  
                    Street = a.Street,
                    City = a.City,
                    State = a.State,
                    ZipCode = a.ZipCode,
                    Country = a.Country
                }).ToList()
            };

            return View(vm);
        }

        //GET:/Account/Addresses
        [HttpGet]
        public async Task<IActionResult> Addresses()
        {
            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
            var addresses = await _context.Addresses.Where(a => a.UserId == userId).ToListAsync();

            var vm = addresses.Select(a => new AddressViewModel
            {
                Id = a.Id,
                Street = a.Street,
                City = a.City,
                State = a.State,
                ZipCode = a.ZipCode,
                Country = a.Country
            }).ToList();

            return View(vm);
        }

        //GET:/Account/Security
        [HttpGet]
        public IActionResult Security()
        {
            //TODO:VM con políticas de contraseña, etc.
            return View();
        }
    }
}
