<?php
declare(strict_types=1);

namespace App\Controllers;

use App\Core\Request;
use App\Industry\Catalog;
use App\Models\PipelineStage;
use App\Services\SetupDefaults;
use App\Services\SetupService;
use RuntimeException;

final class SetupController extends BaseController
{
    public function index(Request $request): void
    {
        $this->showSection('index');
    }

    public function names(Request $request): void
    {
        $this->showSection('names');
    }

    public function fields(Request $request): void
    {
        $this->showSection('fields');
    }

    public function stages(Request $request): void
    {
        $this->showSection('stages');
    }

    public function forms(Request $request): void
    {
        $this->showSection('forms');
    }

    public function alerts(Request $request): void
    {
        $this->showSection('alerts');
    }

    public function dashboard(Request $request): void
    {
        $this->showSection('dashboard');
    }

    public function permissions(Request $request): void
    {
        $this->showSection('permissions');
    }

    public function save(Request $request): void
    {
        $this->requireModule('setup');
        $this->verifyCsrf($request);
        try {
            SetupService::saveSection((string) $request->input('section'), $request->all());
        } catch (RuntimeException $e) {
            json_out(['ok' => false, 'error' => $e->getMessage()], 422);
        }
        json_out(['ok' => true, 'message' => 'Saved as a draft. Publish when you want staff to see it.']);
    }

    public function publish(Request $request): void
    {
        $this->requireModule('setup');
        $this->verifyCsrf($request);
        try {
            SetupService::publish();
        } catch (RuntimeException $e) {
            json_out(['ok' => false, 'error' => $e->getMessage()], 422);
        }
        json_out(['ok' => true, 'message' => 'Published. Staff now see the new names, stages, and forms.']);
    }

    public function undo(Request $request): void
    {
        $this->requireModule('setup');
        $this->verifyCsrf($request);
        try {
            SetupService::undo();
        } catch (RuntimeException $e) {
            json_out(['ok' => false, 'error' => $e->getMessage()], 422);
        }
        json_out(['ok' => true, 'message' => 'Reverted to the last published save.']);
    }

    private function showSection(string $section): void
    {
        $this->requireModule('setup');
        $draft = SetupService::draft();
        view('setup/' . $section, [
            'title' => 'Setup',
            'section' => $section,
            'draft' => $draft,
            'dirty' => SetupService::isDirty(),
            'canUndo' => SetupService::canUndo(),
            'publishedAt' => SetupService::publishedAt(),
            'industry' => Catalog::current(),
            'defaults' => SetupDefaults::class,
            'liveStages' => PipelineStage::allActive(),
            'notifications' => $this->notifications(),
        ]);
    }
}
