<?php

declare(strict_types=1);

namespace Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
 * Auto-generated Migration: Please modify to your needs!
 */
final class Version20251006214042 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Initial schema with default data';
    }

    public function up(Schema $schema): void
    {
        // Werbenetzwerk table
        $this->addSql('CREATE TABLE werbenetzwerk (
            id INT AUTO_INCREMENT NOT NULL,
            umrechnungskurs DOUBLE PRECISION NOT NULL,
            eigenanteil_prozent SMALLINT UNSIGNED NOT NULL,
            interfacepasswort VARCHAR(255) NOT NULL,
            interface_anfragen_pro_tag SMALLINT UNSIGNED NOT NULL,
            user_id VARCHAR(50) NOT NULL,
            date_updated DATETIME DEFAULT NULL,
            PRIMARY KEY (id)
        ) DEFAULT CHARACTER SET utf8mb4');

        // Users table
        $this->addSql('CREATE TABLE users (
            id INT AUTO_INCREMENT NOT NULL,
            username VARCHAR(100) NOT NULL,
            email VARCHAR(255) NULL,
            password VARCHAR(255) NULL,
            rolle ENUM(\'user\', \'admin\', \'werbenetzwerk\'),
            createdAt DATETIME NOT NULL,
            werbenetzwerk_id INT DEFAULT NULL,
            INDEX IDX_1483A5E948B23AFC (werbenetzwerk_id),
            PRIMARY KEY (id)
        ) DEFAULT CHARACTER SET utf8mb4');

        // Kampagnen table
        $this->addSql('CREATE TABLE kampagnen (
            id INT AUTO_INCREMENT NOT NULL,
            is_fb TINYINT(1) NOT NULL,
            is_tr TINYINT(1) NOT NULL,
            is_mail TINYINT(1) NOT NULL,
            dest_url VARCHAR(255) NOT NULL,
            kid VARCHAR(100) DEFAULT NULL,
            mailtext LONGTEXT DEFAULT NULL,
            img VARCHAR(255) DEFAULT NULL,
            linktext VARCHAR(255) DEFAULT NULL,
            ma SMALLINT NOT NULL,
            reload SMALLINT NOT NULL,
            verguetung DOUBLE PRECISION DEFAULT NULL,
            uebrig INT DEFAULT NULL,
            dateCreated DATETIME NOT NULL,
            dateModified DATETIME NOT NULL,
            user_id INT DEFAULT NULL,
            INDEX IDX_37572FC6A76ED395 (user_id),
            PRIMARY KEY (id)
        ) DEFAULT CHARACTER SET utf8mb4');

        // Reloadsperre table
        $this->addSql('CREATE TABLE reloadsperre (
            id INT AUTO_INCREMENT NOT NULL,
            reload_bis DATETIME NOT NULL,
            ip INT UNSIGNED NOT NULL,
            dateCreated DATETIME NOT NULL,
            kampagne_id INT NOT NULL,
            user_id INT NOT NULL,
            INDEX IDX_B6A62B2AACE19231 (kampagne_id),
            INDEX IDX_B6A62B2AA76ED395 (user_id),
            INDEX idx_kampagne_user (kampagne_id, user_id),
            INDEX idx_reload_bis (reload_bis),
            PRIMARY KEY (id)
        ) DEFAULT CHARACTER SET utf8mb4');

        // Foreign keys
        $this->addSql('ALTER TABLE kampagnen ADD CONSTRAINT FK_37572FC6A76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE SET NULL');
        $this->addSql('ALTER TABLE reloadsperre ADD CONSTRAINT FK_B6A62B2AACE19231 FOREIGN KEY (kampagne_id) REFERENCES kampagnen (id) ON DELETE RESTRICT');
        $this->addSql('ALTER TABLE reloadsperre ADD CONSTRAINT FK_B6A62B2AA76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE');
        $this->addSql('ALTER TABLE users ADD CONSTRAINT FK_1483A5E948B23AFC FOREIGN KEY (werbenetzwerk_id) REFERENCES werbenetzwerk (id) ON DELETE SET NULL');

        // Default data - Werbenetzwerk
        $this->addSql('INSERT INTO werbenetzwerk (id, umrechnungskurs, eigenanteil_prozent, interfacepasswort, interface_anfragen_pro_tag, user_id, date_updated) VALUES (1, 0.0000000022, 25, "pAKc34EajovTvsnD6Y6WfhS", 500, "1244", NOW())');

        // Default data - Users
        $this->addSql('INSERT INTO users (username, email, password, rolle, createdAt) VALUES ("admin", "admin@example.com", "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi", "admin", NOW())');
        $this->addSql('INSERT INTO users (username, email, password, rolle, createdAt) VALUES ("testuser1", "testuser1@example.com", "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi", "user", NOW())');
        $this->addSql('INSERT INTO users (username, email, password, rolle, createdAt, werbenetzwerk_id) VALUES ("adcity-eu", "", "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi", "werbenetzwerk", NOW(),1)');
    }

    public function down(Schema $schema): void
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->addSql('ALTER TABLE kampagnen DROP FOREIGN KEY FK_37572FC6A76ED395');
        $this->addSql('ALTER TABLE reloadsperre DROP FOREIGN KEY FK_B6A62B2AACE19231');
        $this->addSql('ALTER TABLE reloadsperre DROP FOREIGN KEY FK_B6A62B2AA76ED395');
        $this->addSql('ALTER TABLE users DROP FOREIGN KEY FK_1483A5E948B23AFC');
        $this->addSql('DROP TABLE kampagnen');
        $this->addSql('DROP TABLE reloadsperre');
        $this->addSql('DROP TABLE users');
        $this->addSql('DROP TABLE werbenetzwerk');
    }
}
