<?php

namespace App\Http\Controllers\Controllers;

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

class NewsAdminController extends Controller
{
    public function index()
    {
        // Get all news for the table
        $news = News::orderBy('created_at', 'desc')->get();

        return view('admin.features.berita.show-berita', compact('news'));
    }

    public function create()
    {
        return view('admin.features.berita.tambah-berita');
    }

    public function store(Request $request)
    {
        // Validate request
        $request->validate([
            'title' => 'required|string|max:255',
            'slug' => 'nullable|string|max:255|unique:news,slug',
            'content' => 'required|string',
            'category' => 'required|string|max:100',
            'status' => 'required|in:draft,published,archived',
            'image' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
        ]);

        // Generate slug if not provided
        if (empty($request->slug)) {
            $request->merge(['slug' => Str::slug($request->title)]);
        }

        // Handle file upload
        $image = null;
        if ($request->hasFile('image')) {
            $image = $request->file('image')->store('news', 'public');
        }

        // Create news
        News::create([
            'title' => $request->title,
            'slug' => $request->slug,
            'content' => $request->content,
            'category' => $request->category,
            'status' => $request->status,
            'image' => $image,
        ]);

        return redirect()->route('admin.berita.index')
            ->with('success', 'Berita berhasil ditambahkan!');
    }

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

    public function update(Request $request, $id)
    {
        $news = News::findOrFail($id);

        // Validate request
        $request->validate([
            'title' => 'required|string|max:255',
            'slug' => 'nullable|string|max:255|unique:news,slug,' . $id,
            'content' => 'required|string',
            'category' => 'required|string|max:100',
            'status' => 'required|in:draft,published,archived',
            'image' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
        ]);

        // Generate slug if not provided
        if (empty($request->slug)) {
            $request->merge(['slug' => Str::slug($request->title)]);
        }

        // Handle file upload
        $image = $news->image;
        if ($request->hasFile('image')) {
            // Delete old image if exists
            if ($news->image) {
                Storage::disk('public')->delete($news->image);
            }
            $image = $request->file('image')->store('news', 'public');
        }

        // Update news
        $news->update([
            'title' => $request->title,
            'slug' => $request->slug,
            'content' => $request->content,
            'category' => $request->category,
            'status' => $request->status,
            'image' => $image,
        ]);

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

    public function destroy($id)
    {
        $news = News::findOrFail($id);

        // Delete image if exists
        if ($news->image) {
            Storage::disk('public')->delete($news->image);
        }

        $news->delete();

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