<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use DateTime;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Symfony\Component\Yaml\Yaml;

final class Version20250522212300 extends AbstractMigration
{
    private const BLOG_POST_SLUGS = [
        'doctor_who_gayness_church',
        'does_resurrection_ground_works',
        'easter',
        'euhwc_toast_to_the_lasses_2024',
        'god_is_not_great',
        'llms_do_not_understand_anything',
        'my_feed_and_reading_list',
        'no_more_youtube',
        'open_questions_about_sex',
        'paradox',
        'sapiens_on_religion',
        'science_and_philosophy',
        'surprised_by_hope',
        'tracking_pixels',
        'who_consecrates_the_temple',
        'word_hallucination_with_reference_to_llms',
    ];

    public function getDescription(): string {
        return 'Migrating my blog posts from my old website to notes in my new website';
    }

    public function up(Schema $schema): void {
        foreach (self::BLOG_POST_SLUGS as $slug) {
            $blogPostPath = './migrations/blog-migrated/' . $slug . '.yaml';
            $blogPost = Yaml::parseFile($blogPostPath);

            $publishedDateTime = DateTime::createFromFormat('U', strval($blogPost['pubDate']));
            $publishedDate = $publishedDateTime->format('Y-m-d');
            $updatedDateTime = array_key_exists(array: $blogPost, key: 'updatedDate')
                ? DateTime::createFromFormat('U', strval($blogPost['updatedDate']))
                : null;
            $updatedDate = $updatedDateTime == null
                ? 'NULL'
                : "'" . $updatedDateTime->format('Y-m-d') . "'";
            $title = str_replace("'", "''", $blogPost['title']);
            $description = str_replace("'", "''", $blogPost['description']);
            $content = str_replace("'", "''", $blogPost['content']);

            $this->addSql(<<<SQL
                INSERT INTO blog_post(slug, published_date, updated_date, title, description, content)
                VALUES('$slug', '$publishedDate', $updatedDate, '$title', '$description', '$content')
            SQL);
        }
    }

    public function down(Schema $schema): void {
        foreach (self::BLOG_POST_SLUGS as $slug) {
            $this->addSql(<<<SQL
                DELETE FROM blog_post WHERE slug = '$slug'
            SQL);
        }
    }
}
