<?php

namespace api\classes;

use api\classes\API;
use JetBrains\PhpStorm\NoReturn;

require_once 'API.php';

class API_platforms extends API
{
    public function getPlatforms($returnBoolean = false)
    {
        list($query, $types, $params) = $this->buildDynamicQuery('autop_platforms');

        $items = $this->generalGetFunction($query, $types, $params, $returnBoolean, 'Platform');

        return $items;
    }


    #[NoReturn]
    public function createPlatforms()
    {
        if (isset($this->data['platform_image'])) {
            $query = "INSERT INTO autop_platforms (platform_uuid, platform_name, platform_slugify, platform_description, platform_enabled, platform_image, platform_create_timestamp) VALUES (UUID(), ?, ?, ?, ?, ?, ?)";
            $stmt = $this->prepareStatement($query);
            $stmt->bind_param('sssssi', $this->data['platform_name'], $this->data['platform_slugify'], $this->data['platform_description'], $this->data['platform_enabled'], $this->data['platform_image'], time());
        } else {
            $query = "INSERT INTO autop_platforms (platform_uuid, platform_name, platform_slugify, platform_description, platform_enabled, platform_create_timestamp) VALUES (UUID(), ?, ?, ?, ?, ?)";
            $stmt = $this->prepareStatement($query);
            $stmt->bind_param('ssssi', $this->data['platform_name'], $this->data['platform_slugify'], $this->data['platform_description'], $this->data['platform_enabled'], time());
        }

        $this->executeStatement($stmt);
        $stmt->close();

        $result = $this->getPlatformSlugify();
        if ($result->num_rows === 0) {
            $this->apiOutput(500, ['error' => 'Something went wrong creating the platform on the server.'], 'error_contact_support');
        }

        $platform_data = $result->fetch_assoc();

        $this->apiOutput(200, ['success' => $platform_data], 'item_added');
    }

    public function getPlatformSlugify()
    {
        $query = "SELECT * FROM autop_platforms WHERE platform_slugify = ?";
        $stmt = $this->prepareStatement($query);
        $stmt->bind_param("s", $this->data['platform_slugify']);
        $this->executeStatement($stmt);
        return $stmt->get_result();
    }

    public function editPlatforms()
    {
        if (isset($this->data['platform_image'])) {
            $query = "UPDATE autop_platforms SET platform_name = ?, platform_description = ?, platform_enabled = ?, platform_image = ?, platform_modified_timestamp = ? WHERE platform_uuid = ?";
            $stmt = $this->prepareStatement($query);
            $stmt->bind_param("ssisis", $this->data['platform_name'], $this->data['platform_description'], $this->data['platform_enabled'], $this->data['platform_image'], time(), $this->data['platform_uuid']);
        } else {
            $query = "UPDATE autop_platforms SET platform_name = ?, platform_description = ?, platform_enabled = ?, platform_modified_timestamp = ? WHERE platform_uuid = ?";
            $stmt = $this->prepareStatement($query);
            $stmt->bind_param("ssiis", $this->data['platform_name'], $this->data['platform_description'], $this->data['platform_enabled'], time(), $this->data['platform_uuid']);
        }

        if ($this->executeStatement($stmt)) {
            $this->apiOutput(200, ['success' => 'Platform updated successfully.']);
        }

    }
}