<?php

use ZenoChatbot\Framework\Database\Migration\Migration;

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

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

    public function up()
    {
        return $this->create($this->table, [
            'id'           => 'BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY',
            'customer_id'  => 'BIGINT UNSIGNED NULL',
            'department'   => 'VARCHAR(100) NULL',
            'started_at'   => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP',
            'ended_at'     => 'TIMESTAMP NULL',
            'status'       => 'varchar(20) default "open"', // open|closed
            'total_tokens' => 'INT UNSIGNED NULL DEFAULT 0',
            'created_at'   => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP',
            'updated_at'   => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'
        ], [
            'INDEX idx_customer_id (customer_id)',
            'INDEX idx_department (department)',
            'INDEX idx_started_at (started_at)',
            'INDEX idx_ended_at (ended_at)',
            'INDEX idx_customer_started (customer_id, started_at)',
            'INDEX idx_department_started (department, started_at)'
        ]);
    }

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