<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Portal Base Model
 * 
 * All models should extend this class to automatically filter by portal_id
 * This ensures data isolation between different portals
 */
class Portal_Model extends CI_Model {

    protected $portal_id;
    protected $portal_name;
    protected $portal_identifier;

    public function __construct() {
        parent::__construct();
        
        // Load portal configuration
        $this->config->load('portal', TRUE);
        $portal_config = $this->config->item('portal');
        
        $this->portal_id = $portal_config['portal_id'];
        $this->portal_name = $portal_config['portal_name'];
        $this->portal_identifier = $portal_config['portal_identifier'];
    }

    /**
     * Get current portal ID
     * 
     * @return int Portal ID
     */
    public function get_portal_id() {
        return $this->portal_id;
    }

    /**
     * Get current portal name
     * 
     * @return string Portal name
     */
    public function get_portal_name() {
        return $this->portal_name;
    }

    /**
     * Get current portal identifier
     * 
     * @return string Portal identifier
     */
    public function get_portal_identifier() {
        return $this->portal_identifier;
    }

    /**
     * Add portal filter to query
     * 
     * @param string $table_alias Optional table alias
     * @return $this
     */
    protected function filter_by_portal($table_alias = '') {
        $column = empty($table_alias) ? 'portal_id' : $table_alias . '.portal_id';
        $this->db->where($column, $this->portal_id);
        return $this;
    }

    /**
     * Insert data with portal_id automatically added
     * 
     * @param string $table Table name
     * @param array $data Data to insert
     * @return bool Success status
     */
    protected function portal_insert($table, $data) {
        $data['portal_id'] = $this->portal_id;
        return $this->db->insert($table, $data);
    }

    /**
     * Update data with portal filter
     * 
     * @param string $table Table name
     * @param array $data Data to update
     * @param array $where Where conditions
     * @return bool Success status
     */
    protected function portal_update($table, $data, $where) {
        $where['portal_id'] = $this->portal_id;
        $this->db->where($where);
        return $this->db->update($table, $data);
    }

    /**
     * Delete data with portal filter
     * 
     * @param string $table Table name
     * @param array $where Where conditions
     * @return bool Success status
     */
    protected function portal_delete($table, $where) {
        $where['portal_id'] = $this->portal_id;
        return $this->db->delete($table, $where);
    }

    /**
     * Get data with automatic portal filtering
     * 
     * @param string $table Table name
     * @param array $where Optional where conditions
     * @return array Query result
     */
    protected function portal_get($table, $where = array()) {
        $where['portal_id'] = $this->portal_id;
        return $this->db->get_where($table, $where)->result();
    }

    /**
     * Get single row with automatic portal filtering
     * 
     * @param string $table Table name
     * @param array $where Where conditions
     * @return object|null Query result
     */
    protected function portal_get_row($table, $where = array()) {
        $where['portal_id'] = $this->portal_id;
        $result = $this->db->get_where($table, $where)->row();
        return $result ? $result : null;
    }

    /**
     * Count rows with automatic portal filtering
     * 
     * @param string $table Table name
     * @param array $where Optional where conditions
     * @return int Count
     */
    protected function portal_count($table, $where = array()) {
        $where['portal_id'] = $this->portal_id;
        $this->db->where($where);
        return $this->db->count_all_results($table);
    }
}
