<?php
declare(strict_types=1);

use Migrations\BaseMigration;

class SeedLandingContent extends BaseMigration
{
    public function up(): void
    {
        $now = date('Y-m-d H:i:s');
        $q = static fn (string $s): string => str_replace("'", "''", $s);

        $row = $this->fetchRow('SELECT COUNT(*) AS count FROM programs');
        if ($row === false || (int)$row['count'] === 0) {
            $programs = [
                [
                    'slug' => 'pottery',
                    'label' => 'Pottery',
                    'headline' => 'Hands on clay. No experience assumed.',
                    'body' => "Start with the wheel or hand-building — either is a fine place to begin. You'll have a patient teacher beside you, time to make mistakes, and a finished piece to take home from your very first visit.",
                    'bullets' => "Wheel throwing basics\nHand-building & sculpting\nGlazing & finishing",
                    'tone' => 'clay',
                    'image_path' => 'studio/wheel.jpg',
                    'vessel_type' => 'bowl',
                    'sort_order' => 10,
                ],
                [
                    'slug' => 'knitting',
                    'label' => 'Knitting',
                    'headline' => 'Soft yarn, quiet rhythm.',
                    'body' => "Whether you've never cast on or you're picking up needles again after years away, the group is small, the pace is yours, and every session ends with a little more done than when you arrived.",
                    'bullets' => "Needle & yarn fundamentals\nProject-based learning\nRepair & re-use",
                    'tone' => 'moss',
                    'image_path' => 'studio/knitter.jpg',
                    'vessel_type' => 'jar',
                    'sort_order' => 20,
                ],
                [
                    'slug' => 'unsure',
                    'label' => 'Not sure',
                    'headline' => "That's a fine place to start.",
                    'body' => "Tell us a little about what drew you here and we'll suggest a first step — maybe a trial session of each, a quiet observe-and-chat visit, or a recommendation for when the timing feels better.",
                    'bullets' => "Trial of both crafts\nObserve-and-chat visits\nHonest timing advice",
                    'tone' => 'amber',
                    'image_path' => 'studio/mixed-still-life.jpg',
                    'vessel_type' => 'jug',
                    'sort_order' => 30,
                ],
            ];
            foreach ($programs as $p) {
                $this->execute(sprintf(
                    "INSERT INTO programs (slug, label, headline, body, bullets, tone, image_path, vessel_type, sort_order, created, modified) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s',%d,'%s','%s')",
                    $q($p['slug']),
                    $q($p['label']),
                    $q($p['headline']),
                    $q($p['body']),
                    $q($p['bullets']),
                    $q($p['tone']),
                    $q($p['image_path']),
                    $q($p['vessel_type']),
                    $p['sort_order'],
                    $now, $now,
                ));
            }
        }

        $row = $this->fetchRow('SELECT COUNT(*) AS count FROM testimonials');
        if ($row === false || (int)$row['count'] === 0) {
            $quotes = [
                ["I came in worried I'd be the slowest in the room. It turned out there was no room like that — just me, a wheel, and someone patient.", 'Ava · pottery, 6 weeks in', 10],
                ["My daughter's first lesson ended with her asking when the next one was. That told me everything.", 'Marcus · parent', 20],
                ["I'd put knitting down twenty years ago. I picked it up here in an afternoon.", 'Rina · knitting, returning maker', 30],
            ];
            foreach ($quotes as [$quote, $attribution, $order]) {
                $this->execute(sprintf(
                    "INSERT INTO testimonials (quote, attribution, sort_order, created, modified) VALUES ('%s','%s',%d,'%s','%s')",
                    $q($quote), $q($attribution), $order, $now, $now,
                ));
            }
        }

        $row = $this->fetchRow('SELECT COUNT(*) AS count FROM landing_stats');
        if ($row === false || (int)$row['count'] === 0) {
            $stats = [
                ['Crafts taught',   2,   null, 'clay', 10],
                ['Learners taught', 120, '+',  'ink',  20],
                ['Avg. reply time', 48,  'h',  'moss', 30],
            ];
            foreach ($stats as [$label, $value, $suffix, $tone, $order]) {
                $this->execute(sprintf(
                    "INSERT INTO landing_stats (label, value, suffix, tone, sort_order, created, modified) VALUES ('%s',%d,%s,'%s',%d,'%s','%s')",
                    $q($label),
                    $value,
                    $suffix === null ? 'NULL' : "'" . $q($suffix) . "'",
                    $q($tone),
                    $order,
                    $now, $now,
                ));
            }
        }
    }

    public function down(): void
    {
        $this->execute('DELETE FROM programs');
        $this->execute('DELETE FROM testimonials');
        $this->execute('DELETE FROM landing_stats');
    }
}
