<?php
/**
 * app/plugins/bookmarks/controllers/api/bookmarks.php
 *
 * This is the api bookmarks controller.
 *
 * @version 3.0
 * @author  Joey Kimsey <Joey@thetempusproject.com>
 * @link    https://TheTempusProject.com
 * @license https://opensource.org/licenses/MIT [MIT LICENSE]
 */
namespace TheTempusProject\Controllers\Api;

use TheTempusProject\Models\User;
use TheTempusProject\Classes\ApiController;
use TheTempusProject\Houdini\Classes\Views;
use TheTempusProject\Canary\Bin\Canary as Debug;
use TheTempusProject\Bedrock\Functions\Input;
use TheTempusProject\Models\Bookmarks as Bookmark;

class Bookmarks extends ApiController {
    protected static $bookmarks;

    public function __construct() {
        parent::__construct();
        self::$bookmarks = new Bookmark;
    }

    public function create() {
        header('Access-Control-Allow-Origin: *');

        $user = self::$authToken->createdBy;

        $payload = @file_get_contents('php://input');
        $payload = json_decode( $payload, true );

        $result = self::$bookmarks->create(
            $payload['name'],
            $payload['url'],
            $payload['folder'] ?? 0,
            $payload['notes'] ?? '',
            $payload['color'] ?? 'default',
            $payload['privacy'] ?? 'private',
            'external',
            $user
        );
        if ( ! $result ) {
            $responseType = 'error';
            $response = 'There was an error creating your folder.';
        } else {
            $responseType = 'data';
            $response = $result;
        }
        Views::view( 'api.response', ['response' => json_encode( [ $responseType => $response ], true )]);
    }
}