<?php

namespace Modules\AdministrativeUnits\Models;

use CodeIgniter\Model;

class DistrictModel extends Model
{
    public function __construct()
    {
        parent::__construct();

        $this->table = "districts";
        $this->primaryKey = "district_id";
       
    }
    
    protected function initialize()
    {
        $this->validationRules    = [
            'district_name'     => 'required|alpha_numeric_space|min_length[3]',
            
        ];

        $this->validationMessages = [
            'district_name'        => [
                'is_unique' => 'Name is already in use.',
            ],
        ];

        $this->allowedFields = ['district_name'];
    }

    /**public function findAll(int $limit = 0, int $offset = 0)
    {
        $builder = $this->db->table($this->table);
        $builder->select('*');
        if($limit > 0){
            $builder->limit($limit, $offset);
        }
       
        return $builder->get()->getResultArray();
    }**/

    public function findByName($name)
    {
        $builder = $this->db->table($this->table);
        $builder->select('*');
        $builder->where("{$this->table}.district_name",$name);
        return $builder->get()->getRowArray();
    }

    public function like($name)
    {
        $builder = $this->db->table($this->table);
        $builder->select('*');
        $builder->like("{$this->table}.district_name",$name);
        return $builder->get()->getResultArray();
    }

    public function exists($name)
    {
        $builder = $this->db->table($this->table);
        $builder->select('*');
        $builder->where("{$this->table}.district_name",$name);
        return $builder->countAllResults() > 0;
    }
    public function getAll()
    {
        $builder = $this->db->table($this->table);
        $results = $builder->get()->getResult();
        return $results;
    }
    public function bulkInsert($data) {
        return $this->db
                        ->table($this->table)
                        ->insertBatch($data);
    }
    
}