<?php

declare(strict_types=1);

namespace App\Modules\Administration\Repositories;

use System\Database\QueryBuilder;

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

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

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

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

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

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

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

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