<?php
/**
 * Database Migration 003
 * Add Telegram Bot Integration
 * 
 * Adds telegram_user_id and telegram_chat_id fields
 * to support Telegram bot interactions
 */

namespace Database\Migrations;

class Migration_003_AddTelegramBotIntegration {
    
    protected $db;
    
    public function __construct($db) {
        $this->db = $db;
    }
    
    /**
     * Run migration up
     */
    public function up() {
        try {
            $this->db->beginTransaction();
            
            // Add telegram_user_id column
            $this->db->exec('
                ALTER TABLE users ADD COLUMN telegram_user_id VARCHAR(50) UNIQUE NULL
                AFTER user_id
            ');
            
            // Add telegram_chat_id column
            $this->db->exec('
                ALTER TABLE users ADD COLUMN telegram_chat_id VARCHAR(50) NULL
                AFTER telegram_user_id
            ');
            
            // Create settings table if not exists
            $this->db->exec('
                CREATE TABLE IF NOT EXISTS settings (
                    id INT PRIMARY KEY AUTO_INCREMENT,
                    setting_key VARCHAR(100) UNIQUE NOT NULL,
                    setting_value LONGTEXT,
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
                )
            ');
            
            // Insert telegram settings
            $this->db->exec('
                INSERT IGNORE INTO settings (setting_key, setting_value) VALUES
                ("telegram_user_bot_token", ""),
                ("telegram_admin_bot_token", ""),
                ("telegram_admin_chat_ids", ""),
                ("telegram_webhook_enabled", "0")
            ');
            
            // Create telegram_logs table for debugging
            $this->db->exec('
                CREATE TABLE IF NOT EXISTS telegram_logs (
                    id INT PRIMARY KEY AUTO_INCREMENT,
                    bot_type ENUM("user", "admin") NOT NULL,
                    chat_id VARCHAR(50),
                    user_id INT,
                    message_type VARCHAR(50),
                    message_data LONGTEXT,
                    response LONGTEXT,
                    status VARCHAR(20),
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                )
            ');
            
            // Create indexes
            $this->db->exec('
                CREATE INDEX idx_telegram_user_id ON users(telegram_user_id)
            ');
            
            $this->db->exec('
                CREATE INDEX idx_telegram_chat_id ON users(telegram_chat_id)
            ');
            
            $this->db->commit();
            
            echo "✅ Migration 003 completed: Telegram Bot Integration added\n";
            return true;
            
        } catch (\Exception $e) {
            $this->db->rollBack();
            echo "❌ Migration Error: " . $e->getMessage() . "\n";
            return false;
        }
    }
    
    /**
     * Run migration down (rollback)
     */
    public function down() {
        try {
            $this->db->beginTransaction();
            
            // Drop tables
            $this->db->exec('DROP TABLE IF EXISTS telegram_logs');
            
            // Drop columns
            $this->db->exec('ALTER TABLE users DROP COLUMN telegram_user_id');
            $this->db->exec('ALTER TABLE users DROP COLUMN telegram_chat_id');
            
            $this->db->commit();
            
            echo "✅ Migration 003 rolled back\n";
            return true;
            
        } catch (\Exception $e) {
            $this->db->rollBack();
            echo "❌ Rollback Error: " . $e->getMessage() . "\n";
            return false;
        }
    }
}
?>
