<?php

declare(strict_types=1);

namespace App\Modules\Administration\Repositories;

use System\Database\QueryBuilder;

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

    public function all(): array
    {
        return $this->query
            ->table('organizations')
            ->orderBy('id', 'DESC')
            ->get();
    }

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

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

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

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

    public function setActive(
        int $id,
        int $active
    ): bool {
        return $this->query
            ->table('organizations')
            ->where('id', $id)
            ->update([
                'active' => $active,
                'updated_at' => date('Y-m-d H:i:s')
            ]);
    }
    public function findByCode(
    string $code
): ?array {
    return $this->query
        ->table('organizations')
        ->where('organization_code', $code)
        ->first();
}
}