<?php

namespace App\Http\Controllers;

use App\Models\Poetries;
use App\Models\Poet;
use App\Models\systemAdministration\category;
use App\Models\systemAdministration\subCategory;
use App\Models\Alphabet;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class PoetriesController extends Controller
{
    /**
     * Display a listing of poetries with optional filtering.
     */
    public function index(Request $request)
    {
        $query = Poetries::with(['poet', 'category', 'sub_category']);

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

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

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

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

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

        // For filter dropdowns
        $poets = Poet::all();
        $categories = Category::all();
        $alphabets = Alphabet::all();

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

    /**
     * Show form for creating new poetry.
     */
    public function create()
    {
        $poets = Poet::all();
        $categories = Category::all();
        $subCategories = SubCategory::all();
        $alphabets = Alphabet::all();

        return view('back-end.poetries.create', compact('poets', 'categories', 'subCategories', 'alphabets'));
    }

    /**
     * Store a newly created poetry.
     */
    public function store(Request $request)
    {
        $request->validate([
            'title'    => 'required',
            'body'     => 'required',
            'cat_id'   => 'required|exists:categories,id',
            'poet_id'  => 'required|exists:poets,id',
            'alphabit' => 'nullable|exists:alphabets,name',
            'scat_id'  => 'nullable|exists:sub_categories,id',
        ]);

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

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

    /**
     * Display the specified poetry.
     */
    public function show($id)
    {
        $data = Poetries::with(['poet', 'category', 'sub_category', 'poet.centry'])->findOrFail($id);
        return view('back-end.poetries.show', compact('data'));
    }

    /**
     * Show form for editing the specified poetry.
     */
    public function edit($id)
    {
        $data = Poetries::findOrFail($id);
        $poets = Poet::all();
        $categories = Category::all();
        $subCategories = SubCategory::all();
        $alphabets = Alphabet::all();

        return view('back-end.poetries.edit', compact('data', 'poets', 'categories', 'subCategories', 'alphabets'));
    }

    /**
     * Update the specified poetry.
     */
    public function update(Request $request, $id)
    {
        $request->validate([
            'title'    => 'required',
            'body'     => 'required',
            'cat_id'   => 'required|exists:categories,id',
            'poet_id'  => 'required|exists:poets,id',
            'alphabit' => 'nullable|exists:alphabets,name',
            'scat_id'  => 'nullable|exists:sub_categories,id',
        ]);

        $poetry = Poetries::findOrFail($id);
        $poetry->title     = $request->title;
        $poetry->poet_id   = $request->poet_id;
        $poetry->cat_id    = $request->cat_id;
        $poetry->scat_id   = $request->scat_id;
        $poetry->alphabit  = $request->alphabit;
        $poetry->body      = $request->body;
        $poetry->save();

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

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

    /**
     * AJAX: Get subcategories for a given category.
     */
    public function getSubCategories(Request $request)
    {
        $subs = SubCategory::where('category_id', $request->cat_id)->get();
        return response()->json($subs);
    }
}
