<?php

namespace Modules\Redd\Models;

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

class ProjectMethodologiesModel extends Model
{

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

    protected $allowedFields = ['project_id', 'methodology_id', 'description'];

    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_methodologies');
        $builder->where('deleted_at', null);
        $result = $builder->get()->getResult();
        return $result;
    }

    public function getProjectMethodologies($project)
    {
        $builder = $this->db->table('redd_project_methodologies');
        $builder->select('redd_project_methodologies.*, redd_methodologies.name as methodology_name, redd_projects.name as project_name')
                ->join('redd_projects', 'redd_projects.id=redd_project_methodologies.project_id')
                ->join('redd_methodologies', 'redd_methodologies.id=redd_project_methodologies.methodology_id', 'left')
                ->where('redd_project_methodologies.deleted_at', null)
                ->where('redd_project_methodologies.project_id', $project);
        return $results = $builder->get()->getResult();
    }

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