<?php

declare(strict_types=1);

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\ReserveRequest;
use App\Services\ReserveService;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

final class ReservesController extends Controller
{
    public function __construct(
        private readonly ReserveService $reserveService
    ) {
    }

    /**
     * 予約一覧（検索・並び替え・ページング）
     * Why: Controller を薄くし、検索条件・クエリ構築は Service に集約する。
     */
    public function index(ReserveRequest $request): View
    {
        $result = $this->reserveService->paginate($request->payload());

        return view('admin.reserves.index', $result);
    }

    /**
     * 新規（画面）
     * Why: プルダウン等の初期表示データ取得は Service に寄せる。
     */
    public function create(): View
    {
        $form = $this->reserveService->getCreateForm();

        return view('admin.reserves.create', [
            'record'    => null,

            // Blade: ($userTypes ?? []) as $id => $label  형태로 사용
            'userTypes' => $form['userTypeOptions'] ?? [],

            // Blade: ($parks ?? []) as $id => $label
            'parks'     => $form['parkOptions'] ?? [],

            // Blade: ($prices ?? []) as $id => $label (prine_name 표시)
            'prices'    => $form['priceOptions'] ?? [],

            // Blade: ($psections ?? []) as $id => $label
            'psections' => $form['psectionOptions'] ?? [],

            // Blade: ($ptypes ?? []) as $id => $label
            'ptypes'    => $form['ptypeOptions'] ?? [],
        ]);
    }

    /**
     * 新規（登録）
     */
    public function store(ReserveRequest $request): RedirectResponse
    {
        $this->reserveService->create($request->payload());

        return redirect()
            ->route('reserves.index')
            ->with('success', __('登録を完了しました。'));
    }

    /**
     * 編集（画面）
     */
    public function edit(int $reserveId): View
    {
        $form = $this->reserveService->getEditForm($reserveId);

        return view('admin.reserves.edit', [
            'record'    => $form['row'], 

            'userTypes' => $form['userTypeOptions'] ?? [],
            'parks'     => $form['parkOptions'] ?? [],
            'prices'    => $form['priceOptions'] ?? [],
            'psections' => $form['psectionOptions'] ?? [],
            'ptypes'    => $form['ptypeOptions'] ?? [],
        ]);
    }



    /**
     * 編集（更新）
     */
    public function update(ReserveRequest $request, int $reserveId): RedirectResponse
    {
        $this->reserveService->update($reserveId, $request->payload());

        return redirect()
            ->route('reserves.index')
            ->with('success', __('更新を完了しました。'));
    }

    /**
     * 削除（複数削除 pk[]）
     */
    public function destroy(ReserveRequest $request): RedirectResponse
    {
        $payload = $request->payload();
        $deleted = $this->reserveService->deleteMany($payload['ids'] ?? []);

        return redirect()
            ->route('reserves.index')
            ->with(
                $deleted > 0 ? 'success' : 'warning',
                $deleted > 0 ? __(':count 件を削除しました。', ['count' => $deleted]) : __('削除対象がありません。')
            );
    }
}
