<?php
session_start();

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csvFile'])) {
    $file = $_FILES['csvFile'];

    if ($file['error'] === UPLOAD_ERR_OK) {
        $filePath = $file['tmp_name'];
        
        // Analyze the CSV file
        $analysis = analyzeCsv($filePath);
        
        // Store analysis in session
        $_SESSION['csv_analysis'] = $analysis;
        
        // Redirect back to the main page
        header('Location: index.php');
        exit;
    }
}

// Simple CSV analysis function
function analyzeCsv($filePath, $rowsToAnalyze = 500) {
    $fileHandle = fopen($filePath, 'r');
    if (!$fileHandle) {
        return ['error' => 'Could not open file'];
    }

    // 1. Detect Delimiter
    $firstLine = fgets($fileHandle);
    $commaCount = substr_count($firstLine, ',');
    $tabCount = substr_count($firstLine, "\t");
    $delimiter = $tabCount > $commaCount ? "\t" : ',';
    
    // Reset pointer and read header
    rewind($fileHandle);
    $header = fgetcsv($fileHandle, 0, $delimiter);
    
    $data = [];
    $rowCount = 0;
    while (($row = fgetcsv($fileHandle, 0, $delimiter)) !== false && $rowCount < $rowsToAnalyze) {
        if (count($row) == count($header)) {
            $data[] = array_combine($header, $row);
        }
        $rowCount++;
    }
    fclose($fileHandle);

    if (empty($data)) {
        return ['error' => 'No data found in file or header mismatch.'];
    }

    // 2. Guess Column Types and Stats
    $schema = [];
    foreach ($header as $col) {
        $values = array_column($data, $col);
        $uniqueVals = array_unique($values);
        $blankCount = count(array_filter($values, fn($v) => $v === '' || $v === null));

        // Type detection
        $type = 'text';
        $isNumeric = true;
        $isInteger = true;
        $isDate = true;
        
        foreach ($values as $val) {
            if ($val === '' || $val === null) continue;

            if (!is_numeric($val)) $isNumeric = false;
            if (filter_var($val, FILTER_VALIDATE_INT) === false) $isInteger = false;
            if (strtotime($val) === false) $isDate = false;
        }

        if ($isDate) {
            $type = 'date';
        } elseif ($isNumeric) {
            $type = $isInteger ? 'integer' : 'decimal';
        }

        $schema[$col] = [
            'type' => $type,
            'unique_count' => count($uniqueVals),
            'blank_count' => $blankCount
        ];
    }

    // 3. Suggest dashboard items
    $suggestions = suggestDashboard($schema);

    return [
        'delimiter' => $delimiter,
        'header' => $header,
        'schema' => $schema,
        'suggestions' => $suggestions,
        'sample' => array_slice($data, 0, 20)
    ];
}

function suggestDashboard($schema) {
    $suggestions = [];
    $dateCols = [];
    $numericCols = [];
    $categoryCols = [];

    foreach ($schema as $col => $info) {
        if ($info['type'] === 'date') $dateCols[] = $col;
        if ($info['type'] === 'integer' || $info['type'] === 'decimal') $numericCols[] = $col;
        if ($info['type'] === 'text' && $info['unique_count'] < 50) $categoryCols[] = $col;
    }

    if (!empty($dateCols) && !empty($numericCols)) {
        $suggestions['time_chart'] = ['time_col' => $dateCols[0], 'metric_col' => $numericCols[0]];
    } elseif (!empty($numericCols)) {
        $suggestions['histogram'] = ['metric_col' => $numericCols[0]];
    }

    if (!empty($categoryCols) && !empty($numericCols)) {
        $suggestions['category_chart'] = ['category_col' => $categoryCols[0], 'metric_col' => $numericCols[0]];
    } elseif (empty($numericCols) && !empty($categoryCols)) {
        $suggestions['category_table'] = ['category_col' => $categoryCols[0]];
    }
    
    $suggestions['kpis'] = !empty($numericCols) ? $numericCols : [];
    $suggestions['filters'] = array_slice($categoryCols, 0, 2);

    return $suggestions;
}