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

class Notulen_model extends CI_Model {

    private $table         = 'notulen_rapat';
    private $table_peserta = 'notulen_peserta';
    private $table_poin    = 'notulen_poin';

    // ─────────────────────────────────────────────
    // GET ALL dengan filter
    // ─────────────────────────────────────────────
    public function get_all($filters = []) {
        $this->db->select('nr.*, u.nama_lengkap AS created_by_name');
        $this->db->from($this->table . ' nr');
        $this->db->join('users u', 'u.id = nr.created_by', 'left');

        if (!empty($filters['status'])) {
            $this->db->where('nr.status', $filters['status']);
        }
        if (!empty($filters['tim'])) {
            $this->db->where('nr.tim', $filters['tim']);
        }
        if (!empty($filters['dari'])) {
            $this->db->where('nr.tanggal >=', $filters['dari']);
        }
        if (!empty($filters['sampai'])) {
            $this->db->where('nr.tanggal <=', $filters['sampai']);
        }
        if (!empty($filters['q'])) {
            $q = $this->db->escape_like_str($filters['q']);
            $this->db->group_start();
                $this->db->like('nr.judul_rapat', $q);
                $this->db->or_like('nr.moderator', $q);
                $this->db->or_like('nr.notulis', $q);
                $this->db->or_like('nr.tempat', $q);
                $this->db->or_like('nr.tim', $q);
            $this->db->group_end();
        }

        $this->db->order_by('nr.tanggal', 'DESC');
        $this->db->order_by('nr.created_at', 'DESC');
        return $this->db->get()->result();
    }

    // ─────────────────────────────────────────────
    // GET BY ID
    // ─────────────────────────────────────────────
    public function get_by_id($id) {
        $this->db->select('nr.*, u.nama_lengkap AS created_by_name');
        $this->db->from($this->table . ' nr');
        $this->db->join('users u', 'u.id = nr.created_by', 'left');
        $this->db->where('nr.id', $id);
        return $this->db->get()->row();
    }

    // ─────────────────────────────────────────────
    // PESERTA
    // ─────────────────────────────────────────────
    public function get_peserta($notulen_id) {
        $this->db->where('notulen_id', $notulen_id);
        $this->db->order_by('urutan', 'ASC');
        return $this->db->get($this->table_peserta)->result();
    }

    public function insert_peserta($data) {
        return $this->db->insert($this->table_peserta, $data);
    }

    public function delete_peserta($notulen_id) {
        $this->db->where('notulen_id', $notulen_id);
        return $this->db->delete($this->table_peserta);
    }

    // ─────────────────────────────────────────────
    // POIN PEMBAHASAN
    // ─────────────────────────────────────────────
    public function get_poin($notulen_id) {
        $this->db->where('notulen_id', $notulen_id);
        $this->db->order_by('urutan', 'ASC');
        return $this->db->get($this->table_poin)->result();
    }

    public function insert_poin($data) {
        return $this->db->insert($this->table_poin, $data);
    }

    public function delete_poin($notulen_id) {
        $this->db->where('notulen_id', $notulen_id);
        return $this->db->delete($this->table_poin);
    }

    // ─────────────────────────────────────────────
    // CRUD UTAMA
    // ─────────────────────────────────────────────
    public function insert($data) {
        $data['created_at'] = date('Y-m-d H:i:s');
        $data['updated_at'] = date('Y-m-d H:i:s');
        return $this->db->insert($this->table, $data);
    }

    public function update($id, $data) {
        $data['updated_at'] = date('Y-m-d H:i:s');
        $this->db->where('id', $id);
        return $this->db->update($this->table, $data);
    }

    public function delete($id) {
        // Hapus relasi dulu
        $this->delete_peserta($id);
        $this->delete_poin($id);
        $this->db->where('id', $id);
        return $this->db->delete($this->table);
    }

    // ─────────────────────────────────────────────
    // STATISTIK
    // ─────────────────────────────────────────────
    public function get_statistik() {
        return [
            'total'    => $this->db->count_all($this->table),
            'draft'    => $this->db->where('status', 'draft')->count_all_results($this->table),
            'menunggu' => $this->db->where('status', 'menunggu')->count_all_results($this->table),
            'final'    => $this->db->where('status', 'final')->count_all_results($this->table),
        ];
    }

    // ─────────────────────────────────────────────
    // HELPER: daftar tim unik
    // ─────────────────────────────────────────────
    public function get_tim_list() {
        $this->db->select('tim');
        $this->db->distinct();
        $this->db->order_by('tim', 'ASC');
        $rows = $this->db->get($this->table)->result();
        return array_column($rows, 'tim');
    }
}
