<?php

namespace App\Models;

use CodeIgniter\Model;

class Securite extends Model
{
    /**
     * table name
     * @var string
     */
    protected $table = 'securite';
    protected $primaryKey = 'id'; // Primary key column
    protected $allowedFields = ['product_id', 'status', 'date', 'active'];

    

    /**
     * To get store data by ID
     * @param int $id
     * @return array|object|null
     */
    public function getSecuriteData(int $id = null)
    {
        if ($id) {
            return $this->where('id', $id)->first();
        }

        return $this->findAll();
    }
    

    public function getAllSecuriteData(int $id = null)
    {
        $session = session();
        $users = $session->get('user');
        if ($users["group_name"] == "Direction" || $users["group_name"] == "SuperAdmin" || $users["group_name"] == "DAF") {
            return $this->where('active', true)
            ->findAll();
        }
            return $this->where('active', true)
            ->where('store_id', $users['store_id'])->findAll();
        
    }
    /**
     * create store
     * @param array $data
     * @return bool|int|string
     */
    public function createStore(array $data)
    {
        if ($data) {
            return $this->insert($data);
        }
        return false;
    }

    /**
     * update stores
     * @param array $data
     * @param int $id
     * @return bool
     */
    public function updateSecurite(array $data, int $id)
    {
        if ($data && $id) {
            return $this->update($id, $data);
        }
        return false;
    }

    /**
     * remove store 
     * @param int $id
     * @return bool|\CodeIgniter\Database\BaseResult
     */
    public function removeSecurite(int $id)
    {
        if ($id) {
            return $this->where("product_id", $id)->delete();
        }
        return false;
    }



    public function removeStore(int $id)
    {
        if ($id) {
            return $this->delete($id);
        }
        return false;
    }

    /**
     * count total store
     * @return int|string
     */
    public function countTotalStores()
    {
        return $this->where('active', 1)->countAllResults();
    }

    
    
    
}