<?php

namespace App\Models;

use App\Models\Scopes\TenantScope;
use Illuminate\Database\Eloquent\Model;

class TutorialVideo extends Model
{
    protected $fillable = [
        'tenant_id',
        'user_id',
        'title',
        'status',
        'progress',
        'current_step',
        'screenshots',
        'analysis_data',
        'slides_data',
        'output_path',
        'gcs_path',
        'cost_usd',
        'error_message',
    ];

    protected $casts = [
        'screenshots' => 'array',
        'analysis_data' => 'array',
        'slides_data' => 'array',
        'progress' => 'integer',
        'cost_usd' => 'decimal:4',
    ];

    protected static function booted(): void
    {
        static::addGlobalScope(new TenantScope);
    }

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

    const STATUS_PENDING = 'pending';

    const STATUS_ANALYZING = 'analyzing';

    const STATUS_GENERATING_SLIDES = 'generating_slides';

    const STATUS_GENERATING_TTS = 'generating_tts';

    const STATUS_GENERATING_BGM = 'generating_bgm';

    const STATUS_ASSEMBLING = 'assembling';

    const STATUS_COMPLETED = 'completed';

    const STATUS_FAILED = 'failed';

    public function updateProgress(string $status, int $progress, string $step): void
    {
        $this->update([
            'status' => $status,
            'progress' => $progress,
            'current_step' => $step,
        ]);
    }

    public function markFailed(string $errorMessage): void
    {
        $this->update([
            'status' => self::STATUS_FAILED,
            'error_message' => $errorMessage,
        ]);
    }
}
