<?php

declare(strict_types=1);

namespace App\Modules\Administration\Services;

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

final class RankService
{
    public function __construct(
        private RankRepository $ranks
    ) {
    }

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

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

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

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

        $level = (int) (
            $data['rank_level'] ?? 0
        );

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

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

        if ($level < 1) {
            throw new RuntimeException(
                'Rank level must be greater than zero.'
            );
        }

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

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

        $this->ranks->create([
            'rank_code' => $code,
            'rank_name' => $name,
            'rank_level' => $level,
            '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 {
        $rank = $this->ranks->find($id);

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

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

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

        $level = (int) (
            $data['rank_level'] ?? 0
        );

        if (
            $code === ''
            || $name === ''
            || $level < 1
        ) {
            throw new RuntimeException(
                'Rank code, name and valid level are required.'
            );
        }

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

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

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

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

        $this->ranks->update(
            $id,
            [
                'rank_code' => $code,
                'rank_name' => $name,
                'rank_level' => $level,
                'status' => isset($data['status'])
                    ? 1
                    : 0,
                'updated_at' => date('Y-m-d H:i:s')
            ]
        );
    }

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

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

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

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