<?php

declare(strict_types=1);

namespace App\Modules\Administration\Services;

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

final class BranchService
{
    public function __construct(
        private BranchRepository $branches
    ) {
    }

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

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

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

    public function create(array $data): void
    {
        $organizationId = (int) (
            $data['organization_id'] ?? 0
        );

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

        if ($organizationId < 1) {
            throw new RuntimeException(
                'Organization is required.'
            );
        }

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

        $existing = $this->branches->findByName(
            $organizationId,
            $branchName
        );

        if ($existing !== null) {
            throw new RuntimeException(
                'Branch already exists in this organization.'
            );
        }

        $this->branches->create([
            'organization_id' => $organizationId,
            'branch_name' => $branchName,
            '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 {
        $branch = $this->branches->find($id);

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

        $organizationId = (int) (
            $data['organization_id'] ?? 0
        );

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

        if ($organizationId < 1) {
            throw new RuntimeException(
                'Organization is required.'
            );
        }

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

        $existing = $this->branches->findByName(
            $organizationId,
            $branchName
        );

        if (
            $existing !== null
            && (int) $existing['id'] !== $id
        ) {
            throw new RuntimeException(
                'Branch already exists in this organization.'
            );
        }

        $this->branches->update(
            $id,
            [
                'organization_id' => $organizationId,
                'branch_name' => $branchName,
                'active' => isset($data['active']) ? 1 : 0,
                'updated_at' => date('Y-m-d H:i:s')
            ]
        );
    }

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

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

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

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