<?php

declare(strict_types=1);

require_once __DIR__ . '/../includes/layout.php';
require_once __DIR__ . '/../includes/api_service.php';
require_once __DIR__ . '/../includes/invoice_contract.php';

auth_require();

$mode = $invoiceLinkMode ?? null;
$config = is_string($mode) ? invoice_contract_mode_config($mode) : null;
$listPath = $config['list_path'] ?? '/admin/dashboard';
$createPath = $config['create_path'] ?? '/admin/create';
$pageTitleKey = $config['form_title_key'] ?? 'contract_ready';
$pageKey = $config['create_page'] ?? 'create';

$token = trim((string)($_GET['token'] ?? ''));
$contract = null;
$customerLink = '';

if ($token !== '') {
    try {
        $contract = api_find_contract($token);
    } catch (Throwable) {
        $contract = null;
    }
}

if ($contract !== null) {
    $customerLink = contract_public_sign_url_from_record($contract);
} elseif ($token !== '') {
    $customerLink = contract_public_sign_url($token);
    if ($customerLink === '') {
        $customerLink = contract_url($token);
    }
}

if ($customerLink === '') {
    set_flash('Contract link could not be loaded. Create a new contract and try again.', 'error');
    redirect($createPath);
}

render_header($pageTitleKey, ['layout' => 'admin', 'page' => $pageKey]);
render_flash();
?>

<div class="invoice-page-head mb-6">
    <a href="<?= e(app_path($listPath)) ?>" class="btn-secondary invoice-back-btn">
        <span class="material-symbols-rounded">arrow_back</span>
        <?= e(__('back')) ?>
    </a>
    <h2 class="font-display font-bold text-xl sm:text-2xl text-karvi-950 text-center flex-1"><?= e(__('contract_ready')) ?></h2>
    <div class="invoice-page-head-spacer" aria-hidden="true"></div>
</div>

<div class="link-success-box mb-6 animate-fade-in">
    <div class="flex items-start gap-4 mb-4">
        <div class="w-10 h-10 rounded-xl bg-karvi-950 text-white flex items-center justify-center shrink-0">
            <span class="material-symbols-rounded">link</span>
        </div>
        <div>
            <h3 class="font-display font-bold text-base text-karvi-950"><?= e(__('contract_ready')) ?></h3>
            <p class="text-xs text-karvi-500 mt-1"><?= e(__('contract_ready_sub')) ?></p>
        </div>
    </div>
    <div class="copy-link-box">
        <input type="text" id="contractLink" readonly value="<?= e($customerLink) ?>" class="form-input">
        <button type="button" data-copy="contractLink" class="btn-secondary whitespace-nowrap !py-2 !px-3 !text-sm">
            <span class="material-symbols-rounded text-base">content_copy</span>
            <?= e(__('copy_link_btn')) ?>
        </button>
    </div>
    <div class="flex flex-col sm:flex-row gap-3 mt-4">
        <a href="<?= e($customerLink) ?>" target="_blank" rel="noopener" class="btn-primary inline-flex justify-center">
            <span class="material-symbols-rounded">open_in_new</span>
            <?= e(__('open_link')) ?>
        </a>
        <a href="<?= e(app_path($createPath)) ?>" class="btn-secondary inline-flex justify-center">
            <span class="material-symbols-rounded">add</span>
            <?= e(__('nav_new_contract')) ?>
        </a>
    </div>
</div>

<?php render_footer(['layout' => 'admin']); ?>
