<?php

namespace App\Controllers;

use CodeIgniter\Controller;
use CodeIgniter\HTTP\ResponseInterface;
use App\Models\BbtApiModel;

class PullUsersAPI extends Controller
{
    public function index()
    {
        $client = \Config\Services::curlrequest();
        $response = $client->request('GET', 'https://brechbilltools.com/index.php/admin/WillisPlaygroundAPI/getUsersData', [
            'headers' => [
                'X-API-KEY' => 'hK9g8D1jS0aWx3QzhK9g8D1jS0aWx3Qz',
            ],
        ]);

        $remoteData = json_decode($response->getBody(), true);

        $model = new BbtApiModel();
        $model->upsertUsers($remoteData);

        return $this->response->setJSON([
            'status'  => 'success',
            'message' => 'Users data synced successfully',
        ]);
    }
}
