<?php

namespace App\Models;

use App\Models\Concerns\BelongsToOwner;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Str;

class Transcript extends Model
{
    use BelongsToOwner;

    protected $table = 'meet_transcripts';

    protected $fillable = [
        'uuid', 'owner_ref', 'session_id', 'recording_id', 'status',
        'content', 'segments', 'live_captions_enabled',
    ];

    protected function casts(): array
    {
        return ['segments' => 'array', 'live_captions_enabled' => 'boolean'];
    }

    protected static function booted(): void
    {
        static::creating(function (Transcript $transcript) {
            if (empty($transcript->uuid)) {
                $transcript->uuid = (string) Str::uuid();
            }
        });
    }

    public function session(): BelongsTo
    {
        return $this->belongsTo(Session::class, 'session_id');
    }

    public function recording(): BelongsTo
    {
        return $this->belongsTo(Recording::class, 'recording_id');
    }

    public function aiSummary(): HasOne
    {
        return $this->hasOne(AiSummary::class, 'transcript_id');
    }
}
