<?php

namespace console\migrations;

use yii\db\Migration;

/**
 * Class m250801_150000_rename_bookings_to_lesson_bookings
 * 
 * Renames bookings table to lesson_bookings to avoid conflicts
 */
class m250801_150000_rename_bookings_to_lesson_bookings extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        // Drop foreign key constraints first
        $this->dropForeignKey('fk-booking_history-booking_id', 'booking_history');
        
        // Rename the table
        $this->renameTable('bookings', 'lesson_bookings');
        
        // Re-add the foreign key with updated reference
        $this->addForeignKey(
            'fk-booking_history-lesson_booking_id',
            'booking_history',
            'booking_id',
            'lesson_bookings',
            'id',
            'CASCADE'
        );
        
        echo "Renamed bookings table to lesson_bookings successfully!\n";
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        // Drop foreign key constraints first
        $this->dropForeignKey('fk-booking_history-lesson_booking_id', 'booking_history');
        
        // Rename back to original
        $this->renameTable('lesson_bookings', 'bookings');
        
        // Re-add the original foreign key
        $this->addForeignKey(
            'fk-booking_history-booking_id',
            'booking_history',
            'booking_id',
            'bookings',
            'id',
            'CASCADE'
        );
        
        echo "Renamed lesson_bookings table back to bookings!\n";
    }
}
