<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class ProjectProgressPhoto extends Model
{
    protected $table = 'project_progress_photos';

    protected $fillable = [
        'project_progress_id',
        'photo_url',
        'caption',
        'uploaded_by',
        'uploaded_at',
    ];

    protected $casts = [
        'uploaded_at' => 'datetime',
    ];

    public function progress(): BelongsTo
    {
        return $this->belongsTo(ProjectProgress::class, 'project_progress_id');
    }

    public function uploader(): BelongsTo
    {
        return $this->belongsTo(User::class, 'uploaded_by');
    }

    public function getPhotoUrlAttribute($value): string
    {
        if (filter_var($value, FILTER_VALIDATE_URL)) {
            return $value;
        }
        return asset('storage/' . $value);
    }

    public function scopeByProgress($query, $progressId)
    {
        return $query->where('project_progress_id', $progressId);
    }
}