<?php

namespace App\Http\Controllers\Main;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class MovieController extends Controller
{
    public function showAllMovies()
    {
        return view('main.movies.index', ['title' => "Movies", 'movies' => \App\Models\Movie::all(), 'genres' => \App\Models\Genre::all(), 'showings' => \App\Models\Showing::all()]);
    }

    public function moviesNowShowing()
    {
        // map showings that are in the future to movies
        $showings = \App\Models\Showing::all()->filter(function ($showing) {
            return $showing->showing_start > now();
        });
        // $movies must be a collection of unique movies
        $movies = collect();
        foreach ($showings as $showing) {
            if (!$movies->contains($showing->movie)) {
                $movies->push($showing->movie);
            }
        }
        return view('main.movies.index', ['title' => "Movies Now Showing", 'movies' => $movies, 'genres' => \App\Models\Genre::all(), 'showings' => \App\Models\Showing::all()]);
    }

    public function show($id)
    {
        return view('main.movies.movie', ['title' => "Movie", 'movie' => \App\Models\Movie::findOrfail($id)]);
    }
}