<?php

namespace App\Http\Controllers;

use App\Models\OfficialPost;
use Illuminate\Http\Request;

class OfficialPostController extends Controller
{
    /**
     * Frontend - list posts
     */
    public function index()
    {
        // paginate so frontend doesn't load everything at once
        $posts = OfficialPost::latest()->paginate(10);
        return view('app.main.official.index', compact('posts'));
    }

    /**
     * Frontend - show single post
     * Route-model binding gives us the post instance.
     */
    public function show(OfficialPost $officialPost)
    {
        // use variable name $post in view for consistency
        $post = $officialPost;
        return view('app.main.official.show', compact('post'));
    }

    /**
     * AJAX: increment likes
     * Uses route-model binding for safety.
     */
    public function like(OfficialPost $officialPost)
    {
        // Increment atomically
        $officialPost->increment('likes');

        // Return new count
        return response()->json(['likes' => $officialPost->likes]);
    }

    /**
     * Admin list
     */
    public function adminIndex()
    {
        $posts = OfficialPost::latest()->paginate(10);
        return view('admin.official.index', compact('posts'));
    }

    /**
     * Admin create view
     */
    public function create()
    {
        return view('admin.official.create');
    }

    /**
     * Admin store
     */
    public function store(Request $request)
    {
        $data = $request->validate([
            'title'   => 'required|string|max:255',
            'content' => 'required',
            'images'  => 'nullable|array',
            'images.*'=> 'nullable|string',
            'link'    => 'nullable|string',
        ]);

        // sanitize images: remove empty values and reindex
        if (!empty($data['images']) && is_array($data['images'])) {
            $data['images'] = array_values(array_filter($data['images'], function ($v) {
                return !empty($v);
            }));
        } else {
            $data['images'] = null;
        }

        OfficialPost::create($data);

        return redirect()->route('admin.official.index')->with('success', 'Post created successfully');
    }

    /**
     * Admin edit view
     */
    public function edit(OfficialPost $officialPost)
    {
        return view('admin.official.edit', compact('officialPost'));
    }

    /**
     * Admin update
     */
    public function update(Request $request, OfficialPost $officialPost)
    {
        $data = $request->validate([
            'title'   => 'required|string|max:255',
            'content' => 'required',
            'images'  => 'nullable|array',
            'images.*'=> 'nullable|string',
            'link'    => 'nullable|string',
        ]);

        if (!empty($data['images']) && is_array($data['images'])) {
            $data['images'] = array_values(array_filter($data['images'], function ($v) {
                return !empty($v);
            }));
        } else {
            $data['images'] = null;
        }

        $officialPost->update($data);

        return redirect()->route('admin.official.index')->with('success', 'Post updated successfully');
    }

    /**
     * Admin delete
     */
    public function destroy(OfficialPost $officialPost)
    {
        $officialPost->delete();
        return redirect()->route('admin.official.index')->with('success', 'Post deleted successfully');
    }
}
