<?php

namespace App\Models;

use CodeIgniter\Model;

class AdmissionModel extends Model
{

    // Course Start ///////////////////////////////////////////////////

    function get_all_course()
    {
        return $this->db->table('course')
        ->get()
        ->getResult();
    }

    public function get_all_course_by_added_by($id)
    {
        return $this->db->table('course')
        ->where('course_added_by', $id)
        ->get()
        ->getResult();
    }

    public function add_course($data)
    {
        return $this->db->table('course')
        ->insert($data);
    }

    public function get_course_by_code($code)
    {
        return $this->db->table('course')
        ->where('course_code', $code)
        ->get()
        ->getRow();
    }

    public function get_course_by_id($id)
    {
        return $this->db->table('course')
        ->where('course_id', $id)
        ->get()
        ->getRow();
    }

    public function update_course($data, $code)
    {
        return $this->db->table('course')
        ->where('course_code', $code)
        ->update($data);
    }

    public function delete_course($code)
    {
        return $this->db->table('course')
        ->where('course_code', $code)
        ->delete();
    }

    // Course End ///////////////////////////////////////////////////

    // Batch Start ///////////////////////////////////////////////////

    public function get_all_batch()
    {
        return $this->db->table('course_batch')
        ->join('users', 'users.user_id = course_batch.batch_teacher')
        ->join('course', 'course.course_id = course_batch.batch_course')
        ->select('course_batch.*, users.user_name, course.course_name')
        ->get()
        ->getResult();
    }

    public function get_all_batch_by_added_by($id)
    {
        return $this->db->table('course_batch')
        ->where('batch_added_by', $id)
        ->get()
        ->getResult();
    }

    public function add_batch($data)
    {
        return $this->db->table('course_batch')
        ->insert($data);
    }

    public function get_batch_by_id($id)
    {
        return $this->db->table('course_batch')
        ->where('batch_id', $id)
        ->get()
        ->getRow();
    }

    public function get_batch_by_course_id($id)
    {
        return $this->db->table('course_batch')
        ->select('batch_id, batch_name')
        ->where('batch_course', $id)
        ->get()
        ->getResult();
    }

    public function update_batch($data, $id)
    {
        return $this->db->table('course_batch')
        ->where('batch_id', $id)
        ->update($data);
    }

    public function delete_batch($id)
    {
        return $this->db->table('course_batch')
        ->where('batch_id', $id)
        ->delete();
    }

    public function check_batch_room_availability($id)
    {
        return $this->db->table('course_batch')
        ->where('batch_room_no', $id)
        ->get()
        ->getNumRows();
    }

    // Batch End ///////////////////////////////////////////////////

    // Student Start //////////////////////////////////////////////

    public function get_all_student()
    {
        return $this->db->table('users')
        ->where('user_role', 'Student')
        ->get()
        ->getResult();
    }

    public function get_all_student_by_added_by($id)
    {
        return $this->db->table('users')
        ->where('user_added_by', $id)
        ->where('user_role', 'Student')
        ->get()
        ->getResult();
    }

    public function insert_new_student($data)
    {
        $user = $this->db->table('users')
        ->insert($data);
        return $this->db->insertID();
    }

    public function insert_data_for_course_admission($data)
    {
        return $this->db->table('student_course')
        ->insert($data);
    }

    public function update_data_for_course_admission($data, $user_id, $c_id)
    {
        return $this->db->table('student_course')
        ->where('st_course_id', $c_id)
        ->where('st_student_id', $user_id)
        ->update($data);
    }

    public function get_all_course_by_student_id($id)
    {
        return $this->db->table('student_course')
        ->join('course', 'course.course_id = student_course.st_course_id')
        ->select('*')
        ->where('st_student_id', $id)
        ->get()
        ->getResult();
    }

    public function check_course_exits_by_student_id($student_id, $course_id)
    {
        return $this->db->table('student_course')
        ->where('st_course_id', $course_id)
        ->where('st_student_id', $student_id)
        ->get()
        ->getNumRows();
    }

    public function get_student_by_rand_id($id)
    {
        return $this->db->table('users')
        ->where('user_rand_code', $id)
        ->get()
        ->getRow();
    }

    public function get_student_by_user_id($id)
    {
        return $this->db->table('users')
        ->where('user_id', $id)
        ->get()
        ->getRow();
    }

    public function update_admission($data, $id)
    {
        return $this->db->table('users')
        ->where('user_id', $id)
        ->update($data);
    }

    public function delete_admission($id)
    {
        return $this->db->table('users')
        ->where('user_rand_code', $id)
        ->delete();
    }

}
