<?php

namespace App\Controllers\Elearning;

use App\Controllers\BaseController;
use App\Models\Master\SubjectModel;
use App\Models\MateriModel;

class Materi extends BaseController
{
    protected $subject;
    protected $materi;

    public function __construct()
    {
        helper('text');
        $this->subject = new SubjectModel();
        $this->materi  = new MateriModel();
    }

    public function index()
    {
        return view('elearning/materi/materi_index', [
            'mapel'  => $this->subject->orderBy('nama_mapel')->findAll(),
            'materi' => $this->materi->getWithMapel() // 🔥 guru lihat ALL
        ]);
    }

    public function store()
    {
        $filePath = null;

        if ($file = $this->request->getFile('file')) {
            if ($file->isValid()) {
                $newName = $file->getRandomName();
                $file->move('uploads/materi', $newName);
                $filePath = 'uploads/materi/' . $newName;
            }
        }

        $this->materi->insert([
            'subject_id'   => $this->request->getPost('subject_id'),
            'kelas'        => $this->request->getPost('kelas'),
            'judul'        => $this->request->getPost('judul'),
            'deskripsi'    => $this->request->getPost('deskripsi'),
            'tipe'         => $this->request->getPost('tipe'),
            'file_path'    => $filePath,
            'is_publish'   => $this->request->getPost('is_publish') ? 1 : 0,
            'published_at' => $this->request->getPost('is_publish')
                ? date('Y-m-d H:i:s')
                : null
        ]);

        return redirect()->to('elearning/materi')
            ->with('success', 'Materi berhasil disimpan');
    }

    public function filter()
    {
        $materi = $this->materi->filterMateri(
            $this->request->getGet('keyword'),
            $this->request->getGet('mapel'),
            $this->request->getGet('kelas')
        );

        return view('elearning/materi/_materi_cards', [
            'materi' => $materi
        ]);
    }

    public function toggle()
    {
        $id = $this->request->getPost('id');
        $materi = $this->materi->find($id);

        if (!$materi) {
            return $this->response->setJSON(['status' => false]);
        }

        $status = $materi['is_publish'] ? 0 : 1;

        $this->materi->update($id, [
            'is_publish'   => $status,
            'published_at'=> $status ? date('Y-m-d H:i:s') : null
        ]);

        return $this->response->setJSON(['status' => true]);
    }

    public function view($id)
    {
        $materi = $this->materi
            ->where('id', $id)
            ->where('is_publish', 1) // 🔒 WAJIB
            ->first();

        if (!$materi) {
            return redirect()->back();
        }

        $this->materi
            ->where('id', $id)
            ->set('view_count', 'view_count+1', false)
            ->update();

        return redirect()->to(base_url($materi['file_path']));
    }
}
