<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class InterviewController extends Controller
{
    public function show(Request $request, $id, $slug = null)
    {
        $id = (int) $id;

        if ($request->isMethod('post')) {
            $action = $request->input('action');
            if ($action === 'store' && (!empty($_SESSION['web_login']['user_type']) && $_SESSION['web_login']['user_type'] > 1)) {
                $comment = $request->input('comment');
                $tekst = nl2br(htmlspecialchars($comment ?? '', ENT_QUOTES, 'UTF-8'));
                $interviewId = (int) $request->input('interview_id');

                try {
                    DB::table('interviews_comment')->insert([
                        'nid' => $interviewId,
                        'author' => $_SESSION['web_login']['username'] ?? 'Anonymous',
                        'datum' => DB::raw('CURRENT_TIMESTAMP'),
                        'tekst' => $tekst,
                    ]);

                    $ar2 = DB::table('users')
                        ->where('uname', $_SESSION['web_login']['username'])
                        ->first();

                    if (!empty($ar2->user_id)) {
                        DB::table('users_statistics')
                            ->where('user_id', $ar2->user_id)
                            ->increment('newscomment');
                    }
                } catch (\Throwable $e) {
                    // fail silently
                }
            }
        }

        try {
            $ar = DB::table('interviews')->where('id', $id)->first();
        } catch (\Throwable $e) {
            $ar = null;
        }

        if (! $ar) {
            return redirect('/interviews');
        }

        try {
            $artworks = DB::table('wallz')
                ->where('uname', $ar->username)
                ->inRandomOrder()
                ->limit(2)
                ->get();
        } catch (\Throwable $e) {
            $artworks = collect();
        }

        try {
            $comments = DB::table('interviews_comment as c')
                ->leftJoin('users as u', 'u.uname', '=', 'c.author')
                ->where('c.nid', $id)
                ->select('c.*', 'u.user_id', 'u.user_type', 'u.signature', 'u.icon')
                ->orderBy('c.datum')
                ->get();
        } catch (\Throwable $e) {
            $comments = collect();
        }

        $authors = $comments->pluck('author')->unique()->values()->all();
        $postCounts = [];
        if (!empty($authors)) {
            try {
                $counts = DB::table('interviews_comment')
                    ->select('author', DB::raw('COUNT(*) as cnt'))
                    ->whereIn('author', $authors)
                    ->groupBy('author')
                    ->get();

                foreach ($counts as $c) {
                    $postCounts[$c->author] = $c->cnt;
                }
            } catch (\Throwable $e) {
            }
        }

        $page_title = 'Interview with ' . ($ar->username ?? '');

        return view('legacy::interview', [
            'ar' => $ar,
            'artworks' => $artworks,
            'comments' => $comments,
            'postCounts' => $postCounts,
            'page_title' => $page_title,
        ]);
    }
}
