<?php

use api\classes\API_platforms;
use api\classes\imageProcessor;

session_start();
require_once "{$_SERVER['DOCUMENT_ROOT']}/api/classes/API_platforms.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/api/classes/imageProcessor.php";

$API_platforms = new API_platforms();

if ($API_platforms->request_method === 'GET') {
    # get all the platforms
    $API_platforms->checkPermissions('admin-platforms', 'RO');

} elseif ($API_platforms->request_method === 'POST') {
    # create a new platform

    $API_platforms->checkPermissions('admin-platforms', 'RW');

    try {
        $imageProcessor = new imageProcessor('platform_image');
        $imageProcessor->imageRestrictions = [
            'min_width' => 200,
            'max_width' => 200,
            'min_height' => 200,
            'max_height' => 200,
            'square' => true,
            'allowed_types' => ['image/png'],
            'max_size_kb' => 1024
        ];

        $imageProcessor->validateAndProcess();
        $finalImageData = $imageProcessor->returnBase64image();
    } catch (Exception $e) {
        $API_platforms->apiOutput(401, ['error' => 'Error: ' . $e->getMessage()]);
    }

    $API_platforms->postedData['platform_image'] = $finalImageData;

    $requiredFields = [
        'platform_name' => ['type' => 'string'],
        'platform_slugify' => ['type' => 'slugify'],
        'platform_enabled' => ['type' => 'boolean'],
        'platform_description' => ['type' => 'string'],
    ];
    $optionalFields = ['platform_image' => ['type' => 'string']];

    $API_platforms->validateData($requiredFields, $optionalFields);

    $API_platforms->createPlatforms();
} elseif ($API_platforms->request_method === 'PUT') {

    # edit a platform
    $API_platforms->checkPermissions('admin-platforms', 'RW');

    try {
        $imageProcessor = new imageProcessor('platform_image');
        $imageProcessor->imageRestrictions = [
            'min_width' => 200,
            'max_width' => 200,
            'min_height' => 200,
            'max_height' => 200,
            'square' => true,
            'allowed_types' => ['image/png'],
            'max_size_kb' => 1024
        ];
        $imageProcessor->validateAndProcess();
        $finalImageData = $imageProcessor->returnBase64image();
    } catch (Exception $e) {
        $API_platforms->apiOutput(401, ['error' => 'Error: ' . $e->getMessage()]);
    }

    if ($finalImageData) {
        $API_platforms->postedData['platform_image'] = $finalImageData;
    }

    $requiredFields = [
        'platform_uuid' => ['type' => 'uuid'],
        'platform_name' => ['type' => 'string'],
        'platform_enabled' => ['type' => 'boolean'],
        'platform_description' => ['type' => 'string'],
    ];
    $optionalFields = ['platform_image' => ['type' => 'string']];


    $API_platforms->validateData($requiredFields, $optionalFields);

    $_GET['builder'] = [1 => ['where' => [0 => 'platform_uuid', 1 => $API_platforms->data['platform_uuid']]]];
    $API_platforms->getPlatforms();

    $API_platforms->editPlatforms();

}