<?php
declare(strict_types=1);

use Migrations\BaseMigration;

/**
 * Seeds 1,000 realistic-looking enquiries for stress-testing the admin inbox.
 *
 * Idempotent: only inserts if `contact_messages` currently has < 100 rows
 * (i.e. the original demo seed only). Safe to run repeatedly without ballooning.
 *
 * To remove the bulk rows later, run `bin/cake migrations rollback` or:
 *     DELETE FROM contact_messages WHERE email LIKE '%@stresstest.local';
 */
class SeedThousandEnquiries extends BaseMigration
{
    public function up(): void
    {
        $row = $this->fetchRow('SELECT COUNT(*) AS count FROM contact_messages');
        $existing = $row !== false ? (int)$row['count'] : 0;
        if ($existing >= 100) {
            return;
        }

        $first = ['Maya', 'Luca', 'Erin', 'Zara', 'Ahmed', 'Priya', 'Tom', 'Olivia', 'Sam', 'Jess',
            'Ravi', 'Mei', 'Ben', 'Nora', 'Jin', 'Hana', 'Felix', 'Iris', 'Theo', 'Amara',
            'Hugo', 'Aria', 'Eli', 'Chloe', 'Kai', 'Yusuf', 'Ines', 'Owen', 'Saoirse', 'Diego',
            'Leila', 'Marcus', 'Nadia', 'Pip', 'Quinn', 'Reena', 'Soren', 'Tess', 'Uma', 'Vikram'];
        $last = ['Chen', 'Singh', 'Walsh', 'Nguyen', 'Patel', 'Khan', 'Lee', 'Garcia', 'Wong', 'Anderson',
            'Park', 'Cohen', 'Brown', 'Rossi', 'Murphy', 'Tanaka', 'Diallo', 'Rivera', 'Andersson', 'Kimura',
            'Martin', 'Hassan', 'Vargas', 'Olsen', 'Yusuf', 'Marin', 'Pereira', 'Connor', 'Schmidt', "O'Sullivan"];

        $interests = ['pottery', 'knitting', 'both', 'not_sure'];
        $experiences = ['beginner', 'some', 'returning', 'advanced', null];
        $statuses = ['new', 'new', 'new', 'in_progress', 'in_progress', 'resolved', 'resolved', 'resolved', 'resolved', 'spam'];

        $subjects = [
            'Trying pottery for the first time',
            'Beginner knitting question',
            'Booking for a birthday gift',
            'Are weekend slots available?',
            'Do you teach kids?',
            'Returning after a long break',
            'Group class for two friends',
            'Christmas gift card',
            'Allergic to wool — alternatives?',
            'Wheelchair access?',
            'Just curious — what\'s a class like?',
            'Want to make a mug for a friend',
            'Cable knitting help',
            'Glazing my own pots',
            'Private lesson availability',
            null, null,
        ];

        $bodies = [
            "Hi, I've never done any pottery or knitting before but I'd really love to start. Could you tell me a little about how the first lesson works and what I'd need to bring?",
            "Looking to book a class as a birthday gift for my sister. She's a complete beginner. What's the best option?",
            "I knitted as a teenager but haven't picked up needles in maybe 15 years. I'd love to ease back into it — is there a specific class you'd recommend for someone returning?",
            "My partner and I would love to do a class together. Do you do pairs / two-person sessions?",
            "We have a 12 year old who's been asking to try pottery. Can children join, and from what age?",
            "I want to make my own mug — like, the whole thing including glazing. Roughly how many sessions does that take?",
            "Allergic to wool but I really want to learn knitting. Do you have non-wool yarn options for beginners?",
            "Just exploring options for a quiet hobby that gets me away from screens. Pottery looked nice. Is the studio noisy?",
            "Hi! I'd like to book a few sessions before Christmas as a present. Do you do gift cards?",
            "Need a wheelchair-accessible studio. Is your space accessible? Happy to chat on the phone if easier.",
            "I have intermediate hand-building experience but no wheel time. Can I jump straight to wheel?",
            "What's the cancellation policy if I need to reschedule?",
            "Are there any open slots this Saturday?",
            "Considering pottery as a regular weekly thing for the next couple of months. Do you do bulk packs?",
        ];

        $now = new DateTimeImmutable();
        $rows = [];
        for ($i = 0; $i < 1000; $i++) {
            $fname = $first[array_rand($first)];
            $lname = $last[array_rand($last)];
            $email = strtolower($fname . '.' . preg_replace('/[^a-z]/i', '', $lname) . ($i + 1)) . '@stresstest.local';
            $interest = $interests[array_rand($interests)];
            $experience = $experiences[array_rand($experiences)];
            $subject = $subjects[array_rand($subjects)];
            $body = $bodies[array_rand($bodies)];
            $status = $statuses[array_rand($statuses)];
            // Spread created dates over the last 120 days
            $daysAgo = random_int(0, 120);
            $minutesAgo = random_int(0, 1440);
            $created = $now->modify("-{$daysAgo} days -{$minutesAgo} minutes")->format('Y-m-d H:i:s');

            $rows[] = [
                'full_name' => $fname . ' ' . $lname,
                'email' => $email,
                'phone' => random_int(0, 1) ? '04' . random_int(10000000, 99999999) : null,
                'interest_area' => $interest,
                'experience_level' => $experience,
                'subject' => $subject,
                'goals' => null,
                'message_body' => $body,
                'status' => $status,
                'admin_notes' => null,
                'consent_to_contact' => 1,
                'marketing_opt_in_email' => random_int(0, 2) === 0 ? 1 : 0,
                'marketing_opt_in_sms' => random_int(0, 4) === 0 ? 1 : 0,
                'reviewed_by_admin_user_id' => $status === 'resolved' || $status === 'in_progress' ? 1 : null,
                'created' => $created,
                'modified' => $created,
            ];
        }

        $table = $this->table('contact_messages');
        // Chunk inserts to keep statements small and avoid max-allowed-packet issues.
        foreach (array_chunk($rows, 100) as $chunk) {
            $table->insert($chunk)->saveData();
        }
    }

    public function down(): void
    {
        $this->execute("DELETE FROM contact_messages WHERE email LIKE '%@stresstest.local'");
    }
}
