<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\BlogComment;
use App\Models\BlogPost;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\JsonResponse;

class BlogCommentController extends Controller
{
    public function index(Request $request): View
    {
        $query = BlogComment::with(['post', 'user', 'parent'])
            ->latest();

        if ($request->has('status')) {
            if ($request->status === 'pending') {
                $query->where('is_approved', false);
            } elseif ($request->status === 'approved') {
                $query->where('is_approved', true);
            }
        }

        if ($request->has('post_id')) {
            $query->where('post_id', $request->post_id);
        }

        $comments = $query->paginate(20);

        return view('admin.blog-comments.index', compact('comments'));
    }

    public function show(BlogComment $comment): View
    {
        $comment->load(['post', 'user', 'parent', 'replies']);
        return view('admin.blog-comments.show', compact('comment'));
    }

    public function edit(BlogComment $comment): View
    {
        $comment->load(['post', 'user', 'parent']);
        return view('admin.blog-comments.edit', compact('comment'));
    }

    public function update(Request $request, BlogComment $comment): RedirectResponse
    {
        $validated = $request->validate([
            'content'     => 'required|string',
            'is_approved' => 'boolean',
        ]);

        $validated['is_approved'] = $request->boolean('is_approved', $comment->is_approved);

        $comment->update($validated);

        return redirect()->route('admin.blog-comments.show', $comment)
            ->with('success', 'Yorum güncellendi.');
    }

    public function destroy(BlogComment $comment): RedirectResponse
    {
        $comment->delete();
        return redirect()->route('admin.blog-comments.index')
            ->with('success', 'Yorum silindi.');
    }

    /**
     * Approve a comment
     */
    public function approve(BlogComment $comment): RedirectResponse
    {
        $comment->update(['is_approved' => true]);
        return redirect()->back()
            ->with('success', 'Yorum onaylandı.');
    }

    /**
     * Reject (unapprove) a comment
     */
    public function reject(BlogComment $comment): RedirectResponse
    {
        $comment->update(['is_approved' => false]);
        return redirect()->back()
            ->with('success', 'Yorum reddedildi.');
    }

    /**
     * Store admin reply to a comment
     */
    public function reply(Request $request, BlogComment $comment): RedirectResponse
    {
        $validated = $request->validate([
            'content' => 'required|string|max:2000',
        ]);

        BlogComment::create([
            'post_id'     => $comment->post_id,
            'user_id'     => auth()->id(),
            'parent_id'   => $comment->id,
            'content'     => $validated['content'],
            'is_approved' => true, // Admin replies are auto-approved
            'author_name' => null,
            'author_email' => null,
            'author_ip'   => $request->ip(),
        ]);

        return redirect()->route('admin.blog-comments.show', $comment)
            ->with('success', 'Cevabınız eklendi.');
    }

    /**
     * Bulk approve comments
     */
    public function bulkApprove(Request $request): RedirectResponse
    {
        $validated = $request->validate([
            'comment_ids'   => 'required|array',
            'comment_ids.*' => 'exists:blog_comments,id',
        ]);

        BlogComment::whereIn('id', $validated['comment_ids'])
            ->update(['is_approved' => true]);

        return redirect()->back()
            ->with('success', count($validated['comment_ids']) . ' yorum onaylandı.');
    }

    /**
     * Bulk delete comments
     */
    public function bulkDelete(Request $request): RedirectResponse
    {
        $validated = $request->validate([
            'comment_ids'   => 'required|array',
            'comment_ids.*' => 'exists:blog_comments,id',
        ]);

        BlogComment::whereIn('id', $validated['comment_ids'])
            ->delete();

        return redirect()->back()
            ->with('success', count($validated['comment_ids']) . ' yorum silindi.');
    }

    /**
     * Get pending comments count for dashboard
     */
    public function pendingCount(): JsonResponse
    {
        $count = BlogComment::where('is_approved', false)->count();
        return response()->json(['count' => $count]);
    }
}
