<?php declare(strict_types=1);
// Christo Lochenberg Developments | info@christodev.co.za | +27 61 523 9126

class AdminRoles extends Controller {
    
    private $roleModel;

    public function __construct() {
        if (!isLoggedIn() || !isAdmin()) {
            header('Location: ' . URLROOT . '/users/login');
            exit;
        }
        $this->roleModel = $this->model('Role');
    }

    public function roles(): void {
        $data = [
            'title' => 'Staff Roles',
            'description' => 'Create and manage custom job titles and designations for your staff members.',
            'roles' => $this->roleModel->getRoles()
        ];
        $this->view('admin/roles', $data);
    }

    public function addRole(): void {
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
            $data = [
                'role_name' => trim($_POST['role_name']),
                'description' => trim($_POST['description']),
                'name_err' => ''
            ];

            if (empty($data['role_name'])) {
                $data['name_err'] = 'Please enter a role name.';
            }

            if (empty($data['name_err'])) {
                if ($this->roleModel->addRole($data)) {
                    systemLog('ROLE_CREATED', "Admin created a new staff role: {$data['role_name']}");
                    flash('system_msg', 'Role added successfully.', 'success');
                    header('Location: ' . URLROOT . '/admin/roles');
                    exit;
                }
            } else {
                $this->view('admin/add_role', $data);
            }
        } else {
            $data = ['role_name' => '', 'description' => '', 'name_err' => ''];
            $this->view('admin/add_role', $data);
        }
    }

    public function editRole($id = null): void {
        if (!$id) { header('Location: ' . URLROOT . '/admin/roles'); exit; }
        
        $role = $this->roleModel->getRoleById((int)$id);
        if (!$role) { header('Location: ' . URLROOT . '/admin/roles'); exit; }

        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
            $data = [
                'id' => (int)$id,
                'role' => $role,
                'role_name' => trim($_POST['role_name']),
                'description' => trim($_POST['description']),
                'name_err' => ''
            ];

            if (empty($data['role_name'])) {
                $data['name_err'] = 'Please enter a role name.';
            }

            if (empty($data['name_err'])) {
                if ($this->roleModel->updateRole($data)) {
                    systemLog('ROLE_UPDATED', "Admin updated staff role: {$data['role_name']}");
                    flash('system_msg', 'Role updated successfully.', 'success');
                    header('Location: ' . URLROOT . '/admin/roles');
                    exit;
                }
            } else {
                $this->view('admin/edit_role', $data);
            }
        } else {
            $data = ['role' => $role, 'name_err' => ''];
            $this->view('admin/edit_role', $data);
        }
    }

    public function deleteRole($id = null): void {
        if ($_SERVER['REQUEST_METHOD'] == 'POST' && $id) {
            $roleId = (int)$id;
            if ($this->roleModel->deleteRole($roleId)) {
                systemLog('ROLE_DELETED', "Admin deleted a staff role (ID: {$roleId}).");
                flash('system_msg', 'Role permanently deleted.', 'success');
            } else {
                flash('system_msg', 'Something went wrong while deleting the role.', 'error');
            }
        }
        header('Location: ' . URLROOT . '/admin/roles');
        exit;
    }
}