<?php

namespace App\Models;

use App\Enums\AiUsageStatus;
use App\Enums\AiUsageType;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class AiUsage extends Model
{
    /** @use HasFactory<\Database\Factories\AiUsageFactory> */
    use HasFactory, HasUlids;

    protected $fillable = [
        'user_id',
        'type',
        'status',
        'input',
        'output',
        'provider',
        'model',
        'prompt_tokens',
        'completion_tokens',
        'total_tokens',
        'error_message',
        'related_type',
        'related_id',
    ];

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

    public function related(): MorphTo
    {
        return $this->morphTo();
    }

    protected function casts(): array
    {
        return [
            'type' => AiUsageType::class,
            'status' => AiUsageStatus::class,
            'input' => 'array',
            'output' => 'array',
            'prompt_tokens' => 'integer',
            'completion_tokens' => 'integer',
            'total_tokens' => 'integer',
        ];
    }
}
