<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;
use App\Models\AiKbTpModel;

class TpManagement extends BaseController
{
    protected $tpModel;

    public function __construct()
    {
        $this->tpModel = new AiKbTpModel();
    }

    public function index()
    {
        if (!session()->get('logged_in')) {
            return redirect()->to('/login');
        }

        if (!in_array(session()->get('user_role'), ['superadmin', 'admin'])) {
            return redirect()->to('/dashboard')->with('error', 'Akses ditolak!');
        }

        $data = [
            'title' => 'Manajemen Master Materi & TP',
            'tps'   => $this->tpModel->getDetailedList()
        ];

        return view('admin/tp_management/index', $data);
    }

    public function delete($id)
    {
        if (!session()->get('logged_in')) {
            return redirect()->to('/login');
        }

        if (!in_array(session()->get('user_role'), ['superadmin', 'admin'])) {
            return redirect()->to('/dashboard')->with('error', 'Akses ditolak!');
        }

        if ($this->tpModel->delete($id)) {
            return redirect()->to('/admin/tp-management')->with('success', 'Data Master TP berhasil dihapus.');
        }

        return redirect()->to('/admin/tp-management')->with('error', 'Gagal menghapus data Master TP.');
    }
}
