<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\UsertypeRequest;
use App\Models\Usertype;
use App\Services\UsertypeService;
use Illuminate\Http\Request;

class UsertypeController extends Controller
{
    public function __construct(
        private readonly UsertypeService $service
    ) {}

    public function index(Request $request)
    {
        // sort の許可値チェック
        $sort = $request->query('sort', 'user_categoryid');
        $allowSort = [
            'user_categoryid',
            'sort_order',
            'usertype_subject1',
            'usertype_subject2',
            'usertype_subject3',
            'print_name',
        ];
        if (!in_array($sort, $allowSort, true)) {
            $sort = 'user_categoryid';
        }

        $sortType = strtolower($request->query('sort_type', 'asc'));
        if (!in_array($sortType, ['asc', 'desc'], true)) {
            $sortType = 'asc';
        }

        $list = $this->service->paginateList(
            $request->query('filter_sort_order'),
            $request->query('filter_usertype_subject1'),
            $request->query('filter_usertype_subject2'),
            $request->query('filter_usertype_subject3'),
            $sort,
            $sortType
        );

        return view('admin.usertypes.index', [
            'list' => $list,
            'sort' => $sort,
            'sort_type' => $sortType,
            'filter_sort_order' => $request->query('filter_sort_order', ''),
            'filter_usertype_subject1' => $request->query('filter_usertype_subject1', ''),
            'filter_usertype_subject2' => $request->query('filter_usertype_subject2', ''),
            'filter_usertype_subject3' => $request->query('filter_usertype_subject3', ''),
        ]);
    }


    public function create()
    {
        return view('admin.usertypes.create', [
            'isEdit' => false,
        ]);
    }

    public function store(UsertypeRequest $request)
    {
        $this->service->create($request->validated());

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

    public function edit(int $id)
    {
        $record = Usertype::query()->findOrFail($id);

        return view('admin.usertypes.edit', [
            'record' => $record,
            'isEdit' => true,
        ]);
    }

    public function update(UsertypeRequest $request, int $id)
    {
        $record = Usertype::query()->findOrFail($id);

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

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

    public function destroy(Request $request)
    {
        $ids = $request->input('pk', []);
        if (!is_array($ids)) {
            $ids = [$ids];
        }

        if ($ids === []) {
            return redirect()
                ->route('usertypes.index')
                ->with('error', '削除対象を1件以上選択してください。');
        }

        Usertype::query()->whereIn('user_categoryid', $ids)->delete();

        return redirect()
            ->route('usertypes.index')
            ->with('success', '削除が完了しました。');
    }
}
