<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class AiDocumentChunk extends Model
{
    protected $fillable = [
        'document_id',
        'chunk_index',
        'content',
        'embedding'
    ];

    // Don't cast embedding to array automatically
    // We'll handle JSON encoding/decoding in the service
    protected $casts = [
        'chunk_index' => 'integer',
    ];

    public function document()
    {
        return $this->belongsTo(AiDocument::class, 'document_id');
    }
    
    // Helper to get embedding as array
    public function getEmbeddingArrayAttribute()
    {
        return is_string($this->embedding) 
            ? json_decode($this->embedding, true) 
            : $this->embedding;
    }
}