<?php
// seed-kampagnen.php - Beispiel-Kampagnen einfügen

require_once 'doctrine-config.php';

use Entity\Kampagne;
use Entity\User;

try {
    // Prüfen ob bereits Kampagnen vorhanden sind
    $existingData = $entityManager->getRepository(Kampagne::class)->findAll();

    if (count($existingData) > 0) {
        echo "Kampagnen sind bereits vorhanden. Überspringe Seed.\n";
        exit(0);
    }

    // User für Zuweisung holen
    $users = $entityManager->getRepository(User::class)->findAll();
    if (count($users) === 0) {
        echo "Keine User gefunden. Bitte erst User anlegen.\n";
        exit(1);
    }

    // 10 Beispiel-Kampagnen erstellen
    $kampagnenData = [
        ['is_fb' => true, 'is_tr' => false, 'is_mail' => false, 'dest_url' => 'https://example.com/promo1', 'linktext' => 'Super Angebot 1', 'ma' => 5, 'reload' => 24],
        ['is_fb' => false, 'is_tr' => true, 'is_mail' => false, 'dest_url' => 'https://example.com/promo2', 'linktext' => 'Mega Deal 2', 'ma' => 3, 'reload' => 12],
        ['is_fb' => true, 'is_tr' => true, 'is_mail' => false, 'dest_url' => 'https://example.com/promo3', 'linktext' => 'Exklusiv Angebot 3', 'ma' => 10, 'reload' => 48],
        ['is_fb' => false, 'is_tr' => false, 'is_mail' => true, 'dest_url' => 'https://example.com/promo4', 'linktext' => 'Newsletter Special 4', 'ma' => 2, 'reload' => 6, 'mailtext' => 'Hallo, schau dir unser tolles Angebot an!'],
        ['is_fb' => true, 'is_tr' => false, 'is_mail' => true, 'dest_url' => 'https://example.com/promo5', 'linktext' => 'Combo Deal 5', 'ma' => 7, 'reload' => 36, 'mailtext' => 'Exklusive Kombination nur für dich!'],
        ['is_fb' => false, 'is_tr' => true, 'is_mail' => false, 'dest_url' => 'https://example.com/promo6', 'linktext' => 'Traffic Boost 6', 'ma' => 4, 'reload' => 18],
        ['is_fb' => true, 'is_tr' => true, 'is_mail' => true, 'dest_url' => 'https://example.com/promo7', 'linktext' => 'All-in-One 7', 'ma' => 15, 'reload' => 72, 'mailtext' => 'Maximale Reichweite mit allen Kanälen!'],
        ['is_fb' => false, 'is_tr' => false, 'is_mail' => false, 'dest_url' => 'https://example.com/promo8', 'linktext' => 'Basic Campaign 8', 'ma' => 1, 'reload' => 3],
        ['is_fb' => true, 'is_tr' => false, 'is_mail' => false, 'dest_url' => 'https://example.com/promo9', 'linktext' => 'Facebook Power 9', 'ma' => 8, 'reload' => 24],
        ['is_fb' => false, 'is_tr' => true, 'is_mail' => true, 'dest_url' => 'https://example.com/promo10', 'linktext' => 'Traffic + Mail 10', 'ma' => 6, 'reload' => 30, 'mailtext' => 'Doppelte Power für deine Kampagne!'],
    ];

    $userIndex = 0;
    foreach ($kampagnenData as $data) {
        $kampagne = new Kampagne();
        $kampagne->setIsFb($data['is_fb']);
        $kampagne->setIsTr($data['is_tr']);
        $kampagne->setIsMail($data['is_mail']);
        $kampagne->setDestUrl($data['dest_url']);
        $kampagne->setLinktext($data['linktext']);
        $kampagne->setMa($data['ma']);
        $kampagne->setReload($data['reload']);

        if (isset($data['mailtext'])) {
            $kampagne->setMailtext($data['mailtext']);
        }

        // User rotierend zuweisen
        $kampagne->setUser($users[$userIndex % count($users)]);
        $userIndex++;

        $entityManager->persist($kampagne);
    }

    $entityManager->flush();

    echo "10 Beispiel-Kampagnen wurden erfolgreich eingefügt.\n";
} catch (\Exception $e) {
    echo "Fehler beim Einfügen der Daten: " . $e->getMessage() . "\n";
}
