<?php

namespace App\Http\Controllers\Admin;

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

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

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

    public function store(Request $request)
    {
        $validated = $request->validate([
            'title' => 'required|string|max:255',
            'category' => 'required|string|max:100',
            'snippet' => 'required|string|max:300',
            'content' => 'required|string',
            'image' => 'nullable|string',
            'source_url' => 'nullable|string',
        ]);

        if (!empty($validated['image'])) {
            $originalImageInput = $validated['image'];
            $extracted = \App\Helpers\UrlImageExtractor::extract($originalImageInput);

            if (empty($validated['source_url']) && filter_var($originalImageInput, FILTER_VALIDATE_URL)) {
                if ($extracted !== $originalImageInput || empty($extracted)) {
                    $validated['source_url'] = $originalImageInput;
                }
            }

            $validated['image'] = $extracted;
        }

        $validated['slug'] = Str::slug($validated['title']) . '-' . time();
        if (empty($validated['image'])) {
            $validated['image'] = \App\Helpers\UrlImageExtractor::getThemeImage($validated['title'], $validated['category']);
        }

        Article::create($validated);

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

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

    public function update(Request $request, Article $article)
    {
        $validated = $request->validate([
            'title' => 'required|string|max:255',
            'category' => 'required|string|max:100',
            'snippet' => 'required|string|max:300',
            'content' => 'required|string',
            'image' => 'nullable|string',
            'source_url' => 'nullable|string',
        ]);

        if (!empty($validated['image'])) {
            $originalImageInput = $validated['image'];
            $extracted = \App\Helpers\UrlImageExtractor::extract($originalImageInput);

            if (empty($validated['source_url']) && filter_var($originalImageInput, FILTER_VALIDATE_URL)) {
                if ($extracted !== $originalImageInput || empty($extracted)) {
                    $validated['source_url'] = $originalImageInput;
                }
            }

            $validated['image'] = $extracted;
        }

        if (empty($validated['image'])) {
            $validated['image'] = \App\Helpers\UrlImageExtractor::getThemeImage($validated['title'], $validated['category']);
        }

        if ($article->title !== $validated['title']) {
            $validated['slug'] = Str::slug($validated['title']) . '-' . time();
        }

        $article->update($validated);

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

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