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

class FAQ extends REST_Controller
{
    public function getAllFAQ_get()
    {
        $this->db->select('pertanyaan.pertanyaan, jawaban.jawaban');
        $this->db->from('pertanyaan');
        $this->db->join('jawaban', 'jawaban.id_pertanyaan = pertanyaan.id_pertanyaan');
        $data = $this->db->get();

        if ($data->num_rows() > 0) {
            $result = $data->result_array();
            $response = array(
                'status' => 200,
                'message' => "OK",
                'response' => $result
            );
        } else {
            $response = array(
                'status' => 404,
                'message' => "Data tidak ditemukan."
            );
        }

        $this->response($response);
    }

    public function faq_post()
    {
        $id_user = $this->post("id_user");
        $pertanyaan = $this->post("pertanyaan");
        $answer = "0";


        $data = array(
            'pertanyaan' => $pertanyaan,
            'id_user' => $id_user,
            'is_answer' => $answer
            // Menggunakan id_user dari tabel user
        );

        if ($this->db->insert('pertanyaan', $data)) {
            $insert_id = $this->db->insert_id(); // Mendapatkan ID konsumsi yang baru saja dimasukkan
            $response = array(
                'field' => 'id_pertanyaan',
                'message' => $pertanyaan
            );
        } else {
            $response = array(
                'status' => 500,
                'message' => "INTERNAL_SERVER_ERROR"
            );
        }

        $status_response = array(
            'status' => 200,
            'message' => "OK"
        );

        $final_response = array("response" => $response, "message" => $status_response);
        $this->response($final_response);
    }

    public function faq_get()
    {
        $id_user = $this->get("id_user");


        if (!$id_user) {
            $response = array(
                'status' => 400,
                'message' => "Bad Request. id_user dan tanggal harus diisi."
            );
        } else {
            $this->db->select('user.nama, pertanyaan.pertanyaan, jawaban.jawaban');
            $this->db->from('pertanyaan');
            $this->db->join('user', 'user.id_user = pertanyaan.id_user');
            $this->db->join('jawaban', 'jawaban.id_pertanyaan = pertanyaan.id_pertanyaan'); // Sesuaikan nama kolom yang sesuai dalam join.
            $this->db->where('user.id_user', $id_user);

            $data = $this->db->get();

            if ($data->num_rows() > 0) {
                $result = $data->result_array();
                $response = array(
                    'status' => 200,
                    'message' => "OK",
                    'data' => $result
                );
            } else {
                $response = array(
                    'status' => 404,
                    'message' => "Data tidak ditemukan."
                );
            }
        }

        $this->response($response);
    }
}
