<?php

namespace App\Http\Controllers;

use App\Models\publicPoetCategory;
use App\Models\publicPoetries;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class PublicPoetriesController extends Controller
{
    /**
     * Display a listing of folk poetries with filtering.
     */
    public function index(Request $request)
    {
        $query = publicPoetries::with('category');

        // Filter by category
        if ($request->filled('cat_id')) {
            $query->where('cat_id', $request->cat_id);
        }

        // Filter by title
        if ($request->filled('title')) {
            $query->where('title', 'like', '%' . $request->title . '%');
        }

        $data = $query->orderBy('id', 'desc')->paginate(20)->withQueryString();

        // For filter dropdown
        $categories = publicPoetCategory::all();

        return view('back-end.plpoetries.index', compact('data', 'categories'));
    }

    /**
     * Show form for creating new folk poetry.
     */
    public function create()
    {
        $categories = publicPoetCategory::all();
        return view('back-end.plpoetries.create', compact('categories'));
    }

    /**
     * Store a newly created folk poetry.
     */
    public function store(Request $request)
    {
        $request->validate([
            'title'   => 'required',
            'body'    => 'required',
            'cat_id'  => 'required|exists:public_poet_categories,id',
        ]);

        $poetry = new publicPoetries();
        $poetry->user_id = Auth::id();
        $poetry->title   = $request->title;
        $poetry->cat_id  = $request->cat_id;
        $poetry->body    = $request->body;
        $poetry->date    = date('Y-m-d');
        $poetry->save();

        session()->flash('msg', 'Poetry created successfully!');
        return redirect()->route('publicPoetries.index');
    }

    /**
     * Display the specified folk poetry.
     */
    public function show($id)
    {
        $data = publicPoetries::with('category')->findOrFail($id);
        return view('back-end.plpoetries.show', compact('data'));
    }

    /**
     * Show form for editing the specified folk poetry.
     */
    public function edit($id)
    {
        $data = publicPoetries::findOrFail($id);
        $categories = publicPoetCategory::all();
        return view('back-end.plpoetries.edit', compact('data', 'categories'));
    }

    /**
     * Update the specified folk poetry.
     */
    public function update(Request $request, $id)
    {
        $request->validate([
            'title'   => 'required',
            'body'    => 'required',
            'cat_id'  => 'required|exists:public_poet_categories,id',
        ]);

        $poetry = publicPoetries::findOrFail($id);
        $poetry->title   = $request->title;
        $poetry->cat_id  = $request->cat_id;
        $poetry->body    = $request->body;
        $poetry->save();

        session()->flash('msg', 'Poetry updated successfully!');
        return redirect()->route('publicPoetries.index');
    }

    /**
     * Delete the specified folk poetry.
     */
    public function delete($id)
    {
        $poetry = publicPoetries::findOrFail($id);
        $poetry->delete();
        session()->flash('msg', 'Poetry deleted!');
        return back();
    }
}
