<?php

namespace App\Controller;

//error_reporting(error_level: E_ALL);


use PDO;
use PDOException;
use PHPUnit\Exception;

/**
 *
 */
class DatabaseConnection
{
    private PDO $dbConnection;

    const TABLE_PREFIX      = '';
    const TABLE_DOMAINS     = self::TABLE_PREFIX . "domains";
    const TABLE_NAMESERVERS = self::TABLE_PREFIX . "nameservers";
    const TABLE_PANELS      = self::TABLE_PREFIX . "panels";
    const TABLE_APIKEYS     = self::TABLE_PREFIX . "apikeys";
    const TABLE_DYNDNS      = self::TABLE_PREFIX . "dyndns";

    public function __construct(private readonly ConfigController $configController)
    {
        $dbHost     = $this->configController->getConfig(configKey: 'dbHost');
        $dbPort     = $this->configController->getConfig(configKey: 'dbPort');
        $dbDatabase = $this->configController->getConfig(configKey: 'dbDatabase');
        $dbUser     = $this->configController->getConfig(configKey: 'dbUser');
        $dbPassword = $this->configController->getConfig(configKey: 'dbPassword');

        try {
            $this->dbConnection = new PDO(
                dsn: "mysql:host=$dbHost;port=$dbPort;charset=utf8mb4;dbname=$dbDatabase",
                username: $dbUser,
                password: $dbPassword
            );
        } catch (Exception $e) {
            echo $e->getMessage() . PHP_EOL;
            echo 'Did you create the database and adjust the config file?' . PHP_EOL;
            exit(1);
        }

        if (!$this->configController->getConfig(configKey: 'test')) {
            try {
                $sql = "SHOW TABLES";

                $statement = $this->dbConnection->prepare(query: $sql);
                $statement->execute();
                $result = $statement->fetch();
                if (empty($result)) {
                    // ALTER TABLE `domains` ADD `panel_id` INT NULL AFTER `id`;
                    echo COLOR_RED . 'Error: ' . COLOR_DEFAULT . 'Cannot find tables.' . PHP_EOL;
                    echo 'Run the migration: ' . COLOR_YELLOW . './bin/console migrations:migrate' . COLOR_DEFAULT . PHP_EOL;
                }
            } catch (PDOException $exception) {
                echo $exception->getMessage() . PHP_EOL;
                echo 'Did you create the database and adjust the config file?' . PHP_EOL;
                echo PHP_EOL . 'You can create database an user via a panel or manually in mysql shell:' . PHP_EOL;
                $password = $this->generatePassword();
                echo 'Created an initial password: ' . $password . PHP_EOL;
                echo 'CREATE DATABASE bindAPI;' . PHP_EOL;
                echo "CREATE USER 'bindAPI'@'localhost' IDENTIFIED BY '$password';" . PHP_EOL;
                echo "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, INDEX, DROP, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON bindAPI.* TO 'bindAPI'@'localhost';" . PHP_EOL;
                echo 'There is no need to run FLUSH PRIVILEGES when using GRANT!' . PHP_EOL;
                exit(1);
            }
        }
    }
    function generatePassword(int $length = 8): string
    {
        $chars    = '23456789bcdfhkmnprstvzBCDFHJKLMNPRSTVZ';
        $shuffled = str_shuffle(string: $chars);
        return mb_substr(string: $shuffled, start: 0, length: $length);
    }

    public function getConnection(): PDO
    {
        return $this->dbConnection;
    }
}
