<?php
/** @var array|null $result */
/** @var string|null $error */
/** @var array $defaults */
use yii\helpers\Html;
use yii\helpers\Url;

?>
<div class="container mt-3">
    <h1>Generate NetSuite OAuth2 Certificate</h1>

    <?php if ($error): ?>
        <div class="alert alert-danger"><?php echo Html::encode($error); ?></div>
    <?php endif; ?>

    <form method="post" class="form">
        <?php echo Html::hiddenInput(Yii::$app->getRequest()->csrfParam, Yii::$app->getRequest()->getCsrfToken()); ?>

        <div class="mb-3">
            <label class="form-label">Save directory (optional)</label>
            <input name="saveDir" class="form-control" placeholder="<?php echo Html::encode($defaults['saveDir']); ?>" value="">
            <div class="form-text">If provided, PEM files will be written to this directory on the server.</div>
        </div>

        <div class="mb-3">
            <label class="form-label">Key size</label>
            <select name="bits" class="form-select">
                <option value="2048" <?php if($defaults['bits']==2048) echo 'selected'; ?>>2048</option>
                <option value="4096">4096</option>
            </select>
        </div>

        <div class="mb-3">
            <label class="form-label">Private key passphrase (optional)</label>
            <input type="password" name="passphrase" class="form-control" autocomplete="new-password">
            <div class="form-text">Optional passphrase to encrypt the private key. Keep it secure.</div>
        </div>

        <div class="mb-3 form-check">
            <input type="checkbox" class="form-check-input" id="overwrite" name="overwrite" value="1">
            <label class="form-check-label" for="overwrite">Overwrite existing files</label>
        </div>

        <button class="btn btn-primary" type="submit">Generate Certificate</button>
    </form>

    <?php if ($result): ?>
        <hr>
        <h3>Result</h3>
        <?php if (!empty($result['public_path'])): ?>
            <p>Public key saved to: <?php echo Html::encode($result['public_path']); ?></p>
        <?php endif; ?>
        <?php if (!empty($result['private_path'])): ?>
            <p>Private key saved to: <?php echo Html::encode($result['private_path']); ?></p>
        <?php endif; ?>
        <?php if (!empty($result['certificate_path'])): ?>
            <p>Certificate saved to: <?php echo Html::encode($result['certificate_path']); ?></p>
        <?php endif; ?>

        <div class="mb-3">
            <label class="form-label">Public PEM (paste into NetSuite Integration)</label>
            <textarea class="form-control" rows="12"><?php echo Html::encode($result['public_pem']); ?></textarea>
        </div>

        <div class="mb-3">
            <label class="form-label">X.509 Certificate (paste into NetSuite Integration)</label>
            <textarea class="form-control" rows="12"><?php echo Html::encode($result['certificate_pem'] ?? $result['public_pem']); ?></textarea>
        </div>

        <div class="mb-3">
            <label class="form-label">Private PEM (keep secure)</label>
            <textarea class="form-control" rows="12"><?php echo Html::encode($result['private_pem']); ?></textarea>
        </div>
    <?php endif; ?>
</div>
