<?php

namespace App\Http\Controllers;

use App\Models\Project;
use Illuminate\Http\Request;

class ProjectController extends Controller
{
    public function index(Request $request)
    {
        $industry = $request->query('industry');

        $projects = Project::published()
            ->when($industry, fn ($q) => $q->where('industry', $industry))
            ->latest('year')
            ->paginate(9)
            ->withQueryString();

        return view('pages.projects.index', [
            'projects'     => $projects,
            'industries'   => config('brokam.industries'),
            'activeFilter' => $industry,
        ]);
    }

    public function show(Project $project)
    {
        abort_unless($project->published, 404);

        $more = Project::published()
            ->where('id', '!=', $project->id)
            ->latest('year')
            ->take(3)
            ->get();

        return view('pages.projects.show', [
            'project' => $project,
            'more'    => $more,
        ]);
    }
}
