<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Article;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Storage;

class ArticleController extends Controller
{
    // Menampilkan daftar artikel di panel Admin
    public function index()
    {
        $articles = Article::orderBy('created_at', 'desc')->get();
        return view('admin.article_index', compact('articles'));
    }

    // Menampilkan form tambah artikel
    public function create()
    {
        return view('admin.article_form');
    }

    // Menyimpan artikel baru
    public function store(Request $request)
    {
        // Validasi input form, termasuk keyword SEO
        $request->validate([
            'title' => 'required|string|max:255',
            'meta_title' => 'nullable|string|max:255',
            'meta_description' => 'nullable|string|max:500',
            'focus_keyword' => 'nullable|string|max:255',
            'secondary_keywords' => 'nullable|string|max:255',
            'tags' => 'nullable|string|max:255',
            'content' => 'required',
            'status' => 'required|in:draft,published',
            'image' => 'nullable|image|mimes:jpeg,png,jpg|max:2048'
        ]);

        $imagePath = null;
        if ($request->hasFile('image')) {
            $imagePath = $request->file('image')->store('articles', 'public');
        }

        // Menyimpan data ke database
        Article::create([
            'title' => $request->title,
            'slug' => Str::slug($request->title . '-' . time()),
            'meta_title' => $request->meta_title,
            'meta_description' => $request->meta_description,
            'focus_keyword' => $request->focus_keyword,
            'secondary_keywords' => $request->secondary_keywords,
            'tags' => $request->tags,
            'content' => $request->content,
            'status' => $request->status,
            'image' => $imagePath,
        ]);

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

    // Menampilkan form edit artikel
    public function edit($id)
    {
        $article = Article::findOrFail($id);
        return view('admin.article_form', compact('article'));
    }

    // Menyimpan perubahan artikel
    public function update(Request $request, $id)
    {
        $article = Article::findOrFail($id);

        // Validasi input form saat update
        $request->validate([
            'title' => 'required|string|max:255',
            'meta_title' => 'nullable|string|max:255',
            'meta_description' => 'nullable|string|max:500',
            'focus_keyword' => 'nullable|string|max:255',
            'secondary_keywords' => 'nullable|string|max:255',
            'tags' => 'nullable|string|max:255',
            'content' => 'required',
            'status' => 'required|in:draft,published',
            'image' => 'nullable|image|mimes:jpeg,png,jpg|max:2048'
        ]);

        if ($request->hasFile('image')) {
            // Hapus gambar lama
            if ($article->image) {
                Storage::disk('public')->delete($article->image);
            }
            $article->image = $request->file('image')->store('articles', 'public');
        }

        // Memperbarui atribut artikel
        $article->title = $request->title;
        $article->slug = Str::slug($request->title . '-' . time());
        $article->meta_title = $request->meta_title;
        $article->meta_description = $request->meta_description;
        $article->focus_keyword = $request->focus_keyword;
        $article->secondary_keywords = $request->secondary_keywords;
        $article->tags = $request->tags;
        $article->content = $request->content;
        $article->status = $request->status;
        $article->save();

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

    // Menghapus artikel
    public function destroy($id)
    {
        $article = Article::findOrFail($id);
        
        // Hapus gambar dari server jika ada
        if ($article->image) {
            Storage::disk('public')->delete($article->image);
        }
        
        $article->delete();
        
        return redirect()->back()->with('success', 'Artikel dihapus!');
    }
}