<?php
/**
 * VENDOR KYC VERIFICATION PAGE
 * File: vendor/kyc.php
 */

session_start();
require_once '../app/Core/init.php';
require_once '../app/Core/DojahKYC.php';

if (!isset($_SESSION['user_id']) || $_SESSION['user_type'] !== 'vendor') {
    header('Location: ../index.php');
    exit;
}

$page_title  = "KYC Verification";
$active_menu = "kyc";

$vendorModel = new Vendor();
$kycService  = new DojahKYC();

$vendor = $vendorModel->getByUserId($_SESSION['user_id']);
if (!$vendor) { header('Location: ../index.php'); exit; }

$user_id = (int)$_SESSION['user_id'];

// KYC status
$personal_verified = $kycService->isVendorPersonalVerified($user_id);
$business_verified = $kycService->isVendorBusinessVerified($user_id);
$personal_record   = $kycService->getVerification($user_id, DojahKYC::TYPE_VENDOR_PERSONAL);
$business_record   = $kycService->getVerification($user_id, DojahKYC::TYPE_VENDOR_BUSINESS);

$business_name_locked = (bool)($vendor['business_name_locked'] ?? false);

// Is this vendor a registered business?
$is_registered_business = !empty($vendor['rc_number'])
    || in_array(strtolower($vendor['business_type'] ?? ''), ['limited','llc','plc','registered','company']);

// Widget IDs
$personal_widget_id = $kycService->getWidgetId(DojahKYC::TYPE_VENDOR_PERSONAL);
$business_widget_id = $kycService->getWidgetId(DojahKYC::TYPE_VENDOR_BUSINESS);

// Gating status
$can_list     = $kycService->canVendorListItems($user_id);
$can_withdraw = $kycService->canVendorWithdraw($user_id, $is_registered_business);

// Flash message from kyc-callback.php
$kyc_flash = $_SESSION['kyc_flash'] ?? null;
unset($_SESSION['kyc_flash']);

include '../templates/layouts/vendor/header.php';
?>

<div class="main-content">

    <div class="page-header">
        <div class="header-left">
            <h1><i class="fas fa-shield-alt"></i> KYC Verification</h1>
            <p class="text-muted">Verify your identity and business to unlock all platform features</p>
        </div>
    </div>

    <?php if ($kyc_flash): ?>
        <div class="alert alert-<?= htmlspecialchars($kyc_flash['type']) ?> alert-dismissible fade show">
            <i class="fa fa-<?= $kyc_flash['type'] === 'success' ? 'check-circle' : 'info-circle' ?> me-2"></i>
            <?= htmlspecialchars($kyc_flash['message']) ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>

    <!-- ── Status Summary ─────────────────────────────────────────── -->
    <div class="row mb-4">
        <div class="col-md-6">
            <div class="card border-<?= $can_list ? 'success' : 'warning' ?>">
                <div class="card-body d-flex align-items-center">
                    <i class="fas fa-<?= $can_list ? 'check-circle text-success' : 'lock text-warning' ?> fa-2x me-3"></i>
                    <div>
                        <strong>Item Listing</strong><br>
                        <small class="text-muted">
                            <?= $can_list
                                ? 'You can list items for rental.'
                                : 'Complete personal verification to list items.' ?>
                        </small>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="card border-<?= $can_withdraw ? 'success' : 'warning' ?>">
                <div class="card-body d-flex align-items-center">
                    <i class="fas fa-<?= $can_withdraw ? 'check-circle text-success' : 'lock text-warning' ?> fa-2x me-3"></i>
                    <div>
                        <strong>Withdrawals</strong><br>
                        <small class="text-muted">
                            <?php if ($can_withdraw): ?>
                                You can withdraw your earnings.
                            <?php elseif (!$personal_verified): ?>
                                Complete personal verification to enable withdrawals.
                            <?php else: ?>
                                Complete business verification to enable withdrawals.
                            <?php endif; ?>
                        </small>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- ── CARD 1: Personal / Representative Verification ──────────── -->
    <div class="card mb-4">
        <div class="card-header <?= $personal_verified ? 'bg-success' : 'bg-warning' ?> text-white">
            <h5 class="mb-0">
                <i class="fas fa-user-check me-2"></i>
                Personal / Representative Verification
                <?php if ($personal_verified): ?>
                    <span class="badge bg-light text-success ms-2"><i class="fa fa-check-circle"></i> Verified</span>
                <?php else: ?>
                    <span class="badge bg-light text-dark ms-2"><i class="fa fa-exclamation-triangle"></i> Required</span>
                <?php endif; ?>
            </h5>
        </div>
        <div class="card-body">

            <?php if ($personal_verified): ?>
                <div class="alert alert-success d-flex align-items-start mb-0">
                    <i class="fa fa-check-circle fa-2x me-3 mt-1"></i>
                    <div>
                        <strong>Personal verification complete.</strong><br>
                        <span class="text-muted small">
                            Verified as:
                            <strong><?= htmlspecialchars(trim(($personal_record['verified_first_name'] ?? '') . ' ' . ($personal_record['verified_last_name'] ?? ''))) ?></strong>
                            &nbsp;|&nbsp;
                            ID Type: <strong><?= htmlspecialchars(strtoupper($personal_record['id_type'] ?? 'N/A')) ?></strong>
                            &nbsp;|&nbsp;
                            Date: <strong><?= date('M d, Y', strtotime($personal_record['updated_at'])) ?></strong>
                        </span>
                    </div>
                </div>

            <?php else: ?>
                <?php if ($personal_record && $personal_record['status'] === 'failed'): ?>
                    <div class="alert alert-warning">
                        <i class="fa fa-exclamation-triangle me-2"></i>
                        Previous attempt failed: <strong><?= htmlspecialchars($personal_record['failure_reason'] ?? '') ?></strong>.
                        Please try again.
                    </div>
                <?php endif; ?>

                <div class="alert alert-info mb-3">
                    <i class="fa fa-info-circle me-2"></i>
                    <strong>Required before listing items.</strong>
                    Verify your identity as the person/representative managing this account.
                    Your name will be locked after successful verification.
                </div>

                <?php if ($personal_widget_id): ?>
                    <script defer src="https://widget.dojah.io/websdk.js"></script>
                    <dojah-button
                        widgetId="<?= htmlspecialchars($personal_widget_id) ?>"
                        text="Verify My Identity"
                        textColor="#FFFFFF"
                        backgroundColor="#f0a500">
                    </dojah-button>
                    <p class="text-muted mt-2 mb-0" style="font-size:12px;">
                        Supports NIN, BVN, Voter's Card, Driver's License. Powered by Dojah.
                    </p>
                <?php else: ?>
                    <div class="alert alert-secondary">Widget not configured. Contact support.</div>
                <?php endif; ?>
            <?php endif; ?>

        </div>
    </div>

    <!-- ── CARD 2: Business Registration Verification ──────────────── -->
    <div class="card mb-4">
        <div class="card-header <?= $business_verified ? 'bg-success' : 'bg-primary' ?> text-white">
            <h5 class="mb-0">
                <i class="fas fa-building me-2"></i>
                Business Registration Verification
                <?php if ($business_verified): ?>
                    <span class="badge bg-light text-success ms-2"><i class="fa fa-check-circle"></i> Verified</span>
                <?php elseif ($is_registered_business): ?>
                    <span class="badge bg-light text-dark ms-2"><i class="fa fa-exclamation-triangle"></i> Required for withdrawals</span>
                <?php else: ?>
                    <span class="badge bg-light text-secondary ms-2">Optional</span>
                <?php endif; ?>
            </h5>
        </div>
        <div class="card-body">

            <?php if ($business_verified): ?>
                <div class="alert alert-success d-flex align-items-start mb-0">
                    <i class="fa fa-check-circle fa-2x me-3 mt-1"></i>
                    <div>
                        <strong>Business verification complete.</strong><br>
                        <span class="text-muted small">
                            Business: <strong><?= htmlspecialchars($business_record['verified_business_name'] ?? 'N/A') ?></strong>
                            &nbsp;|&nbsp;
                            RC: <strong><?= htmlspecialchars($business_record['verified_rc_number'] ?? 'N/A') ?></strong>
                            &nbsp;|&nbsp;
                            Date: <strong><?= date('M d, Y', strtotime($business_record['updated_at'])) ?></strong>
                        </span>
                        <?php if ($business_name_locked): ?>
                            <br><small class="text-muted"><i class="fa fa-lock"></i> Business name locked after verification</small>
                        <?php endif; ?>
                    </div>
                </div>

            <?php else: ?>
                <?php if ($business_record && $business_record['status'] === 'failed'): ?>
                    <div class="alert alert-warning">
                        <i class="fa fa-exclamation-triangle me-2"></i>
                        Previous attempt failed: <strong><?= htmlspecialchars($business_record['failure_reason'] ?? '') ?></strong>.
                        Please try again.
                    </div>
                <?php endif; ?>

                <div class="alert alert-<?= $is_registered_business ? 'warning' : 'info' ?> mb-3">
                    <i class="fa fa-<?= $is_registered_business ? 'exclamation-triangle' : 'info-circle' ?> me-2"></i>
                    <?php if ($is_registered_business): ?>
                        <strong>Required for withdrawals.</strong>
                        Your account is flagged as a registered business.
                        Verify your CAC registration before withdrawing earnings.
                        Your business name will be locked after verification.
                    <?php else: ?>
                        <strong>For registered businesses only.</strong>
                        If your business is CAC registered, verify here to enable withdrawals.
                        Your business name will be locked after verification.
                    <?php endif; ?>
                </div>

                <?php if ($business_widget_id): ?>
                    <script defer src="https://widget.dojah.io/websdk.js"></script>
                    <dojah-button
                        widgetId="<?= htmlspecialchars($business_widget_id) ?>"
                        text="Verify Business Registration"
                        textColor="#FFFFFF"
                        backgroundColor="<?= $is_registered_business ? '#dc3545' : '#0d6efd' ?>">
                    </dojah-button>
                    <p class="text-muted mt-2 mb-0" style="font-size:12px;">
                        Requires CAC registration documents. Powered by Dojah.
                    </p>
                <?php else: ?>
                    <div class="alert alert-secondary">Widget not configured. Contact support.</div>
                <?php endif; ?>
            <?php endif; ?>

        </div>
    </div>

</div>

<?php include '../templates/layouts/vendor/footer.php'; ?>