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

class RolePermissions extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();

        // RBAC configuration is admin-only: require login + super-admin role
        if (!isset($_SESSION['qurbani_user_id']) || $_SESSION['qurbani_role'] !== 'sa') {
            redirect('/admin', 'refresh');
        }

        // Load models
        $this->load->model('Role_model', 'role');
        $this->load->model('Permission_model', 'perm');
        $this->load->model('RolePermission_model', 'rp');
    }

    // 📄 View All Roles & Permissions (Main Page)
    public function index()
    {
        $data['roles'] = $this->role->get_all();
        $data['permissions'] = $this->perm->get_all();

        $selected_role_id = $this->input->get('role_id');
        $data['selected_role_id'] = $selected_role_id;
        $data['assigned_permissions'] = $selected_role_id 
            ? $this->perm->get_permissions_by_role($selected_role_id) 
            : [];

        $data['title'] = 'Permissions Settings';
        $data['mainContent'] = 'admin/rolepermissions/index';
        $this->load->view('admin/layout/master', $data);
    }

    // ✅ Save Assigned Permissions to Role
    public function save()
    {
        $role_id = $this->input->post('role_id');
        $permissions = $this->input->post('permissions') ?? [];

        // Clear old permissions
        $this->rp->clear_permissions($role_id);

        // Assign new permissions
        foreach ($permissions as $permission_id) {
            $this->rp->assign($role_id, $permission_id);
        }

        $this->session->set_flashdata('success', 'Permissions updated successfully!');
        redirect('admin/settings?role_id=' . $role_id);
    }

    // ➕ Add Role
    public function add_role()
    {
        if ($this->input->post()) {
            $data = [
                'name' => $this->input->post('name'),
                'label' => $this->input->post('label'),
            ];
            $this->role->create($data);
            $this->session->set_flashdata('success', 'Role added successfully!');
            redirect('admin/settings');
        }
    }

    // ✏️ Edit Role
    public function edit_role_modal()
    {
        $id = $this->input->post('id');
        $data = [
            'name'  => $this->input->post('name'),
            'label' => $this->input->post('label'),
        ];
        $this->role->update($id, $data);
        $this->session->set_flashdata('success', 'Role updated successfully!');
        redirect('admin/settings');
    }

    // ❌ Delete Role
    public function delete_role($id)
    {
        $this->role->delete($id);
        $this->session->set_flashdata('success', 'Role deleted successfully!');
        redirect('admin/settings');
    }

    // ➕ Add Permission
    public function add_permission()
    {
        if ($this->input->post()) {
            $data = [
                'controller' => $this->input->post('controller'),
                'method'     => $this->input->post('method'),
                'label'      => $this->input->post('label'),
            ];
            $this->perm->create($data);
            $this->session->set_flashdata('success', 'Permission added successfully!');
            redirect('admin/settings');
        }
    }

    // ✏️ Edit Permission
    public function edit_permission()
    {
        $id = $this->input->post('id');
        $data = [
            'controller' => $this->input->post('controller'),
            'method'     => $this->input->post('method'),
            'label'      => $this->input->post('label'),
        ];
        $this->perm->update($id, $data);
        $this->session->set_flashdata('success', 'Permission updated!');
        redirect('admin/settings');
    }

    // ❌ Delete Permission
    public function delete_permission($id)
    {
        $this->perm->delete($id);
        $this->session->set_flashdata('success', 'Permission deleted!');
        redirect('admin/settings');
    }
}
