<?php

declare(strict_types=1);

namespace App\Modules\Administration\Repositories;

use System\Database\QueryBuilder;

final class RankRepository
{
    public function __construct(
        private QueryBuilder $query
    ) {
    }

    public function all(): array
    {
        return $this->query
            ->table('ranks')
            ->orderBy('rank_level', 'ASC')
            ->get();
    }

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

    public function findByCode(
        string $code
    ): ?array {
        return $this->query
            ->table('ranks')
            ->where('rank_code', $code)
            ->first();
    }

    public function findByName(
        string $name
    ): ?array {
        return $this->query
            ->table('ranks')
            ->where('rank_name', $name)
            ->first();
    }

    public function create(array $data): bool
    {
        return $this->query
            ->table('ranks')
            ->insert($data);
    }

    public function update(
        int $id,
        array $data
    ): bool {
        return $this->query
            ->table('ranks')
            ->where('id', $id)
            ->update($data);
    }

    public function setStatus(
        int $id,
        int $status
    ): bool {
        return $this->query
            ->table('ranks')
            ->where('id', $id)
            ->update([
                'status' => $status,
                'updated_at' => date('Y-m-d H:i:s')
            ]);
    }
}