<?php

declare(strict_types=1);

/**
 * Contract details view — Get contract API.
 *
 * @var \App\Models\ContractGetResult $result
 * @var int $contractId
 * @var array<string,mixed> $contract
 * @var array<string,mixed> $rawData
 * @var string|null $errorCode
 * @var int $httpStatus
 * @var string $backUrl
 * @var string $editUrl
 * @var string $sendUrl
 * @var string $deleteUrl
 * @var string $signToken
 * @var array<string,mixed>|null $sendResult
 * @var array<string,mixed>|null $createResult
 * @var string $signingUrl
 * @var string $signingToken
 */

require_once dirname(__DIR__, 2) . '/includes/layout.php';

render_header('title_contract_details', ['layout' => 'admin', 'page' => 'dashboard']);
render_flash();
?>

<div class="mb-4">
    <a href="<?= e($backUrl) ?>" class="inline-flex items-center gap-1 text-sm text-karvi-600 hover:text-karvi-950">
        <span class="material-symbols-rounded text-base">arrow_back</span>
        <?= e(__('back_to_dashboard')) ?>
    </a>
</div>

<?php if (is_array($createResult) && !empty($createResult['id'])): ?>
<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">check_circle</span>
        </div>
        <div>
            <h3 class="font-display font-bold text-base text-karvi-950"><?= e(__('contract_create_success_title')) ?></h3>
            <p class="text-xs text-karvi-500 mt-1"><?= e((string)($createResult['message'] ?? 'Contract created.')) ?></p>
        </div>
    </div>
    <dl class="grid grid-cols-1 sm:grid-cols-2 gap-3 text-sm mb-4">
        <div><dt class="text-xs text-karvi-500"><?= e(__('field_contract_id')) ?></dt><dd class="font-mono font-semibold text-karvi-950">#<?= e((string)$createResult['id']) ?></dd></div>
        <?php if (!empty($createResult['restaurant_name'])): ?>
        <div><dt class="text-xs text-karvi-500"><?= e(__('field_restaurant_name')) ?></dt><dd class="text-karvi-950"><?= e((string)$createResult['restaurant_name']) ?></dd></div>
        <?php endif; ?>
        <?php if (!empty($createResult['owner_name'])): ?>
        <div><dt class="text-xs text-karvi-500"><?= e(__('field_owner_name')) ?></dt><dd class="text-karvi-950"><?= e((string)$createResult['owner_name']) ?></dd></div>
        <?php endif; ?>
        <?php if (!empty($createResult['email'])): ?>
        <div><dt class="text-xs text-karvi-500"><?= e(__('field_email')) ?></dt><dd class="text-karvi-950"><?= e((string)$createResult['email']) ?></dd></div>
        <?php endif; ?>
        <?php if (!empty($createResult['contract_status'])): ?>
        <div><dt class="text-xs text-karvi-500">contract_status</dt><dd><span class="status-badge status-<?= e(strtolower((string)$createResult['contract_status'])) ?>"><?= e((string)$createResult['contract_status']) ?></span></dd></div>
        <?php endif; ?>
    </dl>
    <?php if (!empty($createResult['sign_url'])): ?>
    <div class="copy-link-box mb-3">
        <label class="form-label text-xs mb-1 block">sign_url</label>
        <input type="text" id="contractCreateSignUrl" readonly value="<?= e((string)$createResult['sign_url']) ?>" class="form-input">
        <button type="button" data-copy="contractCreateSignUrl" 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>
    <?php endif; ?>
    <?php if (!empty($createResult['sign_token'])): ?>
    <div class="copy-link-box">
        <label class="form-label text-xs mb-1 block">sign_token</label>
        <input type="text" id="contractCreateSignToken" readonly value="<?= e((string)$createResult['sign_token']) ?>" class="form-input font-mono text-xs">
        <button type="button" data-copy="contractCreateSignToken" class="btn-secondary whitespace-nowrap !py-2 !px-3 !text-sm">
            <span class="material-symbols-rounded text-base">content_copy</span>
            <?= e(__('copy_token_btn')) ?>
        </button>
    </div>
    <?php endif; ?>
</div>
<?php endif; ?>

<?php if (is_array($sendResult) && ($signingUrl !== '' || $signingToken !== '')): ?>
<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">send</span>
        </div>
        <div>
            <h3 class="font-display font-bold text-base text-karvi-950"><?= e(__('contract_send_success_title')) ?></h3>
            <p class="text-xs text-karvi-500 mt-1"><?= e((string)($sendResult['message'] ?? __('contract_send_success_sub'))) ?></p>
        </div>
    </div>
    <?php if ($signingUrl !== ''): ?>
    <div class="copy-link-box mb-3">
        <label class="form-label text-xs mb-1 block">sign_url</label>
        <input type="text" id="contractSignUrl" readonly value="<?= e($signingUrl) ?>" class="form-input">
        <button type="button" data-copy="contractSignUrl" 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>
    <?php endif; ?>
    <?php if ($signingToken !== ''): ?>
    <div class="copy-link-box mb-3">
        <label class="form-label text-xs mb-1 block">sign_token</label>
        <input type="text" id="contractSignToken" readonly value="<?= e($signingToken) ?>" class="form-input font-mono text-xs">
        <button type="button" data-copy="contractSignToken" class="btn-secondary whitespace-nowrap !py-2 !px-3 !text-sm">
            <span class="material-symbols-rounded text-base">content_copy</span>
            <?= e(__('copy_token_btn')) ?>
        </button>
    </div>
    <?php endif; ?>
    <?php if ($signingUrl !== ''): ?>
    <div class="mt-4 flex flex-wrap gap-2">
        <a href="<?= e($signingUrl) ?>" target="_blank" rel="noopener noreferrer" class="btn-primary inline-flex">
            <span class="material-symbols-rounded">open_in_new</span>
            <?= e(__('open_sign_link')) ?>
        </a>
        <button type="button" data-copy-text="<?= e($signingUrl) ?>" class="btn-secondary inline-flex">
            <span class="material-symbols-rounded">content_copy</span>
            <?= e(__('copy_link_btn')) ?>
        </button>
    </div>
    <?php endif; ?>
</div>
<?php elseif ($result->success && ($signingUrl !== '' || $signingToken !== '')): ?>
<div class="card !p-5 sm:!p-6 mb-6">
    <h3 class="font-display font-bold text-base text-karvi-950 mb-4 pb-3 border-b border-karvi-200"><?= e(__('contract_signing_link_heading')) ?></h3>
    <?php if ($signingUrl !== ''): ?>
    <div class="copy-link-box mb-3">
        <label class="form-label text-xs mb-1 block">sign_url</label>
        <input type="text" id="contractStoredSignUrl" readonly value="<?= e($signingUrl) ?>" class="form-input">
        <button type="button" data-copy="contractStoredSignUrl" 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>
    <?php endif; ?>
    <?php if ($signingToken !== ''): ?>
    <div class="copy-link-box mb-3">
        <label class="form-label text-xs mb-1 block">sign_token</label>
        <input type="text" id="contractStoredSignToken" readonly value="<?= e($signingToken) ?>" class="form-input font-mono text-xs">
        <button type="button" data-copy="contractStoredSignToken" class="btn-secondary whitespace-nowrap !py-2 !px-3 !text-sm">
            <span class="material-symbols-rounded text-base">content_copy</span>
            <?= e(__('copy_token_btn')) ?>
        </button>
    </div>
    <?php endif; ?>
    <?php if ($signingUrl !== ''): ?>
    <div class="flex flex-wrap gap-2">
        <a href="<?= e($signingUrl) ?>" target="_blank" rel="noopener noreferrer" class="btn-primary inline-flex">
            <span class="material-symbols-rounded">open_in_new</span>
            <?= e(__('open_sign_link')) ?>
        </a>
        <button type="button" data-copy-text="<?= e($signingUrl) ?>" class="btn-secondary inline-flex">
            <span class="material-symbols-rounded">content_copy</span>
            <?= e(__('copy_link_btn')) ?>
        </button>
    </div>
    <?php endif; ?>
</div>
<?php endif; ?>

<?php if (!$result->success): ?>
<div class="card !p-6">
    <div class="flex items-start gap-4">
        <div class="w-12 h-12 rounded-xl bg-karvi-950 text-white flex items-center justify-center shrink-0">
            <span class="material-symbols-rounded">error</span>
        </div>
        <div>
            <h2 class="font-display font-bold text-xl text-karvi-950 mb-1"><?= e(__('contract_details_error_title')) ?></h2>
            <p class="text-sm text-karvi-600 mb-3"><?= e($result->message) ?></p>
            <?php if (!empty($errorCode)): ?>
            <p class="text-xs font-mono text-karvi-400"><?= e((string)$errorCode) ?> · HTTP <?= e((string)$httpStatus) ?></p>
            <?php endif; ?>
        </div>
    </div>
</div>
<?php else: ?>

<div class="card !p-5 sm:!p-6 mb-6">
    <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3 mb-5 pb-4 border-b border-karvi-200">
        <div>
            <h2 class="font-display font-bold text-lg text-karvi-950">
                <?= e(__('contract_details_heading')) ?>
                <?php if ($result->id() !== null): ?>
                <span class="font-mono text-base text-karvi-500">#<?= e((string)$result->id()) ?></span>
                <?php endif; ?>
            </h2>
            <p class="text-xs text-karvi-500 mt-0.5">Get contract — GET /karvipartner/contracts/<?= e((string)$contractId) ?></p>
        </div>
        <div class="flex flex-wrap items-center gap-2 self-start">
            <?php if (!empty($contract['contract_status'])): ?>
            <span class="status-badge status-<?= e(strtolower((string)$contract['contract_status'])) ?>">
                <?= e((string)$contract['contract_status']) ?>
            </span>
            <?php endif; ?>
            <?php if (!empty($sendUrl)): ?>
            <form method="POST" action="<?= e($sendUrl) ?>" class="inline-flex">
                <button type="submit" class="btn-primary !py-2 !px-3 !text-sm">
                    <span class="material-symbols-rounded text-base">send</span>
                    <?= e(__('send_contract')) ?>
                </button>
            </form>
            <?php endif; ?>
            <?php if (!empty($editUrl)): ?>
            <a href="<?= e($editUrl) ?>" class="btn-secondary !py-2 !px-3 !text-sm">
                <span class="material-symbols-rounded text-base">edit</span>
                <?= e(__('edit_contract')) ?>
            </a>
            <?php endif; ?>
            <?php if (!empty($deleteUrl)): ?>
            <button type="button" id="contractDetailsDeleteBtn" class="btn-danger !py-2 !px-3 !text-sm">
                <span class="material-symbols-rounded text-base">delete</span>
                <?= e(__('delete_contract')) ?>
            </button>
            <?php endif; ?>
        </div>
    </div>

    <dl>
        <?php contract_details_render_field_rows([
            __('field_contract_id') => $contract['id'] ?? null,
            __('field_restaurant_name') => $contract['restaurant_name'] ?? '',
            __('field_brand') => $contract['brand'] ?? '',
            __('field_owner_name') => $contract['owner_name'] ?? '',
            __('field_company_phone') => $contract['company_phone'] ?? '',
            __('field_address') => contract_details_address($contract),
            __('field_email') => $contract['email'] ?? '',
            'contract_url' => $contract['contract_url'] ?? '',
            'sign_url' => $contract['sign_url'] ?? '',
            'sign_token' => $contract['sign_token'] ?? '',
            'contract_status' => $contract['contract_status'] ?? '',
            __('field_created_at') => $contract['created_at'] ?? '',
            __('field_updated_at') => $contract['updated_at'] ?? ($contract['modified_at'] ?? ''),
            'type' => $contract['type'] ?? '',
        ]); ?>
    </dl>
</div>

<?php if ($rawData !== []): ?>
<div class="card !p-5 sm:!p-6">
    <h3 class="font-display font-bold text-base text-karvi-950 mb-4 pb-3 border-b border-karvi-200">raw_data</h3>
    <?php contract_details_render_raw_data($rawData); ?>
</div>
<?php endif; ?>

<?php endif; ?>

<?php if ($result->success && !empty($deleteUrl)): ?>
<div id="contractDetailsDeleteModal" class="admin-modal hidden" role="dialog" aria-modal="true" aria-labelledby="contractDetailsDeleteModalTitle">
    <div class="admin-modal-backdrop" data-modal-close></div>
    <div class="admin-modal-dialog">
        <div class="admin-modal-head">
            <h3 id="contractDetailsDeleteModalTitle" class="font-display font-bold text-lg text-karvi-950"><?= e(__('delete_contract')) ?></h3>
            <p class="text-sm text-karvi-600 mt-2"><?= e(__('delete_contract_confirm_question')) ?></p>
            <?php if ($result->id() !== null): ?>
            <p class="text-xs font-mono font-semibold text-karvi-700 mt-2">#<?= e((string)$result->id()) ?></p>
            <?php endif; ?>
        </div>
        <form method="POST" action="<?= e($deleteUrl) ?>" class="admin-modal-body">
            <?php if ($signToken !== ''): ?>
            <input type="hidden" name="token" value="<?= e($signToken) ?>">
            <?php endif; ?>
            <div class="admin-modal-actions">
                <button type="button" class="btn-secondary" data-modal-cancel><?= e(__('cancel')) ?></button>
                <button type="submit" class="btn-danger">
                    <span class="material-symbols-rounded">delete</span>
                    <?= e(__('delete_contract')) ?>
                </button>
            </div>
        </form>
    </div>
</div>
<?php endif; ?>

<?php render_footer(['layout' => 'admin', 'scripts' => $result->success && !empty($deleteUrl) ? ['js/contract-details-delete.js'] : []]); ?>
