<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\StoreCaseRequest;
use App\Http\Requests\Admin\UpdateCaseRequest;
use App\Models\Cases;
use App\Services\Admin\CaseService;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

// ══════════════════════════════════════════════════════════════════
//  InPractice — Admin\CaseController
//  Location: app/Http/Controllers/Admin/AdminCaseController.php
//
//  Thin controller — follows the Project Bible strictly.
//  Every method: receive → call service → return response.
//  Zero business logic here.
// ══════════════════════════════════════════════════════════════════

class AdminCaseController extends Controller
{
    public function __construct(
        private readonly CaseService $caseService
    ) {}

    /**
     * List all cases with filters.
     * GET /admin/cases
     */
    public function index(Request $request): Response
    {
        $data = $this->caseService->index($request->only([
            'hub_id', 'status', 'difficulty', 'search',
        ]));

        return Inertia::render('Admin/Cases/Index', $data);
    }

    /**
     * Show the create form.
     * GET /admin/cases/create
     */
    public function create(): Response
    {
        return Inertia::render('Admin/Cases/Form', $this->caseService->create());
    }

    /**
     * Store a new case.
     * POST /admin/cases
     */
    public function store(StoreCaseRequest $request): RedirectResponse
    {
        $case = $this->caseService->store($request->validated(), auth()->id());

        return redirect()
            ->route('admin.cases.index')
            ->with('success', __('cases.case_created'));
    }

    /**
     * Show the edit form.
     * GET /admin/cases/{case}/edit
     */
    public function edit(Cases $case): Response
    {
        return Inertia::render('Admin/Cases/Form', $this->caseService->edit($case));
    }

    /**
     * Update an existing case.
     * PUT /admin/cases/{case}
     */
    public function update(UpdateCaseRequest $request, Cases $case): RedirectResponse
    {
        $this->caseService->update($case, $request->validated());

        return redirect()
            ->route('admin.cases.index')
            ->with('success', __('cases.case_updated'));
    }

    /**
     * Publish a case — makes it visible to members.
     * PATCH /admin/cases/{case}/publish
     */
    public function publish(Cases $case): RedirectResponse
    {
        $this->caseService->publish($case);

        return back()->with('success', __('cases.case_published'));
    }

    /**
     * Archive a case.
     * PATCH /admin/cases/{case}/archive
     */
    public function archive(Cases $case): RedirectResponse
    {
        $this->caseService->archive($case);

        return back()->with('success', __('cases.case_archived'));
    }

    /**
     * Hard delete a case.
     * DELETE /admin/cases/{case}
     */
    public function destroy(Cases $case): RedirectResponse
    {
        $this->caseService->destroy($case);

        return redirect()
            ->route('admin.cases.index')
            ->with('success', __('cases.case_deleted'));
    }
}
