<?php

declare(strict_types=1);

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\CannedResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class CannedResponseController extends Controller
{
    public function index(Request $request): Response
    {
        $query = CannedResponse::query()
            ->with('creator:id,name');

        if ($search = $request->input('search')) {
            $query->where(function ($q) use ($search): void {
                $q->where('title', 'like', "%{$search}%")
                    ->orWhere('content', 'like', "%{$search}%");
            });
        }

        if ($category = $request->input('category')) {
            $query->where('category', $category);
        }

        $cannedResponses = $query->latest()
            ->paginate(25)
            ->withQueryString();

        $categories = CannedResponse::query()
            ->whereNotNull('category')
            ->distinct()
            ->pluck('category')
            ->sort()
            ->values();

        return Inertia::render('Admin/Tickets/CannedResponses/Index', [
            'cannedResponses' => $cannedResponses,
            'categories' => $categories,
            'filters' => [
                'search' => $request->input('search', ''),
                'category' => $request->input('category', ''),
            ],
        ]);
    }

    public function create(): Response
    {
        $categories = CannedResponse::query()
            ->whereNotNull('category')
            ->distinct()
            ->pluck('category')
            ->sort()
            ->values();

        return Inertia::render('Admin/Tickets/CannedResponses/Create', [
            'categories' => $categories,
        ]);
    }

    public function store(Request $request): RedirectResponse
    {
        $validated = $request->validate([
            'title' => ['required', 'string', 'max:255'],
            'content' => ['required', 'string', 'max:10000'],
            'category' => ['nullable', 'string', 'max:100'],
            'is_shared' => ['boolean'],
        ]);

        CannedResponse::query()->create([
            ...$validated,
            'created_by' => $request->user()->id,
        ]);

        return redirect()->route('admin.canned-responses.index')
            ->with('success', 'Canned response created successfully.');
    }

    public function edit(CannedResponse $cannedResponse): Response
    {
        $categories = CannedResponse::query()
            ->whereNotNull('category')
            ->distinct()
            ->pluck('category')
            ->sort()
            ->values();

        return Inertia::render('Admin/Tickets/CannedResponses/Edit', [
            'cannedResponse' => $cannedResponse,
            'categories' => $categories,
        ]);
    }

    public function update(Request $request, CannedResponse $cannedResponse): RedirectResponse
    {
        $validated = $request->validate([
            'title' => ['required', 'string', 'max:255'],
            'content' => ['required', 'string', 'max:10000'],
            'category' => ['nullable', 'string', 'max:100'],
            'is_shared' => ['boolean'],
        ]);

        $cannedResponse->update($validated);

        return redirect()->route('admin.canned-responses.index')
            ->with('success', 'Canned response updated successfully.');
    }

    public function destroy(CannedResponse $cannedResponse): RedirectResponse
    {
        $cannedResponse->delete();

        return redirect()->route('admin.canned-responses.index')
            ->with('success', 'Canned response deleted successfully.');
    }
}
