<?php

namespace Modules\Redd\Models;

use CodeIgniter\Model;
use CodeIgniter\I18n\Time;

class ProjectCarbonPoolsModel extends Model
{

  protected $table = 'redd_project_carbon_pools';
    protected $primaryKey = 'id';

    protected $allowedFields = ['project_id', 'carbon_pool_id'];

    protected $returnType     = 'array';
    protected $useTimestamps  = true;
    protected $useSoftDeletes = true;
    
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';


    public function getAll()
    {
        $builder = $this->db->table('redd_project_carbon_pools');
        $builder->where('deleted_at', null);
        $result = $builder->get()->getResult();
        return $result;
    }

    public function projectCarbonPools($project)
    {
        $builder = $this->db->table('redd_project_carbon_pools');
        $builder->select('redd_project_carbon_pools.*, redd_carbon_pools.name as carbon_pool, redd_projects.name as project_name')
                ->join('redd_carbon_pools', 'redd_carbon_pools.id=redd_project_carbon_pools.carbon_pool_id', 'left')
                ->join('redd_projects', 'redd_projects.id=redd_project_carbon_pools.project_id', 'left')
                ->where('redd_project_carbon_pools.deleted_at', null)
                ->where('redd_project_carbon_pools.project_id', $project);
        $result = $builder->get()->getResult();
        return $result;
    }

    public function getByProject($projectId)
    {
        $builder = $this->db->table('redd_project_carbon_pools');
        $builder->select('redd_project_carbon_pools.*, name,')
                    ->join('redd_carbon_pools', 'redd_carbon_pools.id=redd_project_carbon_pools.carbon_pool_id', 'left')
                    ->where('redd_project_carbon_pools.deleted_at', null)
                    ->where('project_id', $projectId);
        return $results = $builder->get()->getResult();
    }
}