<?php

namespace App\Http\Controllers\Service;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use App\Http\Controllers\Controller;
use App\Models\DTO\SearchData;

class LottoController extends Controller
{
    public function __construct()
    {
        $this->middleware('front');
    }

    /**
     * FIFA 순위 조회
     * @method GET
     * @see /service/fifa
     */
    public function index(Request $request)
    {
        $params = SearchData::fromRequest($request);
        $last = $this->_getLottoNumbers();
        $params->number = $request->get('number', $last);

        $response = Http::acceptJson()->get('https://www.dhlottery.co.kr/common.do?method=getLottoNumber&drwNo=' . $params->number);

        $total = 0;
        $result = null;
        if ($response->ok()) {
            $result = $response->object();
        }

        unset($response);

        return view(layout('service.lotto.index'), [
            'menuID' => 'lotto',
            'params' => $params,
            'total' => $total,
            'result' => $result,
            'last' => $last
        ]);
    }

    /**
     * 로또 회차 목록
     */
    private function _getLottoNumbers(): int
    {
        return (now()->diffInWeeks('2002-12-07') + 1);
    }
}
