<?php
/**
 * CUSTOMER DISPUTES/REPORTS PAGE
 * File: customer/disputes.php
 * View all reported issues
 */

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

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

// Page variables
$page_title = "Disputes";
$active_menu = "disputes";


$customer_user_id = Auth::getUserId();
$db = Database::getInstance();

// Get customer ID
$customer_sql = "SELECT id FROM customers WHERE user_id = ?";
$customer = $db->fetch($customer_sql, [$customer_user_id]);

if (!$customer) {
    flash('error', 'Customer profile not found', 'danger');
    redirect('../index.php');
}

$customer_id = $customer['id'];

// Get all disputes for orders belonging to this customer - FIXED QUERY
$disputes_sql = "SELECT d.*, 
                 o.order_number,
                 i.title as item_title,
                 v.business_name as vendor_name,
                 (SELECT COUNT(*) FROM dispute_messages dm 
                  WHERE dm.dispute_id = d.id 
                  AND dm.sender_type != 'customer' 
                  AND dm.is_read_by_customer = 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
                 WHERE o.customer_id = ?
                 ORDER BY d.created_at DESC";

$disputes = $db->fetchAll($disputes_sql, [$customer_id]);

$page_title = 'My Reports - ' . APP_NAME;
include '../templates/layouts/customer/head.php';
?>

<body class="fixed sidebar-mini">
    <div class="wrapper">
        <!-- Sidebar -->
        <nav class="sidebar">
            <div class="sidebar-header">
                <?php include '../templates/layouts/customer/navbar.php'; ?>
            </div>
        </nav>

        <!-- Page Content -->
        <div class="content-wrapper">
            <div class="main-content">
                <?php include '../templates/layouts/customer/header.php'; ?>
                <!-- Body Content -->
                <div class="body-content">
                    <div class="decoration blur-2"></div>
                    <div class="decoration blur-3"></div>
                    <div class="container-xxl">
                
                    <div class="page-header mb-4">
                        <h1><i class="fas fa-exclamation-triangle"></i> My Reports</h1>
                        <p class="text-muted">View and manage your reported issues</p>
                    </div>

                    <?php if (empty($disputes)): ?>
                        <div class="card">
                            <div class="card-body text-center py-5">
                                <i class="fas fa-clipboard-list fa-3x text-muted mb-3"></i>
                                <h5>No Reports Found</h5>
                                <p class="text-muted">You haven't reported any issues yet.</p>
                                <a href="orders.php" class="btn btn-primary">View My Orders</a>
                            </div>
                        </div>
                    <?php else: ?>
                        <div class="card-body p-0">
                            <div class="table-responsive">
                                <table class="table table-hover mb-0">
                                    <thead>
                                        <tr>
                                            <th>Dispute #</th>
                                            <th>Order</th>
                                            <th>Vendor</th>
                                            <th>Issue Type</th>
                                            <th>Reported By</th>
                                            <th>Status</th>
                                            <th>Last Activity</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($disputes as $dispute): ?>
                                            <?php
                                            $status_colors = [
                                                'open' => 'warning',
                                                'under_review' => 'info',
                                                'resolved' => 'success',
                                                'closed' => 'secondary'
                                            ];
                                            $color = $status_colors[$dispute['status']] ?? 'secondary';
                                            
                                            $type_labels = [
                                                'damage' => 'Damaged',
                                                'non_return' => 'Non-Return',
                                                'quality' => 'Quality Issue',
                                                'overcharge' => 'Overcharge',
                                                'other' => 'Other'
                                            ];
                                            $type_label = $type_labels[$dispute['dispute_type']] ?? ucwords($dispute['dispute_type']);
                                            ?>
                                            <tr class="<?= $dispute['unread_count'] > 0 ? 'table-primary' : '' ?>">
                                                <td>
                                                    <strong><?= esc($dispute['dispute_number']) ?></strong>
                                                    <?php if ($dispute['unread_count'] > 0): ?>
                                                        <br><span class="badge bg-danger"><?= $dispute['unread_count'] ?> new</span>
                                                    <?php endif; ?>
                                                </td>
                                                <td>
                                                    <strong><?= esc($dispute['order_number']) ?></strong><br>
                                                    <small class="text-muted"><?= esc($dispute['item_title']) ?></small>
                                                </td>
                                                <td><?= esc($dispute['vendor_name']) ?></td>
                                                <td><?= $type_label ?></td>
                                                <td>
                                                    <?php if ($dispute['raised_by_type'] === 'customer'): ?>
                                                        <span class="badge bg-primary">You</span>
                                                    <?php else: ?>
                                                        <span class="badge bg-info">Vendor</span>
                                                    <?php endif; ?>
                                                </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 messages</small>
                                                    <?php endif; ?>
                                                </td>
                                                <td>
                                                    <a href="dispute-details.php?id=<?= $dispute['id'] ?>" class="btn btn-sm btn-primary">
                                                        <i class="fas fa-comments"></i> View
                                                    </a>
                                                </td>
                                            </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    <?php endif; ?>
</div>

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