<?php
/**
 * AI Lapse Prediction Dashboard
 * View policies at risk of lapsing
 * Kenyan Pension & Life Insurance Management System
 */

require_once 'layouts/main.php';
require_once 'includes/auth.php';
require_once 'classes/LapsePredictionService.php';

requireLogin();
requirePermission('view_lapse_predictions');

$lapseService = new LapsePredictionService($conn);
$high_risk_policies = $lapseService->getHighRiskPolicies(100);

$pageTitle = 'AI Lapse Prediction';
include 'layouts/topbar.php';
?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'AI Lapse Prediction')); ?>
    <link rel="stylesheet" href="assets/css/pension-dashboard.css">
    <?php include 'layouts/head-css.php'; ?>
</head>

<body data-sidebar="dark">
    <div id="layout-wrapper">
        <?php include 'layouts/sidebar.php'; ?>
        
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <div class="welcome-header" style="background: linear-gradient(135deg, #ec4899 0%, #db2777 100%); margin-bottom: 2rem;">
                        <div class="welcome-content">
                            <div class="welcome-text">
                                <h2>AI Lapse Prediction</h2>
                                <p>Identify policies at risk of lapsing using AI analytics</p>
                            </div>
                        </div>
                    </div>

                    <div class="metrics-row">
                        <div class="metric-card claims">
                            <div class="metric-header">
                                <h6 class="metric-title">High Risk Policies</h6>
                                <span class="metric-icon"><i class="ri-alert-line"></i></span>
                            </div>
                            <div class="metric-value"><?php echo count(array_filter($high_risk_policies, function($p) { return $p['risk_score'] >= 70; })); ?></div>
                            <div class="metric-change">Requires immediate attention</div>
                        </div>

                        <div class="metric-card withdrawals">
                            <div class="metric-header">
                                <h6 class="metric-title">Medium Risk</h6>
                                <span class="metric-icon"><i class="ri-time-warning-line"></i></span>
                            </div>
                            <div class="metric-value"><?php echo count(array_filter($high_risk_policies, function($p) { return $p['risk_score'] >= 50 && $p['risk_score'] < 70; })); ?></div>
                            <div class="metric-change">Monitor closely</div>
                        </div>

                        <div class="metric-card balances">
                            <div class="metric-header">
                                <h6 class="metric-title">Total At Risk</h6>
                                <span class="metric-icon"><i class="ri-file-shield-line"></i></span>
                            </div>
                            <div class="metric-value"><?php echo count($high_risk_policies); ?></div>
                            <div class="metric-change">Policies analyzed</div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="page-card">
                                <div class="page-card-header">
                                    <h4 class="page-card-title">Policies at Risk</h4>
                                </div>
                                <div class="page-card-body">
                                    <div class="table-responsive">
                                        <table class="data-table">
                                            <thead>
                                                <tr>
                                                    <th>Policy ID</th>
                                                    <th>Risk Score</th>
                                                    <th>Risk Level</th>
                                                    <th>Risk Factors</th>
                                                    <th>Recommendations</th>
                                                    <th>Actions</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if (empty($high_risk_policies)): ?>
                                                    <tr><td colspan="6" class="text-center">No high-risk policies found</td></tr>
                                                <?php else: ?>
                                                    <?php foreach ($high_risk_policies as $policy): ?>
                                                        <tr>
                                                            <td><strong>#<?php echo $policy['policy_id']; ?></strong></td>
                                                            <td>
                                                                <div class="progress" style="height: 20px;">
                                                                    <div class="progress-bar <?php echo $policy['risk_score'] >= 70 ? 'bg-danger' : ($policy['risk_score'] >= 50 ? 'bg-warning' : 'bg-success'); ?>" 
                                                                         style="width: <?php echo $policy['risk_score']; ?>%">
                                                                        <?php echo $policy['risk_score']; ?>%
                                                                    </div>
                                                                </div>
                                                            </td>
                                                            <td>
                                                                <span class="status-badge status-<?php echo strtolower($policy['risk_level']); ?>">
                                                                    <?php echo $policy['risk_level']; ?>
                                                                </span>
                                                            </td>
                                                            <td>
                                                                <ul class="mb-0" style="font-size: 0.875rem;">
                                                                    <?php foreach ($policy['risk_factors'] as $factor): ?>
                                                                        <li><?php echo htmlspecialchars($factor); ?></li>
                                                                    <?php endforeach; ?>
                                                                </ul>
                                                            </td>
                                                            <td>
                                                                <ul class="mb-0" style="font-size: 0.875rem;">
                                                                    <?php foreach (array_slice($policy['recommendations'], 0, 2) as $rec): ?>
                                                                        <li><?php echo htmlspecialchars($rec); ?></li>
                                                                    <?php endforeach; ?>
                                                                </ul>
                                                            </td>
                                                            <td>
                                                                <a href="policies.php?id=<?php echo $policy['policy_id']; ?>" class="btn btn-sm btn-info">
                                                                    <i class="ri-eye-line"></i>
                                                                </a>
                                                            </td>
                                                        </tr>
                                                    <?php endforeach; ?>
                                                <?php endif; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
