<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class CinemaController extends Controller
{
    public function cinema()
    {
        $movies = DB::connection('cinema')
            ->table('imdb')
            ->select(
                'imdb.id',
                'imdb.imdb_id',
                'imdb.primary_title',
                'imdb.original_title',
                'imdb.start_year',
                'imdb.average_rating',
                'imdb.num_votes',
                'imdb.synopsis',
                'imdb.poster_url',
                'imdb.title_type',
                'imdb.runtime_minutes',
                'imdb.has_people'
            )
            ->orderBy('imdb.average_rating', 'desc')
            ->orderBy('imdb.num_votes', 'desc')
            ->whereNotNull('imdb.primary_title')
            ->whereNotNull('imdb.wiki_article')
            ->where('imdb.title_type', 'movie')
            ->simplePaginate(12);

        $total = DB::connection('cinema')
            ->table('imdb')
            ->whereNotNull('primary_title')
            ->whereNotNull('wiki_article')
            ->where('title_type', 'movie')
            ->count();

        return view('cinema', ['movies' => $movies, 'count' => $total]);
    }

    public function show($id)
    {
        $movie = DB::connection('cinema')
            ->table('imdb')
            ->where('imdb.imdb_id', $id)
            ->first();

        if (! $movie) {
            abort(404);
        }

        $genres = DB::connection('cinema')
            ->table('imdb_genre')
            ->join('genre', 'genre.id', '=', 'imdb_genre.genre_id')
            ->where('imdb_genre.imdb_id', $movie->id)
            ->pluck('genre.name');

        $casts = DB::connection('cinema')
            ->table('who')
            ->join('people', 'people.id', '=', 'who.people_id')
            ->join('profession', 'profession.id', '=', 'who.profession_id')
            ->where('who.imdb_id', $movie->id)
            ->select('people.name', 'profession.name as profession')
            ->get();

        $actors = $casts->where('profession', 'actor')->take(8);
        $directors = $casts->where('profession', 'director');
        $screenwriters = $casts->where('profession', 'screenwriter')->take(3);

        return view('cinema_show', [
            'movie' => $movie,
            'genres' => $genres,
            'actors' => $actors,
            'directors' => $directors,
            'screenwriters' => $screenwriters,
        ]);
    }

    public function genres()
    {
        $genres = DB::connection('cinema')
            ->table('genre')
            ->select('genre.name', DB::raw('COUNT(imdb_genre.imdb_id) as count'))
            ->join('imdb_genre', 'imdb_genre.genre_id', '=', 'genre.id')
            ->join('imdb', 'imdb.id', '=', 'imdb_genre.imdb_id')
            ->whereNotNull('imdb.primary_title')
            ->whereNotNull('imdb.wiki_article')
            ->where('imdb.title_type', 'movie')
            ->groupBy('genre.name')
            ->orderBy('count', 'desc')
            ->get();

        return view('genres', ['genres' => $genres]);
    }

    public function genre($name)
    {
        $genre = DB::connection('cinema')
            ->table('genre')
            ->where('name', $name)
            ->first();

        if (! $genre) {
            abort(404);
        }

        $movies = DB::connection('cinema')
            ->table('imdb')
            ->select(
                'imdb.id',
                'imdb.imdb_id',
                'imdb.primary_title',
                'imdb.original_title',
                'imdb.start_year',
                'imdb.average_rating',
                'imdb.num_votes',
                'imdb.synopsis',
                'imdb.poster_url',
                'imdb.title_type',
                'imdb.runtime_minutes'
            )
            ->join('imdb_genre', 'imdb_genre.imdb_id', '=', 'imdb.id')
            ->join('genre', 'genre.id', '=', 'imdb_genre.genre_id')
            ->where('genre.name', $name)
            ->whereNotNull('imdb.primary_title')
            ->whereNotNull('imdb.wiki_article')
            ->where('imdb.title_type', 'movie')
            ->orderBy('imdb.average_rating', 'desc')
            ->simplePaginate(12);

        $count = DB::connection('cinema')
            ->table('imdb')
            ->join('imdb_genre', 'imdb_genre.imdb_id', '=', 'imdb.id')
            ->join('genre', 'genre.id', '=', 'imdb_genre.genre_id')
            ->where('genre.name', $name)
            ->whereNotNull('imdb.primary_title')
            ->whereNotNull('imdb.wiki_article')
            ->where('imdb.title_type', 'movie')
            ->count();

        return view('genre_movies', ['movies' => $movies, 'count' => $count, 'genre_name' => $name]);
    }

    public function search(Request $request)
    {
        $query = $request->input('q', '');

        $movies = DB::connection('cinema')
            ->table('imdb')
            ->select(
                'imdb.id',
                'imdb.imdb_id',
                'imdb.primary_title',
                'imdb.original_title',
                'imdb.start_year',
                'imdb.average_rating',
                'imdb.num_votes',
                'imdb.synopsis',
                'imdb.poster_url',
                'imdb.title_type',
                'imdb.runtime_minutes'
            )
            ->whereNotNull('imdb.primary_title')
            ->whereNotNull('imdb.wiki_article')
            ->where('imdb.title_type', 'movie')
            ->where(function ($q) use ($query) {
                $q->where('imdb.primary_title', 'like', '%' . $query . '%')
                  ->orWhere('imdb.original_title', 'like', '%' . $query . '%')
                  ->orWhere('imdb.synopsis', 'like', '%' . $query . '%');
            })
            ->simplePaginate(12);

        $total = DB::connection('cinema')
            ->table('imdb')
            ->whereNotNull('primary_title')
            ->whereNotNull('wiki_article')
            ->where('title_type', 'movie')
            ->where(function ($q) use ($query) {
                $q->where('primary_title', 'like', '%' . $query . '%')
                  ->orWhere('original_title', 'like', '%' . $query . '%')
                  ->orWhere('synopsis', 'like', '%' . $query . '%');
            })
            ->count();

        return view('cinema', ['movies' => $movies, 'count' => $total, 'search_query' => $query]);
    }
}
