<?php

declare(strict_types=1);

namespace App\Modules\Administration\Services;

use App\Modules\Administration\Repositories\DynamicFormSectionRepository;
use RuntimeException;

final class DynamicFormSectionService
{
    public function __construct(
        private DynamicFormSectionRepository $sections
    ) {
    }

    public function byForm(
        int $formId
    ): array {
        return $this->sections
            ->byForm($formId);
    }

    public function create(
        int $formId,
        array $data
    ): void {
        if ($formId <= 0) {
            throw new RuntimeException(
                'Invalid form ID.'
            );
        }

        $title = trim(
            (string) (
                $data['section_title']
                ?? ''
            )
        );

        if ($title === '') {
            throw new RuntimeException(
                'Section title is required.'
            );
        }

        $now = date('Y-m-d H:i:s');

        $this->sections->create([
            'form_id' =>
                $formId,

            'section_title' =>
                $title,

            'section_description' =>
                $this->nullableString(
                    $data['section_description']
                    ?? null
                ),

            'sort_order' =>
                $this->sections
                    ->nextSortOrder($formId),

            'status' =>
                1,

            'created_at' =>
                $now,

            'updated_at' =>
                $now
        ]);
    }

    public function update(
        int $id,
        array $data
    ): void {
        $section =
            $this->sections->find($id);

        if ($section === null) {
            throw new RuntimeException(
                'Form section not found.'
            );
        }

        $title = trim(
            (string) (
                $data['section_title']
                ?? ''
            )
        );

        if ($title === '') {
            throw new RuntimeException(
                'Section title is required.'
            );
        }

        $this->sections->update(
            $id,
            [
                'section_title' =>
                    $title,

                'section_description' =>
                    $this->nullableString(
                        $data['section_description']
                        ?? null
                    ),

                'updated_at' =>
                    date('Y-m-d H:i:s')
            ]
        );
    }
public function deactivate(
    int $id
): void {
    if ($id <= 0) {
        throw new RuntimeException(
            'Invalid section ID.'
        );
    }

    $section = $this->sections
        ->find($id);

    if ($section === null) {
        throw new RuntimeException(
            'Form section not found.'
        );
    }

    if (
        $this->sections
            ->hasActiveFields($id)
    ) {
        throw new RuntimeException(
            'Remove all fields from this section before removing the section.'
        );
    }

    $updated = $this->sections
        ->deactivate($id);

    if (!$updated) {
        throw new RuntimeException(
            'Section removal failed.'
        );
    }
}
    private function nullableString(
        mixed $value
    ): ?string {
        $value = trim(
            (string) $value
        );

        return $value !== ''
            ? $value
            : null;
    }
}