<?php
// seed_stories.php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once __DIR__ . '/../src/Utils/Env.php';
require_once __DIR__ . '/../src/Config/Database.php';

use App\Utils\Env;
use App\Config\Database;

try {
    Env::load(__DIR__ . '/../../.env');
    $db = Database::getInstance()->getConnection();

    echo "Seeding stories...\n";

    // 1. Get some users (preferably seed users)
    $stmt = $db->query("SELECT id, username FROM users LIMIT 5");
    $users = $stmt->fetchAll(PDO::FETCH_ASSOC);

    if (empty($users)) {
        die("No users found to create stories for.\n");
    }

    $sampleStories = [
        ['type' => 'text', 'content' => 'Welcome to AnonChat! This is a test story.', 'media_url' => null],
        ['type' => 'image', 'content' => 'Check out this cool view!', 'media_url' => 'https://picsum.photos/400/600'],
        ['type' => 'text', 'content' => 'Anyone up for a chat? Hit me up anonymously!', 'media_url' => null],
        ['type' => 'video', 'content' => 'My new video', 'media_url' => 'https://www.w3schools.com/html/mov_bbb.mp4'],
    ];

    foreach ($users as $user) {
        // Add 1-2 stories for each user
        $count = rand(1, 2);
        for ($i = 0; $i < $count; $i++) {
            $story = $sampleStories[array_rand($sampleStories)];
            
            $expiresAt = date('Y-m-d H:i:s', time() + (24 * 60 * 60)); // Expires in 24h
            
            $stmt = $db->prepare("INSERT INTO stories (user_id, type, content, media_url, expires_at, created_at, updated_at) VALUES (?, ?, ?, ?, ?, NOW(), NOW())");
            $stmt->execute([
                $user['id'],
                $story['type'],
                $story['content'],
                $story['media_url'],
                $expiresAt
            ]);
            
            echo "Created story for {$user['username']}\n";
        }
    }

    echo "Stories seeded successfully.\n";

} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
