<?php

namespace App\Http\Controllers\Admin;

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

class ArticleController extends Controller
{
    public function index()
    {
        $articles = Article::ordered()->paginate(20);
        return view('admin.articles.index', compact('articles'));
    }

    public function create()
    {
        return view('admin.articles.create');
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'title'    => 'required|string|max:255',
            'slug'     => 'nullable|string|max:255|unique:articles',
            'content'  => 'nullable|string',
            'category' => 'nullable|string|max:100',
            'order'    => 'nullable|integer|min:0',
            'is_active'=> 'nullable|boolean',
        ]);

        $data['is_active'] = $request->boolean('is_active', true);

        Article::create($data);
        return redirect()->route('admin.articles.index')->with('success', 'Article created successfully.');
    }

    public function edit(Article $article)
    {
        return view('admin.articles.edit', compact('article'));
    }

    public function update(Request $request, Article $article)
    {
        $data = $request->validate([
            'title'    => 'required|string|max:255',
            'slug'     => 'nullable|string|max:255|unique:articles,slug,' . $article->id,
            'content'  => 'nullable|string',
            'category' => 'nullable|string|max:100',
            'order'    => 'nullable|integer|min:0',
            'is_active'=> 'nullable|boolean',
        ]);

        $data['is_active'] = $request->boolean('is_active', true);

        $article->update($data);
        return redirect()->route('admin.articles.index')->with('success', 'Article updated successfully.');
    }

    public function destroy(Article $article)
    {
        $article->delete();
        return redirect()->route('admin.articles.index')->with('success', 'Article deleted successfully.');
    }
}
