<?php

namespace FluxCms\Core\Http\Controllers\Admin;

use FluxCms\Core\Models\BlogPost;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class BlogController extends Controller
{
    public function __construct()
    {
        $this->middleware('can:flux-cms.view');
    }

    public function index(Request $request)
    {
        $query = BlogPost::with(['author']);

        if ($request->has('status')) {
            switch ($request->status) {
                case 'published':
                    $query->published();
                    break;
                case 'draft':
                    $query->where('is_published', false);
                    break;
            }
        }

        $posts = $query->orderBy('updated_at', 'desc')->paginate(20);

        return view('flux-cms::admin.blog.index', compact('posts'));
    }

    public function edit(BlogPost $blogPost)
    {
        $this->authorize('flux-cms.edit');

        return view('flux-cms::admin.blog.edit', ['post' => $blogPost]);
    }
}
