<?php

namespace App\Models\Juil;

use App\Models\User;
use App\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class ConstructionSitePhoto extends Model
{
    use BelongsToTenant, SoftDeletes;

    protected $table = 'construction_site_photos';

    protected $fillable = [
        'tenant_id',
        'user_id',
        'site_name',
        'work_date',
        'description',
    ];

    protected $casts = [
        'work_date' => 'date',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime',
    ];

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

    public function rows(): HasMany
    {
        return $this->hasMany(ConstructionSitePhotoRow::class)->orderBy('sort_order');
    }

    public function getPhotoCount(): int
    {
        $count = 0;
        foreach ($this->rows as $row) {
            $count += $row->getPhotoCount();
        }

        return $count;
    }
}
