<?php 
session_start(); 

// Handle clearing the session
if (isset($_GET['clear'])) {
    unset($_SESSION['csv_analysis']);
    unset($_SESSION['show_dashboard']);
    header('Location: index.php');
    exit;
}

// Handle dashboard generation
if (isset($_POST['generate_dashboard'])) {
    if (isset($_SESSION['csv_analysis'])) {
        // Update schema with user corrections
        if (isset($_POST['column_types'])) {
            foreach ($_POST['column_types'] as $col => $type) {
                if (isset($_SESSION['csv_analysis']['schema'][$col])) {
                    $_SESSION['csv_analysis']['schema'][$col]['type'] = $type;
                }
            }
            // Re-run suggestions if needed (or just use updated schema in dashboard.php)
        }
        $_SESSION['show_dashboard'] = true;
    }
    header('Location: index.php');
    exit;
}

$show_dashboard = isset($_SESSION['show_dashboard']) && $_SESSION['show_dashboard'];
$csv_analysis = isset($_SESSION['csv_analysis']) ? $_SESSION['csv_analysis'] : null;

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CVS Uploader and Dashboard</title>
    <meta name="description" content="InstantDashboard: Upload CSVs to view insightful data summaries with automatic column type detection.">
    <meta name="keywords" content="csv dashboard, data visualization, file upload, data analysis, csv reader, data insights, business intelligence, no-code tools, data reporting, analytics dashboard, tsv support, data summary">
    <meta property="og:title" content="dashboard-for-philip-v001">
    <meta property="og:description" content="InstantDashboard: Upload CSVs to view insightful data summaries with automatic column type detection.">
    <meta property="og:image" content="https://project-screens.s3.amazonaws.com/screenshots/34601/app-hero-20251002-190105.png">
    <meta name="twitter:card" content="summary_large_image">
    <meta name="twitter:image" content="https://project-screens.s3.amazonaws.com/screenshots/34601/app-hero-20251002-190105.png">
    
    <link rel="icon" href="assets/pasted-20251003-114511-3bf2c1aa.png">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;700&family=Montserrat:wght@700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />
</head>
<body>

    <div class="container py-5">
        <header class="text-center mb-5">
            <img src="assets/pasted-20251003-114511-3bf2c1aa.png" alt="Logo" class="mb-4" style="max-width: 150px;">
            <h1 class="display-4">Instant CSV Dashboard</h1>
            <p class="lead">Upload a CSV or TSV file to automatically generate an interactive dashboard.</p>
            <hr class="divider my-4">
        </header>

        <?php if ($show_dashboard && $csv_analysis): ?>
            <div id="dashboard-container">
                <?php include 'dashboard.php'; ?>
            </div>
             <div class="text-center mt-4">
                <a href="index.php?clear=1" class="btn btn-info">Start Over with a New File</a>
            </div>

        <?php elseif ($csv_analysis): ?>
            <div class="row justify-content-center" id="schema-preview">
                <div class="col-lg-10">
                    <div class="card p-4">
                        <h2 class="text-center mb-4">Schema Preview</h2>
                        <?php if (isset($csv_analysis['error'])): ?>
                            <div class="alert alert-danger"><?php echo htmlspecialchars($csv_analysis['error']); ?></div>
                             <a href="index.php?clear=1" class="btn btn-secondary">Upload New File</a>
                        <?php else: ?>
                            <form action="index.php" method="post">
                                <table class="table table-striped table-hover">
                                    <thead>
                                        <tr>
                                            <th>Column Name</th>
                                            <th>Guessed Type</th>
                                            <th>Unique Values</th>
                                            <th>Blank Values</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($csv_analysis['schema'] as $col => $info): ?>
                                        <tr>
                                            <td><?php echo htmlspecialchars($col); ?></td>
                                            <td>
                                                <select class="form-select form-select-sm" name="column_types[<?php echo htmlspecialchars($col); ?>]">
                                                    <option value="text" <?php if ($info['type'] == 'text') echo 'selected'; ?>>Text</option>
                                                    <option value="integer" <?php if ($info['type'] == 'integer') echo 'selected'; ?>>Integer</option>
                                                    <option value="decimal" <?php if ($info['type'] == 'decimal') echo 'selected'; ?>>Decimal</option>
                                                    <option value="date" <?php if ($info['type'] == 'date') echo 'selected'; ?>>Date</option>
                                                </select>
                                            </td>
                                            <td><?php echo $info['unique_count']; ?></td>
                                            <td><?php echo $info['blank_count']; ?></td>
                                        </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                                <div class="d-grid gap-2 d-md-flex justify-content-md-end mt-4">
                                    <a href="index.php?clear=1" class="btn btn-secondary">Upload New File</a>
                                    <button type="submit" name="generate_dashboard" class="btn btn-primary">Generate Dashboard</button>
                                </div>
                            </form>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        <?php else: ?>
            <div class="row justify-content-center">
                <div class="col-lg-8">
                    <div class="card p-5">
                        <div class="card-body">
                            <h2 class="card-title text-center mb-4">Upload Your Data</h2>
                            <form action="upload.php" method="post" enctype="multipart/form-data">
                                <div class="mb-3">
                                    <input class="form-control form-control-lg" type="file" id="csvFile" name="csvFile" accept=".csv, .tsv" required>
                                </div>
                                <div class="d-grid">
                                    <button type="submit" class="btn btn-primary btn-lg">Upload for Preview</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        <?php endif; ?>

    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <script src="assets/js/main.js"></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/html2canvas@1.0.0-rc.5/dist/html2canvas.min.js"></script>
</body>
</html>