<?php

declare(strict_types=1);

namespace App\Modules\Administration\Services;

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

final class PermissionService
{
    public function __construct(
        private PermissionRepository $permissions
    ) {
    }

    public function all(): array
    {
        return $this->permissions->all();
    }

    public function find(int $id): ?array
    {
        return $this->permissions->find($id);
    }

    public function create(array $data): void
    {
        $permissionName = trim(
            (string) (
                $data['permission_name']
                ?? ''
            )
        );

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

        if ($permissionName === '') {
            throw new RuntimeException(
                'Permission name is required.'
            );
        }

        if (
            $this->permissions->findByName(
                $permissionName
            ) !== null
        ) {
            throw new RuntimeException(
                'Permission name already exists.'
            );
        }

        $this->permissions->create([
            'permission_name' =>
                $permissionName,

            'description' =>
                $description,

            'active' => 1,

            'created_at' =>
                date('Y-m-d H:i:s'),

            'updated_at' =>
                date('Y-m-d H:i:s')
        ]);
    }

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

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

        $permissionName = trim(
            (string) (
                $data['permission_name']
                ?? ''
            )
        );

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

        if ($permissionName === '') {
            throw new RuntimeException(
                'Permission name is required.'
            );
        }

        $existing =
            $this->permissions->findByName(
                $permissionName
            );

        if (
            $existing !== null
            && (int) $existing['id'] !== $id
        ) {
            throw new RuntimeException(
                'Permission name already exists.'
            );
        }

        $this->permissions->update(
            $id,
            [
                'permission_name' =>
                    $permissionName,

                'description' =>
                    $description,

                'active' =>
                    isset($data['active'])
                        ? 1
                        : 0,

                'updated_at' =>
                    date('Y-m-d H:i:s')
            ]
        );
    }

    public function toggleStatus(
        int $id
    ): void {
        $permission =
            $this->permissions->find($id);

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

        $newStatus =
            (int) $permission['active'] === 1
                ? 0
                : 1;

        $this->permissions->setActive(
            $id,
            $newStatus
        );
    }
}