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

/**
 * @property CI_Db $db
 * @property CI_Session $session
 * @property CI_Form_validation $form_validation
 * @property CI_Input $input
 * @property CI_Wali_santri_model $wali_santri_model
 * @property CI_Santri_model $santri_model
 * @property CI_Sejarah_model $sejarah_model
 */

class FCM extends CI_Controller
{
    public function saveToken()
    {
        $json = json_decode(file_get_contents('php://input'), true);
        $id = $json['id_wali_santri'];
        $token = $json['token'];

        // Cek jika token sudah ada untuk wali yang sama
        $this->db->where('id_wali_santri', $id);
        $exist = $this->db->get('fcm_tokens')->row();

        if ($exist) {
            $this->db->where('id_wali_santri', $id);
            $this->db->update('fcm_tokens', ['token' => $token]);
        } else {
            $this->db->insert('fcm_tokens', [
                'id_wali_santri' => $id,
                'token' => $token
            ]);
        }
        echo json_encode(['status' => true, 'message' => 'Token berhasil disimpan']);
    }
}
