<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Every call to ClaudeService (app/Libraries/ClaudeService.php) writes
 * exactly one row here, success or failure — the guardrail from the
 * Phase 5 AI brief that nothing this app does with Claude is ever
 * silent. `feature` identifies which feature made the call (e.g.
 * 'kpi_narration', 'maintenance_triage'); `related_type`/`related_id`
 * is an optional polymorphic pointer (e.g. 'maintenance_work_order'/7)
 * so a specific record's AI history can be looked up without a table
 * per feature. `request_messages`/`tool_calls` are stored as JSON text
 * (not a JSON column type — matches this schema's existing convention
 * of TEXT for structured blobs, e.g. message_log.provider_response)
 * so the full exchange is inspectable for debugging/cost tracking.
 * `triggered_by` is null for scheduled/system-initiated calls, a real
 * user id for anything a staff member clicked.
 */
class CreateAiCallLogTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'feature' => [
                'type'       => 'VARCHAR',
                'constraint' => 60,
            ],
            'related_type' => [
                'type'       => 'VARCHAR',
                'constraint' => 60,
                'null'       => true,
            ],
            'related_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'model' => [
                'type'       => 'VARCHAR',
                'constraint' => 60,
                'null'       => true,
            ],
            'system_prompt' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'request_messages' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'tool_calls' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'response_text' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'input_tokens' => [
                'type'     => 'INT',
                'unsigned' => true,
                'null'     => true,
            ],
            'output_tokens' => [
                'type'     => 'INT',
                'unsigned' => true,
                'null'     => true,
            ],
            'success' => [
                'type'       => 'TINYINT',
                'constraint' => 1,
                'default'    => 0,
            ],
            'error_message' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'triggered_by' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey('feature');
        $this->forge->addKey(['related_type', 'related_id']);
        $this->forge->addForeignKey('triggered_by', 'users', 'id', '', 'SET NULL');
        $this->forge->createTable('ai_call_log');
    }

    public function down()
    {
        $this->forge->dropTable('ai_call_log', true);
    }
}
