using Common.Constant;
using Microsoft.AspNetCore.Mvc;
using SicboSub.Api.Business;
using SicboSub.Api.DTO;

namespace SicboSub.Api.Controllers
{
    /// <summary>
    /// Controller cho Authentication APIs
    /// Xử lý đăng nhập website qua URL token
    /// 
    /// Flow:
    ///   1. User nhận link từ SMS/App: https://domain.com?token=abc123
    ///   2. Frontend lấy token từ URL query parameter
    ///   3. Frontend gọi POST /apis/auth/login với token
    ///   4. API xác thực, trả về thông tin user
    ///   5. Frontend lưu token và user info, cho phép truy cập
    /// </summary>
    [ApiController]
    public class AuthController : Controller
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(AuthController));

        private readonly IAuthBusiness authBusiness;

        public AuthController(IAuthBusiness authBusiness)
        {
            this.authBusiness = authBusiness;
        }

        /// <summary>
        /// Đăng nhập bằng token từ URL
        /// POST /apis/auth/login
        /// 
        /// Body: { "token": "abc123xyz..." }
        /// 
        /// Response success:
        /// {
        ///   "errorCode": "0",
        ///   "data": {
        ///     "id": 1,
        ///     "msisdn": "509xxxxxxxx",
        ///     "username": "user1",
        ///     "winCoin": 1000,
        ///     ...
        ///   }
        /// }
        /// </summary>
        [HttpPost]
        [Route(ApiUrlConstant.AuthLoginUrl)]
        public async Task<IActionResult> LoginByToken([FromBody] TokenLoginReq request)
        {
            return await authBusiness.LoginByToken(HttpContext.Request, request);
        }

        /// <summary>
        /// Lấy MSISDN từ token (API nhẹ)
        /// POST /apis/auth/get-msisdn
        /// 
        /// Body: { "token": "abc123xyz..." }
        /// 
        /// Response:
        /// {
        ///   "errorCode": "0",
        ///   "data": {
        ///     "msisdn": "509xxxxxxxx",
        ///     "isValid": true
        ///   }
        /// }
        /// </summary>
        [HttpPost]
        [Route(ApiUrlConstant.AuthGetMsisdnUrl)]
        public async Task<IActionResult> GetMsisdnFromToken([FromBody] GetMsisdnFromTokenReq request)
        {
            return await authBusiness.GetMsisdnFromToken(HttpContext.Request, request);
        }
    }
}
