<?php

use ZenoChatbot\Framework\Database\Migration\Migration;

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

return new class extends Migration {
    protected $table = 'zenochatbot_messages';

    public function up()
    {
        return $this->create($this->table, [
            'id' => 'BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY',
            'conversation_id' => 'BIGINT UNSIGNED NOT NULL',
            'sender_type' => "VARCHAR(20) NOT NULL DEFAULT 'customer'", // customer|ai|admin
            'sender_id' => 'BIGINT UNSIGNED NULL',
            'message' => 'LONGTEXT NOT NULL',
            'prompt_tokens' => 'INT DEFAULT 0',
            'completion_tokens' => 'INT DEFAULT 0',
            'token_usage' => 'INT DEFAULT 0',
            'sent_via' => "varchar(20) default 'chat'", // chat|email
            'created_at' => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP',
            'updated_at' => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'
        ], [
            'INDEX idx_conversation_id (conversation_id)',
            'INDEX idx_sender_type (sender_type)',
            'INDEX idx_sender_id (sender_id)',
            'INDEX idx_created_at (created_at)',
            'INDEX idx_sent_via (sent_via)',
            'INDEX idx_conversation_created (conversation_id, created_at)',
        ]);
    }

    public function down()
    {
        return $this->drop($this->table);
    }
};
