<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\News;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class NewsController extends Controller
{
    public function index(Request $request)
    {
        $news = News::when($request->search, fn($q) =>
                    $q->where('title', 'like', "%{$request->search}%")
                )
                ->when($request->category, fn($q) =>
                    $q->where('category', $request->category)
                )
                ->latest()
                ->paginate(12);

        return view('admin.news.index', compact('news'));
    }

    public function create()
    {
        return view('admin.news.form', ['news' => new News()]);
    }

    public function store(Request $request)
    {
        $data = $this->validateNews($request);
        $data['slug'] = Str::slug($data['title']) . '-' . time();
        if ($request->hasFile('cover_image')) {
            $data['cover_image'] = $request->file('cover_image')->store('news', 'public');
        }
        News::create($data);
        return redirect()->route('admin.news.index')->with('success', 'تم إضافة الخبر');
    }

    public function edit($id)
    {
        $news = News::findOrFail($id);
        return view('admin.news.form', compact('news'));
    }

    public function update(Request $request, $id)
    {
        $news = News::findOrFail($id);
        $data = $this->validateNews($request);
        if ($request->hasFile('cover_image')) {
            if ($news->cover_image) Storage::disk('public')->delete($news->cover_image);
            $data['cover_image'] = $request->file('cover_image')->store('news', 'public');
        }
        $news->update($data);
        return redirect()->route('admin.news.index')->with('success', 'تم تحديث الخبر');
    }

    public function destroy($id)
    {
        $news = News::findOrFail($id);
        if ($news->cover_image) Storage::disk('public')->delete($news->cover_image);
        $news->delete();
        return back()->with('success', 'تم حذف الخبر');
    }

    public function publish($id)
    {
        $news = News::findOrFail($id);
        $news->update([
            'is_published' => ! $news->is_published,
            'published_at' => $news->is_published ? null : now(),
        ]);
        return back()->with('success', $news->is_published ? 'تم نشر الخبر' : 'تم إلغاء نشر الخبر');
    }

    private function validateNews(Request $request): array
    {
        return $request->validate([
            'title'       => 'required|string|max:255',
            'title_ar'    => 'nullable|string|max:255',
            'excerpt'     => 'nullable|string|max:500',
            'content'     => 'required|string',
            'cover_image' => 'nullable|image|max:3072',
            'category'    => 'nullable|string|max:50',
            'source'      => 'nullable|string|max:100',
            'is_published'=> 'boolean',
        ]);
    }
}
