<?php

namespace App\Http\Controllers;

use App\Http\Requests\FindAnagramWordsRequest;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Lvl0\AnagramFinder\Core\Matcher;

class AnagramController extends Controller
{
    public function index(): View
    {
        return view('index');
    }

    public function find(FindAnagramWordsRequest $request): RedirectResponse
    {
        $anagram = $request->get('anagram');

        return redirect()->route('results', $anagram);
    }

    public function results(Request $request)
    {
        $anagram = $request->anagram;
        $matches = Matcher::findWords($anagram);

        return view('index', ['anagram' => $anagram, 'matches' => $matches]);
    }
}
