<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class InspectionCollection extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'inspection_workspace_id',
        'name',
        'collection_type',
        'property_name',
        'room_name',
        'property_address',
        'metadata',
    ];

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

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

    public function workspace(): BelongsTo
    {
        return $this->belongsTo(InspectionWorkspace::class, 'inspection_workspace_id');
    }

    public function inspections(): HasMany
    {
        return $this->hasMany(Inspection::class, 'inspection_collection_id');
    }

    public function displayName(): string
    {
        return $this->room_name
            ? "{$this->property_name} / {$this->room_name}"
            : $this->property_name;
    }
}
