<?php
require_once 'config.php';
require_once 'includes/Database.php';
require_once 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;

$db = new Database();

// Kullanıcı listesi al
$users = $db->fetchAll("SELECT username FROM users WHERE is_active = 1 ORDER BY username LIMIT 3");

if (empty($users)) {
    die('Veritabanında aktif kullanıcı bulunamadı. Önce kullanıcı ekleyin.');
}

// Gelen Aramalar için örnek Excel
function createIncomingCallsSample($users) {
    $spreadsheet = new Spreadsheet();
    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setTitle('Gelen Aramalar');

    // Başlık satırı
    $headers = [
        'Kullanıcı Adı', 'Tarih', 'Dahili', 'Toplam Arama', 'Cevaplanan',
        'Cevaplanmayan', 'Toplam Süre', 'Ortalama Süre', 'Toplam Bekleme',
        'Ortalama Bekleme', 'Max Bekleme'
    ];

    $column = 'A';
    foreach ($headers as $header) {
        $sheet->setCellValue($column . '1', $header);
        $sheet->getStyle($column . '1')->applyFromArray([
            'font' => ['bold' => true, 'color' => ['rgb' => 'FFFFFF']],
            'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['rgb' => '667eea']],
            'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER]
        ]);
        $sheet->getColumnDimension($column)->setAutoSize(true);
        $column++;
    }

    // Örnek veriler
    $row = 2;
    $dates = [date('Y-m-d'), date('Y-m-d', strtotime('-1 day')), date('Y-m-d', strtotime('-2 days'))];

    foreach ($users as $user) {
        foreach ($dates as $date) {
            $totalCalls = rand(30, 80);
            $answered = rand(25, $totalCalls);
            $unanswered = $totalCalls - $answered;

            $sheet->setCellValue('A' . $row, $user['username']);
            $sheet->setCellValue('B' . $row, $date);
            $sheet->setCellValue('C' . $row, '10' . rand(1, 9));
            $sheet->setCellValue('D' . $row, $totalCalls);
            $sheet->setCellValue('E' . $row, $answered);
            $sheet->setCellValue('F' . $row, $unanswered);
            $sheet->setCellValue('G' . $row, sprintf('%02d:%02d:%02d', rand(1, 3), rand(0, 59), rand(0, 59)));
            $sheet->setCellValue('H' . $row, sprintf('00:%02d:%02d', rand(2, 5), rand(0, 59)));
            $sheet->setCellValue('I' . $row, sprintf('00:%02d:%02d', rand(10, 30), rand(0, 59)));
            $sheet->setCellValue('J' . $row, sprintf('00:00:%02d', rand(15, 45)));
            $sheet->setCellValue('K' . $row, sprintf('00:%02d:%02d', rand(1, 3), rand(0, 59)));
            $row++;
        }
    }

    // Kenarlıklar
    $lastRow = $row - 1;
    $sheet->getStyle('A1:K' . $lastRow)->applyFromArray([
        'borders' => [
            'allBorders' => ['borderStyle' => Border::BORDER_THIN, 'color' => ['rgb' => 'CCCCCC']]
        ]
    ]);

    $writer = new Xlsx($spreadsheet);
    $filename = 'ornek_gelen_aramalar_' . date('Y-m-d') . '.xlsx';
    $writer->save(__DIR__ . '/samples/' . $filename);

    return $filename;
}

// Giden Aramalar için örnek Excel
function createOutgoingCallsSample($users) {
    $spreadsheet = new Spreadsheet();
    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setTitle('Giden Aramalar');

    // Başlık satırı
    $headers = ['Kullanıcı Adı', 'Tarih', 'Arama Sayısı', 'Toplam Süre', 'Ortalama Süre'];

    $column = 'A';
    foreach ($headers as $header) {
        $sheet->setCellValue($column . '1', $header);
        $sheet->getStyle($column . '1')->applyFromArray([
            'font' => ['bold' => true, 'color' => ['rgb' => 'FFFFFF']],
            'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['rgb' => '667eea']],
            'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER]
        ]);
        $sheet->getColumnDimension($column)->setAutoSize(true);
        $column++;
    }

    // Örnek veriler
    $row = 2;
    $dates = [date('Y-m-d'), date('Y-m-d', strtotime('-1 day')), date('Y-m-d', strtotime('-2 days'))];

    foreach ($users as $user) {
        foreach ($dates as $date) {
            $callCount = rand(20, 50);

            $sheet->setCellValue('A' . $row, $user['username']);
            $sheet->setCellValue('B' . $row, $date);
            $sheet->setCellValue('C' . $row, $callCount);
            $sheet->setCellValue('D' . $row, sprintf('%02d:%02d:%02d', rand(1, 2), rand(0, 59), rand(0, 59)));
            $sheet->setCellValue('E' . $row, sprintf('00:%02d:%02d', rand(2, 4), rand(0, 59)));
            $row++;
        }
    }

    // Kenarlıklar
    $lastRow = $row - 1;
    $sheet->getStyle('A1:E' . $lastRow)->applyFromArray([
        'borders' => [
            'allBorders' => ['borderStyle' => Border::BORDER_THIN, 'color' => ['rgb' => 'CCCCCC']]
        ]
    ]);

    $writer = new Xlsx($spreadsheet);
    $filename = 'ornek_giden_aramalar_' . date('Y-m-d') . '.xlsx';
    $writer->save(__DIR__ . '/samples/' . $filename);

    return $filename;
}

// Samples klasörünü oluştur
if (!file_exists(__DIR__ . '/samples')) {
    mkdir(__DIR__ . '/samples', 0777, true);
}

echo "<h1>Örnek Excel Dosyaları Oluştur</h1>";

try {
    $incomingFile = createIncomingCallsSample($users);
    echo "<p style='color: green;'>✓ Gelen aramalar örnek dosyası oluşturuldu: <a href='samples/$incomingFile'>$incomingFile</a></p>";

    $outgoingFile = createOutgoingCallsSample($users);
    echo "<p style='color: green;'>✓ Giden aramalar örnek dosyası oluşturuldu: <a href='samples/$outgoingFile'>$outgoingFile</a></p>";

    echo "<br><h3>Dosyaları İndir:</h3>";
    echo "<p><a href='samples/$incomingFile' download style='display:inline-block; padding:10px 20px; background:#667eea; color:white; text-decoration:none; border-radius:5px; margin:5px;'>Gelen Aramalar İndir</a></p>";
    echo "<p><a href='samples/$outgoingFile' download style='display:inline-block; padding:10px 20px; background:#667eea; color:white; text-decoration:none; border-radius:5px; margin:5px;'>Giden Aramalar İndir</a></p>";

} catch (Exception $e) {
    echo "<p style='color: red;'>Hata: " . $e->getMessage() . "</p>";
}
?>
