<?php
declare(strict_types=1);

use Migrations\BaseMigration;

class AddBookingAndTeamTables extends BaseMigration
{
    public function change(): void
    {
        // Teachers
        $teachers = $this->table('teachers');
        $teachers
            ->addColumn('slug', 'string', ['limit' => 80, 'null' => false])
            ->addColumn('full_name', 'string', ['limit' => 150, 'null' => false])
            ->addColumn('role', 'string', ['limit' => 120, 'null' => false])
            ->addColumn('bio', 'text', ['null' => false])
            ->addColumn('specialties', 'string', ['limit' => 255, 'null' => true, 'comment' => 'comma-separated tags'])
            ->addColumn('photo_path', 'string', ['limit' => 255, 'null' => true])
            ->addColumn('hourly_rate_cents', 'integer', ['default' => 6500, 'null' => false, 'comment' => 'AUD cents per hour'])
            ->addColumn('is_active', 'boolean', ['default' => true, 'null' => false])
            ->addColumn('sort_order', 'integer', ['default' => 0, 'null' => false])
            ->addColumn('created', 'datetime', ['null' => false])
            ->addColumn('modified', 'datetime', ['null' => false])
            ->addIndex(['slug'], ['unique' => true])
            ->create();

        // Availability slots
        $slots = $this->table('availability_slots');
        $slots
            ->addColumn('teacher_id', 'integer', ['null' => false])
            ->addColumn('starts_at', 'datetime', ['null' => false])
            ->addColumn('ends_at', 'datetime', ['null' => false])
            ->addColumn('class_type', 'string', ['limit' => 40, 'null' => false, 'default' => 'pottery'])
            ->addColumn('capacity', 'integer', ['default' => 1, 'null' => false])
            ->addColumn('is_booked', 'boolean', ['default' => false, 'null' => false])
            ->addColumn('created', 'datetime', ['null' => false])
            ->addColumn('modified', 'datetime', ['null' => false])
            ->addForeignKey('teacher_id', 'teachers', 'id', ['delete' => 'CASCADE', 'update' => 'NO_ACTION'])
            ->addIndex(['teacher_id', 'starts_at'])
            ->addIndex(['starts_at'])
            ->create();

        // Bookings
        $bookings = $this->table('bookings');
        $bookings
            ->addColumn('slot_id', 'integer', ['null' => false])
            ->addColumn('teacher_id', 'integer', ['null' => false])
            ->addColumn('customer_name', 'string', ['limit' => 150, 'null' => false])
            ->addColumn('customer_email', 'string', ['limit' => 255, 'null' => false])
            ->addColumn('customer_phone', 'string', ['limit' => 60, 'null' => true])
            ->addColumn('amount_cents', 'integer', ['null' => false])
            ->addColumn('status', 'string', ['limit' => 20, 'default' => 'pending', 'null' => false, 'comment' => 'pending|paid|cancelled|refunded'])
            ->addColumn('stripe_session_id', 'string', ['limit' => 255, 'null' => true])
            ->addColumn('stripe_payment_intent', 'string', ['limit' => 255, 'null' => true])
            ->addColumn('notes', 'text', ['null' => true])
            ->addColumn('created', 'datetime', ['null' => false])
            ->addColumn('modified', 'datetime', ['null' => false])
            ->addForeignKey('slot_id', 'availability_slots', 'id', ['delete' => 'RESTRICT', 'update' => 'NO_ACTION'])
            ->addForeignKey('teacher_id', 'teachers', 'id', ['delete' => 'RESTRICT', 'update' => 'NO_ACTION'])
            ->addIndex(['status'])
            ->addIndex(['customer_email'])
            ->addIndex(['stripe_session_id'])
            ->create();

        // Extend contact_messages with new enquiry fields
        $cm = $this->table('contact_messages');
        $cm
            ->addColumn('experience_level', 'string', [
                'limit' => 40,
                'null' => true,
                'after' => 'interest_area',
                'comment' => 'beginner|some|returning|advanced',
            ])
            ->addColumn('goals', 'text', [
                'null' => true,
                'after' => 'subject',
                'comment' => 'what they hope to get out of it',
            ])
            ->addColumn('marketing_opt_in_email', 'boolean', [
                'default' => false,
                'null' => false,
                'after' => 'consent_to_contact',
            ])
            ->addColumn('marketing_opt_in_sms', 'boolean', [
                'default' => false,
                'null' => false,
                'after' => 'marketing_opt_in_email',
            ])
            ->update();
    }
}
