<?php

/*
 * Copyright 2005 - 2025 Centreon (https://www.centreon.com/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * For more information : contact@centreon.com
 *
 */

require_once _CENTREON_PATH_ . '/www/class/centreonDB.class.php';
require_once _CENTREON_PATH_ . '/www/api/class/webService.class.php';

class CentreonOpenticketHistory extends CentreonWebService
{
    /** @var Centreon */
    public $centreon;

    /** @var CentreonDB */
    public $pearDBMonitoring;

    /** @var CentreonDB|null */
    protected $pearDB;

    /**
     * @global Centreon $centreon
     */
    public function __construct()
    {
        global $centreon;

        $this->centreon = $centreon;
        $this->pearDBMonitoring = new CentreonDB('centstorage');

        parent::__construct();
    }

    public function postSaveHistory()
    {
        /* {
         *   "ticket_id": "199",
         *   "timestamp": 1498125177,
         *   "user": "toto",
         *   "subject": "mon sujet",
         *   "links": [
         *      { "hostname": "plop", "service_description": "caca", "service_state": "1" },
         *      { "hostname": "plop", "service_description": "caca2", "service_state": "2" }
         *   ]
         * }
         */
        $result = ['code' => 0, 'message' => 'history saved'];

        if (! isset($this->arguments['ticket_id'])
            || ! isset($this->arguments['user'])
            || ! isset($this->arguments['subject'])
            || ! is_array($this->arguments['links'])
        ) {
            return ['code' => 1, 'message' => 'parameters missing'];
        }
        $timestamp = time();
        if (isset($this->arguments['timestamp'])) {
            $timestamp = $this->arguments['timestamp'];
        }

        $links_ok = [];
        $stmt_host = $this->pearDBMonitoring->prepare('SELECT host_id FROM hosts WHERE name = ?');
        $stmt_service = $this->pearDBMonitoring->prepare(
            'SELECT hosts.host_id, services.service_id
            FROM hosts, services
            WHERE hosts.name = ?
            AND hosts.host_id = services.host_id
            AND services.description = ?'
        );
        foreach ($this->arguments['links'] as $link) {
            if (isset($link['hostname'], $link['service_description'])) {
                $res = $this->pearDBMonitoring->execute(
                    $stmt_service,
                    [$link['hostname'], $link['service_description']]
                );
                if ($row = $stmt_service->fetch()) {
                    $links_ok[] = array_merge(
                        $link,
                        ['service_id' => $row['service_id'], 'host_id' => $row['host_id']]
                    );
                }
            } elseif (isset($link['hostname'])) {
                $res = $this->pearDBMonitoring->execute($stmt_host, [$link['hostname']]);
                if ($row = $stmt_host->fetch()) {
                    $links_ok[] = array_merge($link, ['host_id' => $row['host_id']]);
                }
            }
        }

        if (count($links_ok) == 0) {
            return ['code' => 1, 'message' => 'links parameters missing or wrong'];
        }

        // Insert data
        $this->pearDBMonitoring->beginTransaction();
        $res = $this->pearDBMonitoring->query(
            'INSERT INTO mod_open_tickets (`timestamp`, `user`, `ticket_value`) VALUES ('
                . $this->pearDBMonitoring->quote($timestamp) . ', '
                . $this->pearDBMonitoring->quote($this->arguments['user']) . ', '
                . $this->pearDBMonitoring->quote($this->arguments['ticket_id'])
            . ')'
        );
        if (PEAR::isError($res) === true) {
            return ['code' => 1, 'message' => 'cannot insert in database'];
        }

        // Get Autoincrement
        $res = $this->pearDBMonitoring->query('SELECT LAST_INSERT_ID() AS last_id');
        if (PEAR::isError($res) === true || ! ($row = $res->fetch())) {
            return ['code' => 1, 'message' => 'database issue'];
        }
        $auto_ticket = $row['last_id'];

        // Insert data
        $res = $this->pearDBMonitoring->query(
            'INSERT INTO mod_open_tickets_data (`ticket_id`, `subject`) VALUES ('
                . $this->pearDBMonitoring->quote($auto_ticket) . ', '
                . $this->pearDBMonitoring->quote($this->arguments['subject'])
            . ')'
        );
        if (PEAR::isError($res) === true) {
            return ['code' => 1, 'message' => 'cannot insert in database'];
        }

        // Insert data
        foreach ($links_ok as $link_ok) {
            $names = '';
            $values = '';
            $append = '';
            foreach ($link_ok as $key => $value) {
                $names .= $append . $key;
                $values .= $append . $this->pearDBMonitoring->quote($value);
                $append = ', ';
            }
            $res = $this->pearDBMonitoring->query(
                "INSERT INTO mod_open_tickets_link (`ticket_id`, {$names}) VALUES ("
                    . $this->pearDBMonitoring->quote($auto_ticket) . ', '
                    . $values
                . ')'
            );
            if (PEAR::isError($res) === true) {
                return ['code' => 1, 'message' => 'cannot insert in database'];
            }
        }

        $this->pearDBMonitoring->commit();

        return $result;
    }
}
