<?php

namespace App\Http\Controllers;

use App\Models\NumberingRule;
use App\Services\NumberingRuleService;
use Illuminate\Http\Request;
use Illuminate\View\View;

class NumberingRuleController extends Controller
{
    public function __construct(
        private readonly NumberingRuleService $numberingRuleService
    ) {}

    public function index(Request $request): View
    {
        return view('numbering.index', [
            'documentTypes' => NumberingRule::documentTypes(),
        ]);
    }

    public function create(): View
    {
        $usedTypes = $this->numberingRuleService->getUsedDocumentTypes();

        return view('numbering.create', [
            'documentTypes' => NumberingRule::documentTypes(),
            'resetPeriods' => NumberingRule::resetPeriods(),
            'usedDocumentTypes' => $usedTypes,
        ]);
    }

    public function edit(int $id): View
    {
        $rule = $this->numberingRuleService->getRule($id);

        if (! $rule) {
            abort(404, '채번 규칙을 찾을 수 없습니다.');
        }

        $usedTypes = $this->numberingRuleService->getUsedDocumentTypes($id);

        return view('numbering.edit', [
            'rule' => $rule,
            'documentTypes' => NumberingRule::documentTypes(),
            'resetPeriods' => NumberingRule::resetPeriods(),
            'usedDocumentTypes' => $usedTypes,
        ]);
    }
}
