<?php

namespace App\Http\Controllers;

use App\Models\Episode;
use App\Http\Requests\ProfileUpdateRequest;

use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;

use Conner\Tagging\Model\Tag;

class ProfileController extends Controller
{
    /**
     * Display the user page.
     */
    public function index(Request $request): \Illuminate\View\View
    {
        return view('profile.index', [
            'user' => $request->user(),
        ]);
    }

    /**
     * Display the user's settings form.
     */
    public function settings(Request $request): \Illuminate\View\View
    {
        $example = Episode::where('title', 'Succubus Yondara Gibo ga Kita!?')->first();

        return view('profile.settings', [
            'user'      => $request->user(),
            'example'   => $example,
        ]);
    }

    /**
     * Display the user's watched page.
     */
    public function watched(Request $request): \Illuminate\View\View
    {
        return view('profile.watched', [
            'user' => $request->user(),
        ]);
    }

    /**
     * Display the user's comments page.
     */
    public function comments(Request $request): \Illuminate\View\View
    {
        return view('profile.comments', [
            'user'      => $request->user(),
        ]);
    }

    /**
     * Display the user's likes page.
     */
    public function likes(Request $request): \Illuminate\View\View
    {
        return view('profile.likes', [
            'user'      => $request->user(),
        ]);
    }

    /**
     * Update user settings.
     */
    public function saveSettings(Request $request): \Illuminate\Http\RedirectResponse
    {
        $user = $request->user();
        $user->search_design = $request->input('searchDesign') == 'thumbnail';
        $user->home_top_design = $request->input('topDesign') == 'thumbnail';
        $user->home_middle_design = $request->input('middleDesign') == 'thumbnail';
        $user->save();

        return Redirect::route('profile.settings')->with('status', 'design-updated');
    }

    /**
     * Update user tag blacklist.
     */
    public function saveBlacklist(Request $request): \Illuminate\Http\RedirectResponse
    {
        $user = $request->user();
        $tags = json_decode($request->input('tags'));

        if (!$tags) {
            $user->tag_blacklist = null;
            $user->save();
            return Redirect::route('profile.settings')->with('status', 'blacklist-updated');
        }

        $blacklist = [];
        foreach ($tags as $tag) {
            $t = Tag::where('slug', Str::slug($tag->value))->firstOrFail();
            $blacklist[] = $t->slug;
        }

        $user->tag_blacklist = $blacklist;
        $user->save();

        return Redirect::route('profile.settings')->with('status', 'blacklist-updated');
    }

    /**
     * Delete the user's account.
     */
    public function destroy(Request $request): \Illuminate\Http\RedirectResponse
    {
        $request->validateWithBag('userDeletion', [
            'password' => ['required', 'current_password'],
        ]);

        $user = $request->user();

        Auth::logout();

        $user->delete();

        $request->session()->invalidate();
        $request->session()->regenerateToken();

        return Redirect::to('/');
    }
}
