<?php

namespace Modules\AdministrativeUnits\Models;

use Codeigniter\Model;

class ParishModel extends Model
{
    private $tbl_district = "districts";
    private $tbl_county = "counties";
    private $tbl_subcounty = "subcounties";

    public function __construct()
    {
        parent::__construct();

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

        
        $this->allowedFields = ['parish_name','subcounty_id'];
    }
	
	public function findByName($name, $subcountyId)
    {
        $builder = $this->db->table($this->table);
        $builder->select('*');
        $builder->where("{$this->table}.parish_name",$name);
		$builder->where("{$this->table}.subcounty_id",$subcountyId);
        return $builder->get()->getRowArray();
    }

    /**public function findAll(int $limit = 0, int $offset = 0)
    {
        $builder = $this->db->table($this->table);
        $builder->select('*');
        if($limit > 0){
            $builder->limit($limit, $offset);
        }
        $builder->join($this->tbl_subcounty, "{$this->tbl_subcounty}.subcounty_id = {$this->table}.subcounty_id");
        $builder->join($this->tbl_county, "{$this->tbl_county}.county_id = {$this->tbl_subcounty}.county_id");
        $builder->join($this->tbl_district, "{$this->tbl_district}.district_id = {$this->tbl_county}.district_id");
        return $builder->get()->getResultArray();
    }**/

    public function findByDistrictId($id)
    {
        $builder = $this->db->table($this->table);
        $builder->select('*');
        $builder->where("{$this->tbl_district}.district_id",$id);
        $builder->join($this->tbl_subcounty, "{$this->tbl_subcounty}.subcounty_id = {$this->table}.subcounty_id");
        $builder->join($this->tbl_county, "{$this->tbl_county}.county_id = {$this->tbl_subcounty}.county_id");
        $builder->join($this->tbl_district, "{$this->tbl_district}.district_id = {$this->tbl_county}.district_id");
        return $builder->get()->getResultArray();
    }
    

    public function findByCountyId($id)
    {
        $builder = $this->db->table($this->table);
        $builder->select('*');
        $builder->where("{$this->tbl_county}.county_id",$id);
        $builder->join($this->tbl_subcounty, "{$this->tbl_subcounty}.subcounty_id = {$this->table}.subcounty_id");
        $builder->join($this->tbl_county, "{$this->tbl_county}.county_id = {$this->tbl_subcounty}.county_id");
        $builder->join($this->tbl_district, "{$this->tbl_district}.district_id = {$this->tbl_county}.district_id");
        return $builder->get()->getResultArray();
    }

    public function findBySubCountyId($id)
    {
        $builder = $this->db->table($this->table);
        $builder->select('*');
        $builder->where("{$this->tbl_subcounty}.subcounty_id",$id);
        $builder->join($this->tbl_subcounty, "{$this->tbl_subcounty}.subcounty_id = {$this->table}.subcounty_id");
        $builder->join($this->tbl_county, "{$this->tbl_county}.county_id = {$this->tbl_subcounty}.county_id");
        $builder->join($this->tbl_district, "{$this->tbl_district}.district_id = {$this->tbl_county}.district_id");
        return $builder->get()->getResultArray();
    }
    
}