<?php
/**
 * app/plugins/bookmarks/controllers/api/bookmark_folders.php
 *
 * This is the api bookmark folders 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\Folders;

class BookmarkFolders extends ApiController {
    protected static $folders;

    public function __construct() {
        header('Access-Control-Allow-Origin: *');
        parent::__construct();
        self::$folders = new Folders;
    }

    public function create() {
        $user = self::$authToken->createdBy;

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

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

    public function list( $id = '' ) {
        $user = self::$authToken->createdBy;
        $folders = self::$folders->bySpecificUser( $user );

        if ( ! $folders ) {
            $responseType = 'error';
            $response = 'There was an error creating your folder.';
        } else {
            $responseType = 'folders';
            $response = $folders;
        }
        Views::view( 'api.response', ['response' => json_encode( [ $responseType => $response ], true )]);
    }
}
