using Microsoft.AspNetCore.Mvc;
using economy.Models;
using economy.Models.Lotto;

namespace economy.Controllers
{
    public class LottoController : Controller
    {
        private readonly DhlotteryCoKR _dhlotteryCoKR;

        public LottoController(DhlotteryCoKR dhlotteryCoKR)
        {
            _dhlotteryCoKR = dhlotteryCoKR;
        }

        public async Task<IActionResult> Index(Request request)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            // 주차 간격 계산
            var last = (ushort)(((DateTime.Now - new DateTime(2002, 12, 7)).TotalDays / 7) + 1);
            
            if (request.Number <= 0 && last > 0)
            {
                request.Number = last;
            }

            LottoModel lotto = new LottoModel(_dhlotteryCoKR);
            Response lottoInfo = await lotto.GetLottoNumber(request);

            var viewModel = new View<Request, Response>();
            viewModel.Request = request;
            viewModel.Response = lottoInfo;

            ViewBag.Last = last;

            return View(viewModel);
        }
    }
}
