<?php

namespace Modules\Redd\Models;

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

class ProjectCarbonDataModel extends Model
{

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

    protected $allowedFields = ['project_id',  'reff_period_start', 'reff_period_end', 'credit_period_start', 'credit_period_end', 'estimated_er', 'buffer_credit', 'uncertainty', 'registry', 'description', 'created_by', 'updated_by'];

    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_data');
        $builder->select('redd_project_carbon_data.*, name, fname, lname, project_number, client_id')
                    ->join('redd_projects', 'redd_projects.id=redd_project_carbon_data.project_id', 'left')
                    ->join('redd_clients', 'redd_clients.id = redd_projects.client_id', 'left')
                    ->where('redd_project_carbon_data.deleted_at', null)
                    ->orderBy('redd_project_carbon_data.id', 'DESC');
        $result = $builder->get()->getResult();
        return $result;
    }

    public function getByProject($projectId)
    {
        $builder = $this->db->table('redd_project_carbon_data');
        $builder->select('redd_project_carbon_data.*,')
                    ->where('redd_project_carbon_data.deleted_at', null)
                    ->where('project_id', $projectId);
        return $results = $builder->get()->getRow();
    }
    
    public function showLatestCarbonDataByClient($client)
    {
        $builder = $this->db->table('redd_project_carbon_data');
        $builder->select('redd_project_carbon_data.*')
                    ->join('redd_projects', 'redd_projects.id=redd_project_carbon_data.project_id', 'left')
                    ->where('redd_project_carbon_data.deleted_at', null)
                    ->where('client_id', $client)
                    ->orderBy('redd_project_carbon_data.id', 'DESC');
        return $results = $builder->get()->getRow();
    }
    

}