<?php

namespace App\Controllers;

use App\Models\ApiModel;
use CodeIgniter\API\ResponseTrait;

class Api2 extends BaseController
{
    use ResponseTrait;
    public function __construct()
    {
        helper(['form', 'url']);
        $this->api_model = new ApiModel();
        $this->db = \Config\Database::connect();
        $this->session = \Config\Services::session();
        $this->validation = \Config\Services::validation();
    }

    public function index()
    {
        return view('welcome_message');
    }

    public function register()
    {
        $rules = [
            'fullname' => ["label" => "Name", "rules" => "required"],
            'email' => ["label" => "Email ID", "rules" => "required|valid_email|is_unique[users.email]"],
            'contact' => ["label" => "Contact No.", "rules" => "required|min_length[10]|max_length[10]|numeric"],
            'password' => ["label" => "Password", "rules" => "required"],
        ];

        if (!$this->validate($rules)) {
            $response = [
                'status' => 500,
                'error' => true,
                'message' => $this->validator->getErrors(),
                'data' => [],
            ];
            return $this->respondCreated($response);
        }

        $data = [
            'fullname'      => $this->request->getPost('fullname'),
            'email'         => $this->request->getPost('email'),
            'contact'       => $this->request->getPost('contact'),
            'password'      => $this->request->getPost('password'),
            'last_ip_login' => $_SERVER['REMOTE_ADDR'],
            'created_at'    => date('Y-m-d H:i:s'),
            'status'        => 1,
        ];
        
        $result = $this->db->table('users')->insert($data);
        if ($result) {
            $response = [
                'status' => 200,
                'error' => false,
                'message' => 'You Have Successfully Register.',
                'data' => [],
            ];
            return $this->respondCreated($response);
        } else {
            $response = [
                'status' => 500,
                "error" => true,
                'message' => 'Failed to create user',
                'data' => $data,
            ];
            return $this->respondCreated($response);
        }
    }

    public function login()
    {
        $rules = [
            'email' => ["label" => "Email ID", "rules" => "required|valid_email"],
            'password' => ["label" => "Password", "rules" => "required"],
        ];

        if (!$this->validate($rules)) {
            $response = [
                'status' => 500,
                'error' => true,
                'message' => $this->validator->getErrors(),
                'data' => [],
            ];
            return $this->respondCreated($response);
        }
        $userModel = new UserModel();
        $email = $this->request->getVar('email');
        $password = $this->request->getVar('password');

        $data = $userModel->where('email', $email)->get()->getRowArray();
        if ($data) {
            if (password_verify($password, $data['password'])) {
                $userdata = [
                    'user_id' => $data['id'],
                    'user_name' => $data['name'],
                    'user_email' => $data['email'],
                    'logged_in' => true,
                ];
                $this->session->set($ses_data);

                $response = [
                    'status' => 200,
                    'error' => false,
                    'messages' => 'User logged In successfully',
                    'data' => $userdata
                ];
                return $this->respondCreated($response);
            } else {
                $response = [
                    'status' => 500,
                    'error' => true,
                    'messages' => 'Incorrect details',
                    'data' => [],
                ];
                return $this->respondCreated($response);
            }
        } else {
            $response = [
                'status' => 500,
                'error' => true,
                'messages' => 'User not found',
                'data' => [],
            ];
            return $this->respondCreated($response);
        }
    }

    public function getCourse()
    {
        $result = $this->db->table('courses')->get()->getResultArray();
        if ($result) {
            $response = [
                'status' => 200,
                'error' => false,
                'messages' => 'Success',
                'data' => $result
            ];
            return $this->respondCreated($response);
        }else{
            $response = [
                'status' => 500,
                'error' => true,
                'messages' => 'Failed',
                'data' => [],
            ];
            return $this->respondCreated($response);
        }
    }

    public function getPost($id)
    {
        $result = $this->db->table('post_details')->where('course_id',$id)->get()->getResultArray();
        if ($result) {
            $response = [
                'status' => 200,
                'error' => false,
                'messages' => 'Success',
                'data' => $result
            ];
            return $this->respondCreated($response);
        }else{
            $response = [
                'status' => 500,
                'error' => true,
                'messages' => 'Failed',
                'data' => [],
            ];
            return $this->respondCreated($response);
        }
    }
}
