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

class Admin extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model(['User_model', 'Ebook_model']);
        $this->_check_admin();
    }

    private function _check_admin() {
        if (!$this->session->userdata('user_id')) {
            redirect('login');
        }
        if ($this->session->userdata('role') !== 'admin') {
            redirect('user/dashboard');
        }
    }

    public function index() {
        $data['title']         = 'Admin Dashboard';
        $data['user']          = $this->session->userdata();
        $data['total_ebook']   = $this->Ebook_model->count_all();
        $data['total_user']    = $this->User_model->count_all();
        $data['categories']    = $this->Ebook_model->count_by_category();
        $data['recent_ebooks'] = $this->Ebook_model->get_recent(5);
        $this->load->view('admin/dashboard', $data);
    }

    public function ebooks() {
        $data['title']      = 'Kelola E-Book';
        $data['user']       = $this->session->userdata();
        $data['categories'] = $this->Ebook_model->get_categories();
        $this->load->view('admin/ebooks', $data);
    }

    public function api_docs() {
        $data['title'] = 'API Documentation';
        $data['user']  = $this->session->userdata();
        $this->load->view('admin/api_docs', $data);
    }
}
