<?php

/**
 * @package Dotclear
 *
 * @copyright Olivier Meunier & Association Dotclear
 * @copyright AGPL-3.0
 */
declare(strict_types=1);

namespace Dotclear\Schema\Database\Mysqli;

use Dotclear\App;
use Dotclear\Database\AbstractHandler;
use Dotclear\Exception\DatabaseException;
use Exception;
use mysqli;
use mysqli_result;

/**
 * @class Handler
 *
 * MySQL Database handler
 */
class Handler extends AbstractHandler
{
    public const HANDLER_NAME   = 'MySQLi';

    public const HANDLER_DRIVER = 'mysqli';

    public const HANDLER_SYNTAX = 'mysql';

    /**
     * Enables weak locks if true
     */
    public static bool $weak_locks = true;

    public static function precondition(): void
    {
        if (!function_exists('mysqli_connect')) {
            throw new DatabaseException('PHP MySQLi functions are not available');
        }
    }

    /**
     * Open a DB connection
     *
     * @param      string     $host      The host
     * @param      string     $user      The user
     * @param      string     $password  The password
     * @param      string     $database  The database
     *
     * @throws     DatabaseException
     *
     * @return     mixed
     */
    public function db_connect(string $host, string $user, string $password, string $database)
    {
        self::precondition();

        $port   = abs((int) ini_get('mysqli.default_port'));
        $socket = '';
        if (str_contains($host, ':')) {
            // Port or socket given
            $bits   = explode(':', $host);
            $host   = array_shift($bits);
            $socket = array_shift($bits);
            if (abs((int) $socket) > 0) {
                // TCP/IP connection on given port
                $port   = abs((int) $socket);
                $socket = '';
            } else {
                // Socket connection
                $port = 0;
            }
        }

        if (($link = @mysqli_connect($host, $user, $password, $database, $port, $socket)) === false) {
            throw new DatabaseException('Unable to connect to database');
        }

        $this->db_post_connect($link);

        return $link;
    }

    /**
     * Open a persistant DB connection
     *
     * @param      string  $host      The host
     * @param      string  $user      The user
     * @param      string  $password  The password
     * @param      string  $database  The database
     *
     * @return     mixed
     */
    public function db_pconnect(string $host, string $user, string $password, string $database)
    {
        // No pconnect wtih mysqli, below code is for comatibility
        return $this->db_connect($host, $user, $password, $database);
    }

    /**
     * Post connection helper
     *
     * @param      mysqli  $handle   The DB handle
     */
    private function db_post_connect(mysqli $handle): void
    {
        if (version_compare($this->db_version($handle), '4.1', '>=')) {
            $this->db_query($handle, 'SET NAMES utf8');
            $this->db_query($handle, 'SET CHARACTER SET utf8');
            $this->db_query($handle, "SET COLLATION_CONNECTION = 'utf8_general_ci'");
            $this->db_query($handle, "SET COLLATION_SERVER = 'utf8_general_ci'");
            $this->db_query($handle, "SET CHARACTER_SET_SERVER = 'utf8'");
            if (version_compare($this->db_version($handle), '8.0', '<')) {
                // Setting CHARACTER_SET_DATABASE is obosolete for MySQL 8.0+
                $this->db_query($handle, "SET CHARACTER_SET_DATABASE = 'utf8'");
            }

            $handle->set_charset('utf8');
        }
    }

    /**
     * Close DB connection
     *
     * @param      mixed  $handle  The DB handle
     */
    public function db_close($handle): void
    {
        if ($handle instanceof mysqli) {
            $handle->close();
        }
    }

    /**
     * Get DB version
     *
     * @param      mixed  $handle  The handle
     */
    public function db_version($handle): string
    {
        if ($handle instanceof mysqli) {
            $v = $handle->server_version;

            return sprintf('%s.%s.%s', ($v - ($v % 10000)) / 10000, ($v - ($v % 100)) % 10000 / 100, $v % 100);
        }

        return '';
    }

    /**
     * Parse database tables path
     *
     * @param   mixed   $handle     The handle
     * @param   string  $path       The tables path
     */
    public function db_search_path($handle, $path): string
    {
        return $path;
    }

    /**
     * Execute a DB query
     *
     * @param      mixed      $handle  The handle
     * @param      string     $query   The query
     *
     * @throws     DatabaseException
     *
     * @return     mixed
     */
    public function db_query($handle, string $query)
    {
        if ($handle instanceof mysqli) {
            $res = @$handle->query($query);
            if ($res === false) {
                $msg = (string) $this->db_last_error($handle);
                if (App::config()->devMode()) {
                    $msg .= ' SQL=[' . $query . ']';
                }

                throw new DatabaseException($msg);
            }

            return $res;
        }

        return null;
    }

    /**
     * db_query() alias
     *
     * @param      mixed   $handle  The handle
     * @param      string  $query   The query
     *
     * @return     mixed
     */
    public function db_exec($handle, string $query)
    {
        return $this->db_query($handle, $query);
    }

    /**
     * Get number of fields in result
     *
     * @param      mixed  $res    The resource
     */
    public function db_num_fields($res): int
    {
        return $res instanceof mysqli_result ? $res->field_count : 0;
    }

    /**
     * Get number of rows in result
     *
     * @param      mixed  $res    The resource
     */
    public function db_num_rows($res): int
    {
        return $res instanceof mysqli_result ? (int) $res->num_rows : 0;
    }

    /**
     * Get field name in result
     *
     * @param      mixed   $res       The resource
     * @param      int     $position  The position
     */
    public function db_field_name($res, int $position): string
    {
        if ($res instanceof mysqli_result) {
            $res->field_seek($position);

            $finfo = $res->fetch_field();
            if ($finfo !== false) {
                return $finfo->name;
            }
        }

        return '';
    }

    /**
     * Get field type in result
     *
     * @param      mixed   $res       The resource
     * @param      int     $position  The position
     */
    public function db_field_type($res, int $position): string
    {
        if ($res instanceof mysqli_result) {
            $res->field_seek($position);
            $finfo = $res->fetch_field();

            if ($finfo !== false) {
                return $this->_convert_types((string) $finfo->type);
            }
        }

        return '';
    }

    /**
     * Fetch result data
     *
     * @param      mixed  $res    The resource
     *
     * @return     array<mixed>|false
     */
    public function db_fetch_assoc($res): false|array
    {
        if ($res instanceof mysqli_result) {
            $v = $res->fetch_assoc();

            return $v ?? false;
        }

        return false;
    }

    /**
     * Seek in result
     *
     * @param      mixed   $res    The resource
     * @param      int     $row    The row
     */
    public function db_result_seek($res, int $row): bool
    {
        return $res instanceof mysqli_result && $res->data_seek($row);
    }

    /**
     * Get number of affected rows in last INSERT, DELETE or UPDATE query
     *
     * @param      mixed   $handle  The DB handle
     * @param      mixed   $res     The resource
     */
    public function db_changes($handle, $res): int
    {
        return $handle instanceof mysqli ? (int) $handle->affected_rows : 0;
    }

    /**
     * Get last query error, if any
     *
     * @param      mixed       $handle  The handle
     */
    public function db_last_error($handle): false|string
    {
        if ($handle instanceof mysqli && ($e = $handle->error)) {
            return $e . ' (' . $handle->errno . ')';
        }

        return false;
    }

    /**
     * Escape a string (to be used in a SQL query)
     *
     * @param      string   $str     The string
     * @param      mixed    $handle  The DB handle
     */
    public function db_escape_string(string $str, $handle = null): string
    {
        return $handle instanceof mysqli ? $handle->real_escape_string($str) : addslashes($str);
    }

    /**
     * Locks a table
     *
     * @param      string  $table  The table
     */
    public function db_write_lock(string $table): void
    {
        try {
            $this->execute('LOCK TABLES ' . $this->escapeSystem($table) . ' WRITE');
        } catch (Exception $exception) {
            # As lock is a privilege in MySQL, we can avoid errors with weak_locks static var
            if (!self::$weak_locks) {
                throw $exception;
            }
        }
    }

    /**
     * Unlock tables
     */
    public function db_unlock(): void
    {
        try {
            $this->execute('UNLOCK TABLES');
        } catch (Exception $exception) {
            if (!self::$weak_locks) {
                throw $exception;
            }
        }
    }

    /**
     * Optimize a table
     *
     * @param      string  $table  The table
     */
    public function vacuum(string $table): void
    {
        $this->execute('OPTIMIZE TABLE ' . $this->escapeSystem($table));
    }

    /**
     * Get a date to be used in SQL query
     *
     * @param      string  $field    The field
     * @param      string  $pattern  The pattern
     */
    public function dateFormat(string $field, string $pattern): string
    {
        $pattern = str_replace('%M', '%i', $pattern);

        return 'DATE_FORMAT(' . $field . ',' . "'" . $this->escapeStr($pattern) . "')";
    }

    public function orderBy(array|string ...$args): string
    {
        $res     = [];
        $default = [
            'order'   => '',
            'collate' => false,
        ];
        foreach ($args as $v) {
            if (is_string($v)) {
                $res[] = $v;
            } elseif (!empty($v['field'])) {
                $v     = array_merge($default, $v);
                $order = strtoupper($v['order']);
                $order = ($order === 'DESC' ? $order : '');
                $res[] = $v['field'] . ($v['collate'] ? ' COLLATE utf8_unicode_ci' : '') . ' ' . $order;
            }
        }

        return $res === [] ? '' : ' ORDER BY ' . implode(',', $res) . ' ';
    }

    /**
     * Get fields concerned by lexical sort
     *
     * @param      array<string>|string  ...$args  The arguments
     */
    public function lexFields(array|string ...$args): string
    {
        $res = [];
        $fmt = '%s COLLATE utf8_unicode_ci';
        foreach ($args as $v) {
            if (is_string($v)) {
                $res[] = sprintf($fmt, $v);
            } else {
                $res = array_map(fn (string $i): string => sprintf($fmt, $i), $v);
            }
        }

        return implode(',', $res);
    }

    public function concat(string ...$args): string
    {
        return 'CONCAT(' . implode(',', $args) . ')';
    }

    /**
     * Escape a string
     *
     * @param      string  $str    The string
     */
    public function escapeSystem(string $str): string
    {
        return '`' . $str . '`';
    }

    /**
     * Get type label
     *
     * @param      string  $id     The identifier
     */
    protected function _convert_types(string $id): string
    {
        $id2type = [
            1 => 'int',
            2 => 'int',
            3 => 'int',
            8 => 'int',
            9 => 'int',

            16 => 'int', //BIT type recognized as unknown with mysql adapter

            4   => 'real',
            5   => 'real',
            246 => 'real',

            253 => 'string',
            254 => 'string',

            10 => 'date',
            11 => 'time',
            12 => 'datetime',
            13 => 'year',

            7 => 'timestamp',

            252 => 'blob',

        ];

        return $id2type[(int) $id] ?? 'unknown';
    }
}
