<?php

namespace Modules\Redd\Models;

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

class ProjectStatusModel extends Model
{

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

    protected $allowedFields = ['project_id', 'project_status_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_status');
        $builder->where('deleted_at', null);
        $result = $builder->get()->getResult();
        return $result;
    }

    public function getByProject($projectId)
    {
        $builder = $this->db->table('redd_project_status');
        $builder->select('redd_project_status.*, name, icon, color, redd_status.description as status_description')
                ->join('redd_status', 'redd_status.id=redd_project_status.project_status_id')
                ->where('redd_project_status.deleted_at', null)
                ->where('redd_project_status.project_id', $projectId);
        return $result = $builder->get()->getResult();
    }

    public function showProjectCurrentStatus($projectId)
    {
        $builder = $this->db->table('redd_project_status');
        $builder->select('
            redd_status.name   as status_name,
            redd_status.color  as status_color,
            redd_status.icon   as status_icon,
            redd_status.description as status_description
        ')
        ->join('redd_status', 'redd_status.id = redd_project_status.project_status_id', 'left')
        ->where('redd_project_status.deleted_at', null)
        ->where('redd_project_status.project_id', $projectId)
        ->orderBy('redd_project_status.id', 'DESC')
        ->limit(1);
    
        return $builder->get()->getRow(); // single latest status
    }

}