<?php

declare(strict_types=1);

namespace App\Modules\Administration\Services;

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

final class DepartmentService
{
    public function __construct(
        private DepartmentRepository $departments
    ) {
    }

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

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

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

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

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

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

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

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

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

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

        if (
            $this->departments->findByName(
                $branchId,
                $name
            ) !== null
        ) {
            throw new RuntimeException(
                'Department already exists in this branch.'
            );
        }

        $this->departments->create([
            'branch_id' => $branchId,
            'department_code' => $code,
            'department_name' => $name,
            'status' => 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 {
        $department =
            $this->departments->find($id);

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

        $branchId = (int) (
            $data['branch_id'] ?? 0
        );

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

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

        if (
            $branchId < 1
            || $code === ''
            || $name === ''
        ) {
            throw new RuntimeException(
                'Branch, department code and department name are required.'
            );
        }

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

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

        $existingName =
            $this->departments->findByName(
                $branchId,
                $name
            );

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

        $this->departments->update(
            $id,
            [
                'branch_id' => $branchId,
                'department_code' => $code,
                'department_name' => $name,
                'status' => isset($data['status'])
                    ? 1
                    : 0,
                'updated_at' => date('Y-m-d H:i:s')
            ]
        );
    }

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

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

        $newStatus =
            (int) $department['status'] === 1
                ? 0
                : 1;

        $this->departments->setStatus(
            $id,
            $newStatus
        );
    }
}