<?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::with('user')->latest()->get();

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

    public function store(Request $request)
    {
        $request->validate([
            'title' => 'required|max:255',
            'slug' => 'required|unique:articles,slug',
            'content' => 'required',
            'image' => 'nullable|image|mimes:jpg,png,jpeg|max:2048',
            'status' => 'required',
            'source_url' => 'nullable|url'
        ]);

        $data = $request->all();
        $data['user_id'] = auth()->id(); // Penulis otomatis diambil dari admin yang login

        // Logika upload gambar jika ada
        if ($request->hasFile('image')) {
            $data['image'] = $request->file('image')->store('assets/article', 'public');
        }
        

        Article::create($data);

        return redirect()->route('admin.articles.index')->with('success', 'Artikel berhasil diterbitkan!');
    }

    // Fungsi untuk menampilkan halaman form edit
    public function edit(Article $article)
    {
        return view('admin.articles.edit', compact('article'));
    }

    // Fungsi untuk memproses perubahan data ke database
    public function update(Request $request, Article $article)
    {
        $request->validate([
            'title' => 'required|max:255',
            'slug' => 'required|unique:articles,slug,' . $article->id,
            'content' => 'required',
            'image' => 'nullable|image|mimes:jpg,png,jpeg|max:2048',
            'status' => 'required|in:aktif,non-aktif',
            'source_url' => 'nullable|url',
        ]);

        $data = $request->all();

        if ($request->hasFile('image')) {
            // Hapus foto lama jika ada foto baru yang diupload
            if ($article->image) {
                \Storage::disk('public')->delete($article->image);
            }
            $data['image'] = $request->file('image')->store('assets/article', 'public');
        }

        $article->update($data);

        return redirect()->route('admin.articles.index')->with('success', 'Artikel berhasil diperbarui!');
    }

    public function destroy(Article $article)
    {
        // Hapus foto artikel jika ada
        if ($article->image) {
            \Storage::disk('public')->delete($article->image);
        }

        $article->delete();

        return redirect()->route('admin.articles.index')->with('success', 'Artikel berhasil dihapus!');
    }

    public function show($slug)
    {
        // Cari artikel berdasarkan slug yang statusnya aktif
        $article = Article::where('slug', $slug)->where('status', 'aktif')->firstOrFail();
        
        // Ambil artikel lain untuk rekomendasi di samping/bawah (opsional)
        $otherArticles = Article::where('status', 'aktif')
                                ->where('id', '!=', $article->id)
                                ->latest()
                                ->take(3)
                                ->get();

        return view('articles.show', compact('article', 'otherArticles'));
    }
    
    public function indexFrontend()
    {
        // Mengambil 8 artikel per halaman yang statusnya aktif
        $articles = Article::where('status', 'aktif')->latest()->paginate(8);
        
        return view('articles.index', compact('articles'));
    }
}