<?php

namespace App\Models;

use CodeIgniter\Model;

class Mecanicien extends Model
{
    /**
     * table name
     * @var string
     */
    protected $table = 'reparations';
    protected $primaryKey = 'reparation_id'; // Primary key of your table
    protected $allowedFields = ['user_id', 'produit_id', 'reparation_statut', 'reparation_observation', 'reparation_debut', 'reparation_fin']; // Fields allowed for insert/update
    protected $useTimestamps = false; // Set to true if your table has `created_at` and `updated_at` columns

    public function createRepation(array $data)
    {
        return $this->insert($data);
    }

    public function getReparation(int $id = null) 
{
    $session = session();
    $user = $session->get('user');
    if ($user['group_name'] == "Conseil" || $user['group_name'] == "Direction") {
        $reparation = $this->select('reparations.reparation_id as reparationsID, reparations.user_id, reparations.reparation_statut, reparations.produit_id, reparations.reparation_observation, reparations.reparation_debut, reparations.reparation_fin, users.*, products.*')
         ->join('users', 'reparations.user_id = users.id')
         ->join('products', 'reparations.produit_id = products.id')
         ->findAll();    
    } else {
        if ($id !== null) {
            $reparation = $this->select('reparations.reparation_id as reparationsID, reparations.user_id, reparations.reparation_statut, reparations.produit_id, reparations.reparation_observation, reparations.reparation_debut, reparations.reparation_fin, users.*, products.*')
                ->join('users', 'reparations.user_id = users.id')
                ->join('products', 'reparations.produit_id = products.id')
                ->where('users.id', $id)
                ->findAll();
        } else {
            
            $reparation = [];
        }
    }

    return $reparation;
}


    public function getReparationSingle(int $id)
    {
        $reparation = $this->select('reparations.reparation_id as reparationsID, reparations.user_id, reparations.reparation_statut, reparations.reparation_statut, reparations.produit_id, reparations.reparation_observation, reparations.reparation_debut, reparations.reparation_fin, users.*, products.*')
            ->join('users', 'reparations.user_id = users.id')
            ->join('products', 'reparations.produit_id = products.id')
            ->where('reparations.reparation_id', $id)
            ->first();
        // return $this->where('user_id', $id)->findAll();

        return $reparation;
    }

    public function updateReparation(array $data, int $id)
    {
        return $this->update($id, $data);
    }

    public function deleteReparation(int $id)
    {
        return $this->delete($id);
    }

    public function getTotalReparationPerStatut(){
        return $this->db->table('reparation')
            ->select('COUNT(*) as total') 
            ->where('reparation_statut', 2)
            ->get()
            ->getRow();
    }
    
}
