<?php

declare(strict_types=1);

$host = getenv('POC_HOST');
$port = (int)getenv('POC_PORT');
$schema = getenv('POC_SCHEMA');
$user = getenv('POC_USER');
$password = getenv('POC_PASSWORD');
if (!$host || $port < 1 || !$schema || !$user || !$password) exit(2);
$pdo = new PDO("mysql:host=$host;port=$port;dbname=$schema;charset=utf8mb4", $user, $password, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$result = [];
foreach (['yoshop_user', 'yoshop_recharge_gift_rule'] as $table) {
    $columns = $pdo->prepare('SELECT COLUMN_NAME,COLUMN_TYPE,IS_NULLABLE,COLUMN_DEFAULT,EXTRA FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=? AND TABLE_NAME=? ORDER BY ORDINAL_POSITION');
    $columns->execute([$schema, $table]);
    $required = [];
    foreach ($columns->fetchAll(PDO::FETCH_ASSOC) as $column) {
        if ($column['IS_NULLABLE'] === 'NO' && $column['COLUMN_DEFAULT'] === null) {
            $required[] = ['name' => $column['COLUMN_NAME'], 'type' => $column['COLUMN_TYPE'], 'extra' => $column['EXTRA']];
        }
    }
    $indexes = $pdo->prepare('SELECT INDEX_NAME,COLUMN_NAME,NON_UNIQUE FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=? AND TABLE_NAME=? ORDER BY INDEX_NAME,SEQ_IN_INDEX');
    $indexes->execute([$schema, $table]);
    $unique = [];
    foreach ($indexes->fetchAll(PDO::FETCH_ASSOC) as $index) {
        if ((int)$index['NON_UNIQUE'] === 0) $unique[] = ['index' => $index['INDEX_NAME'], 'column' => $index['COLUMN_NAME']];
    }
    $result[$table] = ['required' => $required, 'unique_indexes' => $unique];
}
echo json_encode($result) . PHP_EOL;
