<?php

namespace App\Http\Controllers\Admin;

use App\Enums\FeedbackStatus;
use App\Enums\FeedbackType;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Feedback\FeedbackRequest;
use App\Http\Requests\Admin\Feedback\UpdateFeedbackStatusRequest;
use App\Http\Requests\PaginatedRequest;
use App\Models\Feedback;
use App\Services\Admin\FeedbackService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class FeedbackController extends Controller
{
    public function __construct(
        private readonly FeedbackService $service,
    ) {}

    public function index(PaginatedRequest $request): Response
    {
        return Inertia::render('admin/feedback/index', [
            'feedbacks' => $this->service->paginated(
                $request->user(),
                ...$request->validatedWithDefaults(),
                type: $request->validated('type'),
                status: $request->validated('status'),
            ),
            'types' => FeedbackType::options(),
            'statuses' => FeedbackStatus::options(),
            'filters' => $request->only(['type', 'status']),
        ]);
    }

    public function create(): Response
    {
        return Inertia::render('admin/feedback/create', [
            'types' => FeedbackType::options(),
        ]);
    }

    public function store(FeedbackRequest $request): RedirectResponse
    {
        $this->service->create($request->user(), $request->validated());

        Inertia::flash('toast', ['type' => 'success', 'message' => 'Masukan berhasil dikirim.']);

        return to_route('admin.feedback.index');
    }

    public function edit(Request $request, Feedback $feedback): Response
    {
        abort_unless($feedback->user_id === $request->user()->id, 403);

        return Inertia::render('admin/feedback/edit', [
            'feedback' => $feedback,
            'types' => FeedbackType::options(),
        ]);
    }

    public function update(FeedbackRequest $request, Feedback $feedback): RedirectResponse
    {
        abort_unless($feedback->user_id === $request->user()->id, 403);

        $this->service->update($feedback, $request->validated());

        Inertia::flash('toast', ['type' => 'success', 'message' => 'Masukan berhasil diperbarui.']);

        return to_route('admin.feedback.index');
    }

    public function updateStatus(UpdateFeedbackStatusRequest $request, Feedback $feedback): RedirectResponse
    {
        $this->service->updateStatus($feedback, $request->validated('status'));

        return Inertia::flash('toast', ['type' => 'success', 'message' => 'Status masukan berhasil diperbarui.'])->back();
    }

    public function destroy(Request $request, Feedback $feedback): RedirectResponse
    {
        abort_unless($feedback->user_id === $request->user()->id, 403);

        $this->service->delete($feedback);

        return Inertia::flash('toast', ['type' => 'success', 'message' => 'Masukan berhasil dihapus.'])->back();
    }
}
