using Microsoft.AspNetCore.Mvc;
using PhilExampleCrawler.Common.Models;
using PhilExampleCrawler.DataBase;
using PhilExampleCrawler.DataBase.Models;

namespace WebAPI.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class UserController : ControllerBase
    {
        private readonly ILogger<UserController> _logger;


        public UserController(ILogger<UserController> logger)
        {
            _logger = logger;
        }


        [HttpGet(Name = "GetUser")]
        public async Task<User?> GetAsync(int userID)
        {
            var user = await ThreadSafeCache.GetUserAsync(userID);
            return user;
        }

        [HttpPost(Name = "RegisterUser")]
        public async Task<User?> PostAsync(string authCode, string phoneNumber, bool optin_telegram)
        {
            if (authCode == null)
                return null;
            if (optin_telegram && phoneNumber == null)
                return null;

            User? user = await ThreadSafeCache.AddUserAsync(authCode, phoneNumber, optin_telegram);
            return user;
        }
    }
}