<?php
declare(strict_types=1);

use Migrations\BaseMigration;

class SeedTeachersAndSlots extends BaseMigration
{
    public function up(): void
    {
        $row = $this->fetchRow('SELECT COUNT(*) AS count FROM teachers');
        if ($row !== false && (int)$row['count'] > 0) {
            return;
        }

        $now = date('Y-m-d H:i:s');

        $teachersData = [
            [
                'slug' => 'ishani-patel',
                'full_name' => 'Ishani Patel',
                'role' => 'Pottery · Lead instructor',
                'bio' => 'Twelve years on the wheel. Ishani teaches the slow way — shape, breath, repeat — and keeps everyone laughing while the clay does its thing.',
                'specialties' => 'wheel throwing,glazing,beginners',
                'photo_path' => null,
                'hourly_rate_cents' => 7500,
                'is_active' => 1,
                'sort_order' => 1,
                'created' => $now,
                'modified' => $now,
                'class_type' => 'pottery',
            ],
            [
                'slug' => 'marcus-oshea',
                'full_name' => "Marcus O'Shea",
                'role' => 'Pottery · Hand-building',
                'bio' => 'A sculptor by training. Marcus loves hand-building — coils, slabs, pinch pots — and showing people how to stop apologising for their wobbly first mugs.',
                'specialties' => 'hand-building,sculpture,intermediate',
                'photo_path' => null,
                'hourly_rate_cents' => 6500,
                'is_active' => 1,
                'sort_order' => 2,
                'created' => $now,
                'modified' => $now,
                'class_type' => 'pottery',
            ],
            [
                'slug' => 'lena-koh',
                'full_name' => 'Lena Koh',
                'role' => 'Knitting · Lead instructor',
                'bio' => 'Third-generation knitter. Lena starts everyone on a scarf they actually like, then gently nudges them toward jumpers that fit.',
                'specialties' => 'knitting,colourwork,beginners',
                'photo_path' => null,
                'hourly_rate_cents' => 6000,
                'is_active' => 1,
                'sort_order' => 3,
                'created' => $now,
                'modified' => $now,
                'class_type' => 'knitting',
            ],
            [
                'slug' => 'tomas-rivera',
                'full_name' => 'Tomás Rivera',
                'role' => 'Knitting · Cables & lace',
                'bio' => "Obsessed with cables and lace charts. Tomás is the person you want once you've finished your first scarf and are ready for something harder.",
                'specialties' => 'knitting,cables,advanced',
                'photo_path' => null,
                'hourly_rate_cents' => 6500,
                'is_active' => 1,
                'sort_order' => 4,
                'created' => $now,
                'modified' => $now,
                'class_type' => 'knitting',
            ],
        ];

        // Insert teachers (strip the synthetic class_type marker before insert)
        $teachersTable = $this->table('teachers');
        foreach ($teachersData as $t) {
            $row = $t;
            unset($row['class_type']);
            $teachersTable->insert($row)->saveData();
        }

        // Look up the IDs we just created so we can insert slots referencing them.
        $rows = $this->fetchAll('SELECT id, slug FROM teachers');
        $idsBySlug = [];
        foreach ($rows as $r) {
            $idsBySlug[$r['slug']] = (int)$r['id'];
        }
        $classTypeBySlug = [];
        foreach ($teachersData as $t) {
            $classTypeBySlug[$t['slug']] = $t['class_type'];
        }

        $slotRows = [];
        $times = ['10:00:00', '13:00:00', '16:00:00'];
        $base = new DateTimeImmutable('tomorrow');
        foreach ($idsBySlug as $slug => $tid) {
            $classType = $classTypeBySlug[$slug] ?? 'pottery';
            for ($d = 0; $d < 14; $d++) {
                $day = $base->modify("+{$d} days");
                if ($day->format('w') === '0') {
                    continue;
                }
                foreach ($times as $time) {
                    $start = $day->format('Y-m-d') . ' ' . $time;
                    $end = (new DateTimeImmutable($start))->modify('+90 minutes')->format('Y-m-d H:i:s');
                    $slotRows[] = [
                        'teacher_id' => $tid,
                        'starts_at' => $start,
                        'ends_at' => $end,
                        'class_type' => $classType,
                        'capacity' => 1,
                        'is_booked' => 0,
                        'created' => $now,
                        'modified' => $now,
                    ];
                }
            }
        }

        if (!empty($slotRows)) {
            $slotsTable = $this->table('availability_slots');
            $slotsTable->insert($slotRows)->saveData();
        }
    }

    public function down(): void
    {
        $this->execute('DELETE FROM availability_slots');
        $this->execute('DELETE FROM teachers');
    }
}
