<?php 
namespace App\Models;

use CodeIgniter\Model;

class BaseModel extends Model {

   
    public function __construct() {
        parent::__construct();

        date_default_timezone_set( 'Asia/Kolkata' );
    }


    public function getDatabaseObject() {
        return \Config\Database::connect();
    }

    public function getCreatedBy() {
        
        $intCreatedBy = UserTypesModel::USER_ID_SUPER_ADMIN;
        
        return $intCreatedBy;
    }
    
    public function getUpdatedBy() {
        
        $intUpdatedBy = UserTypesModel::USER_ID_SUPER_ADMIN;

        return $intUpdatedBy;
    }

    public function add( $arrmixInsertData ) {

        if( false == isValArr( $arrmixInsertData ) ) {
            return false;
        }

        $arrmixInsertData['created_by'] = ( true == isset( $arrmixInsertData['created_by'] ) && true == isValId( $arrmixInsertData['created_by'] ) ) ? $arrmixInsertData['created_by'] : $this->getCreatedBy();
        $arrmixInsertData['updated_by'] = ( true == isset( $arrmixInsertData['updated_by'] ) && true == isValId( $arrmixInsertData['updated_by'] ) ) ? $arrmixInsertData['updated_by'] : $this->getUpdatedBy();
        
        $objQuery = $this->createBuilderObject()->insert( $arrmixInsertData );
        
        if( $objQuery ) {
            return $this->createService()->insertID();
        } 
        
        return false;

    }

    public function addBatch( $arrmixInsertList ) {
        
        if( false == isValArr( $arrmixInsertList ) ) {
            return false;
        }

        foreach( $arrmixInsertList as $arrmixInsertDetails ) {
            $arrmixInsertDetails['created_by'] = ( true == isset( $arrmixInsertDetails['created_by'] ) && true == isValId( $arrmixInsertDetails['created_by'] ) ) ? $arrmixInsertDetails['created_by'] : $this->getCreatedBy();
            $arrmixInsertDetails['updated_by'] = ( true == isset( $arrmixInsertDetails['updated_by'] ) && true == isValId( $arrmixInsertDetails['updated_by'] ) ) ? $arrmixInsertDetails['updated_by'] : $this->getUpdatedBy();
            $arrmixInsertData[] = $arrmixInsertDetails;
        }
        
        $objQuery = $this->createBuilderObject()->insertBatch( $arrmixInsertData );
        
        if( $objQuery ) {
            return true;
        } 
        
        return false;
    }

    public function edit( $strUpdateKey, $arrmixUpdateData ) {
        
        $arrmixUpdateData['updated_by'] = ( true == isset( $arrmixUpdateData['updated_by'] ) && true == isValId( $arrmixUpdateData['updated_by'] ) ) ? $arrmixUpdateData['updated_by'] : $this->getUpdatedBy();
        $arrmixUpdateData['updated_at'] = CURRENT_DATETIME;
        
        $objQuery = $this->createBuilderObject()->where( $strUpdateKey, $arrmixUpdateData[$strUpdateKey] )->update( $arrmixUpdateData );

        if( $objQuery ) {
            return true;
        } 
        
        return false;

    }

    public function createBuilderObject() {
        return self::createService()->builder();
    }
    
    public static function createService() {
        $strClassName = static::class;
        
        return new $strClassName();
    }
    
}