<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class AgentLog extends Model
{
    protected $keyType = 'string';
    public $incrementing = false;

    protected $fillable = [
        'user_id',
        'type',
        'input',
        'output',
        'status',

        // 🔥 Billing
        'credits',

        // 🔥 AI Tracking
        'model',
        'prompt_tokens',
        'completion_tokens',
        'total_tokens',
        'cost_usd',

        // 🔥 Performance
        'duration_ms',

        // 🔥 Debugging
        'ai_response',
    ];

    protected $casts = [
        'ai_response' => 'array',
        'output' => 'array',

        // 🔥 Token + Metrics Casting
        'prompt_tokens' => 'integer',
        'completion_tokens' => 'integer',
        'total_tokens' => 'integer',
        'credits' => 'integer',
        'duration_ms' => 'integer',
    ];

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            if (!$model->id) {
                $model->id = (string) Str::uuid();
            }
        });
    }

    // 🔥 Relation
    public function user()
    {
        return $this->belongsTo(User::class);
    }

}
