<?php
/**
 * ADMIN DISPUTES PAGE
 * File: admin/disputes.php
 * View and manage all disputes
 */

require_once '../app/Core/init.php';
Auth::requireAdmin();

$page_title = "Disputes Management";
$active_menu = "disputes";

$db = Database::getInstance();

// Get all disputes
$disputes_sql = "SELECT d.*, 
                 o.order_number,
                 i.title as item_title,
                 v.business_name as vendor_name,
                 CONCAT(c.first_name, ' ', c.last_name) as customer_name,
                 (SELECT COUNT(*) FROM dispute_messages dm WHERE dm.dispute_id = d.id AND dm.sender_type != 'admin' AND dm.is_read_by_admin = 0) as unread_count,
                 (SELECT message FROM dispute_messages dm WHERE dm.dispute_id = d.id ORDER BY dm.created_at DESC LIMIT 1) as last_message,
                 (SELECT created_at FROM dispute_messages dm WHERE dm.dispute_id = d.id ORDER BY dm.created_at DESC LIMIT 1) as last_message_time
                 FROM disputes d
                 JOIN orders o ON d.order_id = o.id
                 JOIN items i ON o.item_id = i.id
                 JOIN vendors v ON o.vendor_id = v.id
                 JOIN customers c ON o.customer_id = c.id
                 ORDER BY d.created_at DESC";

$disputes = $db->fetchAll($disputes_sql);

$page_title = 'All Disputes - ' . APP_NAME;
include '../templates/layouts/admin/head.php';
?>

<div class="main-content">
    <?php include '../templates/layouts/admin/header.php'; ?>

    <!-- Page Header -->
    <div class="page-header mb-4">
        <h1><i class="fas fa-gavel"></i> Disputes Management</h1>
        <p class="text-muted">Monitor and mediate all platform disputes</p>
        <div style="display: flex; gap: 8px; font-size: 14px; color: #666;">
            <a href="index.php" style="color: var(--primary); text-decoration: none;">Home</a>
            <span>/</span>
            <span>Disputes</span>
        </div>
    </div>

    <!-- Content -->
    <div style="padding: 30px;">

    <!-- Stats -->
    <div class="row mb-4">
        <?php
        $open_count = 0;
        $in_progress_count = 0;
        $resolved_count = 0;
        $closed_count = 0;
        foreach ($disputes as $d) {
            if ($d['status'] === 'open') $open_count++;
            if ($d['status'] === 'in_progress') $in_progress_count++;
            if ($d['status'] === 'resolved') $resolved_count++;
            if ($d['status'] === 'closed') $closed_count++;
        }
        ?>
        <div class="col-md-3">
            <div class="stat-card">
                <div class="stat-icon bg-danger"><i class="fas fa-exclamation-triangle"></i></div>
                <div class="stat-details">
                    <h3><?= $open_count ?></h3>
                    <p>Open Disputes</p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="stat-card">
                <div class="stat-icon bg-info"><i class="fas fa-tasks"></i></div>
                <div class="stat-details">
                    <h3><?= $in_progress_count ?></h3>
                    <p>In Progress</p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="stat-card">
                <div class="stat-icon bg-success"><i class="fas fa-check-circle"></i></div>
                <div class="stat-details">
                    <h3><?= $resolved_count ?></h3>
                    <p>Resolved</p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="stat-card">
                <div class="stat-icon bg-secondary"><i class="fas fa-archive"></i></div>
                <div class="stat-details">
                    <h3><?= $closed_count ?></h3>
                    <p>Closed</p>
                </div>
            </div>
        </div>
    </div>

    <div class="card">
        <div class="card-header">
            <h5 class="mb-0">All Platform Disputes</h5>
        </div>
        <div class="card-body p-0">
            <?php if (empty($disputes)): ?>
                <div class="text-center py-5">
                    <i class="fas fa-clipboard-list fa-3x text-muted mb-3"></i>
                    <p class="text-muted">No disputes found</p>
                </div>
            <?php else: ?>
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead>
                            <tr>
                                <th>Dispute #</th>
                                <th>Order</th>
                                <th>Customer</th>
                                <th>Vendor</th>
                                <th>Type</th>
                                <th>Raised By</th>
                                <th>Status</th>
                                <th>Last Activity</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($disputes as $dispute): ?>
                                <?php
                                $status_colors = [
                                    'open' => 'danger',
                                    'in_progress' => 'info',
                                    'resolved' => 'success',
                                    'closed' => 'secondary'
                                ];
                                $color = $status_colors[$dispute['status']] ?? 'secondary';
                                ?>
                                <tr class="<?= $dispute['unread_count'] > 0 ? 'table-warning' : '' ?>">
                                    <td>
                                        <strong><?= esc($dispute['dispute_number']) ?></strong>
                                        <?php if ($dispute['unread_count'] > 0): ?>
                                            <br><span class="badge bg-danger"><?= $dispute['unread_count'] ?> unread</span>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <a href="order-details.php?id=<?= $dispute['order_id'] ?>">
                                            <?= esc($dispute['order_number']) ?>
                                        </a><br>
                                        <small class="text-muted"><?= esc($dispute['item_title']) ?></small>
                                    </td>
                                    <td><?= esc($dispute['customer_name']) ?></td>
                                    <td><?= esc($dispute['vendor_name']) ?></td>
                                    <td>
                                        <small><?= ucwords(str_replace('_', ' ', $dispute['dispute_type'])) ?></small>
                                    </td>
                                    <td>
                                        <span class="badge bg-secondary">
                                            <?= ucfirst($dispute['raised_by_type']) ?>
                                        </span>
                                    </td>
                                    <td>
                                        <span class="badge bg-<?= $color ?>">
                                            <?= ucfirst(str_replace('_', ' ', $dispute['status'])) ?>
                                        </span>
                                    </td>
                                    <td>
                                        <?php if ($dispute['last_message_time']): ?>
                                            <small><?= time_ago($dispute['last_message_time']) ?></small>
                                        <?php else: ?>
                                            <small class="text-muted">No activity</small>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <a href="dispute-details.php?id=<?= $dispute['id'] ?>" class="btn btn-sm btn-primary">
                                            <i class="fas fa-gavel"></i> Mediate
                                        </a>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

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