<?php

declare(strict_types=1);

namespace App\Modules\Administration\Services;

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

final class OrganizationService
{
    public function __construct(
        private OrganizationRepository $organizations
    ) {
    }

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

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

public function create(array $data): void
{
    $code = strtoupper(
        trim(
            (string) (
                $data['organization_code'] ?? ''
            )
        )
    );

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

    if ($code === '') {
        throw new RuntimeException(
            'Organization code is required.'
        );
    }

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

    if (
        $this->organizations->findByCode($code)
        !== null
    ) {
        throw new RuntimeException(
            'Organization code already exists.'
        );
    }

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

    $this->organizations->create([
        'organization_code' => $code,
        'organization_name' => $name,
        '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 {
    $organization =
        $this->organizations->find($id);

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

    $code = strtoupper(
        trim(
            (string) (
                $data['organization_code'] ?? ''
            )
        )
    );

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

    if ($code === '' || $name === '') {
        throw new RuntimeException(
            'Organization code and name are required.'
        );
    }

    $existingCode =
        $this->organizations->findByCode($code);

    if (
        $existingCode !== null
        && (int) $existingCode['id'] !== $id
    ) {
        throw new RuntimeException(
            'Organization code already exists.'
        );
    }

    $existingName =
        $this->organizations->findByName($name);

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

    $this->organizations->update(
        $id,
        [
            'organization_code' => $code,
            'organization_name' => $name,
            'active' => isset($data['active'])
                ? 1
                : 0,
            'updated_at' => date('Y-m-d H:i:s')
        ]
    );
}
    public function toggleStatus(int $id): void
    {
        $organization =
            $this->organizations->find($id);

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

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

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