<?php
require_once 'config.php';

try {
    $pdo = db();
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // Create drills table
    $pdo->exec("
        CREATE TABLE IF NOT EXISTS drills (
            id INT AUTO_INCREMENT PRIMARY KEY,
            title VARCHAR(255) NOT NULL,
            description TEXT,
            min_players INT,
            max_players INT,
            age_group VARCHAR(50),
            skill_focus VARCHAR(100),
            difficulty VARCHAR(50),
            duration_minutes INT,
            equipment_required TEXT,
            youtube_url VARCHAR(255),
            is_public BOOLEAN DEFAULT TRUE,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            coach_id INT NULL
        )
    ");

    // Check if table is empty before seeding
    $stmt = $pdo->query("SELECT COUNT(*) FROM drills");
    if ($stmt->fetchColumn() == 0) {
        // Seed data
        $drills = [
            [
                'title' => '4-Cone Dribbling Drill',
                'description' => 'A simple drill to improve close control and dribbling skills.',
                'min_players' => 1,
                'max_players' => 8,
                'age_group' => 'U8-U12',
                'skill_focus' => 'Dribbling, Ball Control',
                'difficulty' => 'Beginner',
                'duration_minutes' => 15,
                'equipment_required' => '4 cones, 1 ball per player',
                'youtube_url' => 'https://www.youtube.com/embed/example1'
            ],
            [
                'title' => 'Passing Triangle',
                'description' => 'A drill to practice short, quick passes and movement off the ball.',
                'min_players' => 3,
                'max_players' => 6,
                'age_group' => 'U10-U16',
                'skill_focus' => 'Passing, Movement',
                'difficulty' => 'Intermediate',
                'duration_minutes' => 20,
                'equipment_required' => '3 cones, 1 ball',
                'youtube_url' => 'https://www.youtube.com/embed/example2'
            ],
            [
                'title' => 'Defensive Positioning Game',
                'description' => 'A small-sided game focused on teaching defensive shape and communication.',
                'min_players' => 8,
                'max_players' => 12,
                'age_group' => 'U12-U18',
                'skill_focus' => 'Defending, Team Shape',
                'difficulty' => 'Advanced',
                'duration_minutes' => 30,
                'equipment_required' => 'Bibs, 2 small goals',
                'youtube_url' => null
            ],
        ];

        $stmt = $pdo->prepare(
            "INSERT INTO drills (title, description, min_players, max_players, age_group, skill_focus, difficulty, duration_minutes, equipment_required, youtube_url, coach_id) 
             VALUES (:title, :description, :min_players, :max_players, :age_group, :skill_focus, :difficulty, :duration_minutes, :equipment_required, :youtube_url, :coach_id)"
        );

        foreach ($drills as $drill) {
            $drill['coach_id'] = 1; // Hardcode coach_id for seed data
            $stmt->execute($drill);
        }
        echo "Database seeded successfully!\n";
    } else {
        echo "Database already seeded.\n";
    }

} catch (PDOException $e) {
    die("Database error: " . $e->getMessage());
}
