<?php

namespace api\classes;

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

require_once 'API.php';

class API_vendors extends API
{
    public function getVendors($returnBoolean = false)
    {
        list($query, $types, $params) = $this->buildDynamicQuery('autop_vendors');

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

        return $items;
    }

    #[NoReturn]
    public function createVendor()
    {
        if (isset($this->data['vendor_image'])) {
            $query = "INSERT INTO autop_vendors (vendor_uuid, vendor_name, vendor_slugify, vendor_description, vendor_enabled, vendor_create_timestamp, vendor_image) VALUES (UUID(), ?, ?, ?, ?, ?, ?)";
            $stmt = $this->prepareStatement($query);
            $stmt->bind_param('ssssis', $this->data['vendor_name'], $this->data['vendor_slugify'], $this->data['vendor_description'], $this->data['vendor_enabled'], time(), $this->data['vendor_image']);
        } else {
            $query = "INSERT INTO autop_vendors (vendor_uuid, vendor_name, vendor_slugify, vendor_description, vendor_enabled, vendor_create_timestamp) VALUES (UUID(), ?, ?, ?, ?, ?)";
            $stmt = $this->prepareStatement($query);
            $stmt->bind_param('ssssi', $this->data['vendor_name'], $this->data['vendor_slugify'], $this->data['vendor_description'], $this->data['vendor_enabled'], time());
        }

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

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

        $platform_data = $result->fetch_assoc();

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

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

    public function editVendor()
    {
        if (isset($this->data['vendor_image'])) {
            $query = "UPDATE autop_vendors SET vendor_name = ?, vendor_description = ?, vendor_enabled = ?, vendor_image = ?, vendor_modified_timestamp = ? WHERE vendor_uuid = ?";
            $stmt = $this->prepareStatement($query);
            $stmt->bind_param("ssisis", $this->data['vendor_name'], $this->data['vendor_description'], $this->data['vendor_enabled'], $this->data['vendor_image'], time(), $this->data['vendor_uuid']);
        } else {
            $query = "UPDATE autop_vendors SET vendor_name = ?, vendor_description = ?, vendor_enabled = ?, vendor_modified_timestamp  = ? WHERE vendor_uuid = ?";
            $stmt = $this->prepareStatement($query);
            $stmt->bind_param("ssiis", $this->data['vendor_name'], $this->data['vendor_description'], $this->data['vendor_enabled'], time(), $this->data['vendor_uuid']);
        }

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

    }
}