<?php

namespace App\Modules\Clients;

use App\Models\ValidationModel;
use App\Modules\Clients\SiteUser;
use App\Modules\Clients\Client;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Site extends \Eloquent
{
    use \Venturecraft\Revisionable\RevisionableTrait;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'sites';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'client_id', 'address', 'address2', 'postcode', 'primary_phone'];

    /**
     * Returns all the site users (contact persons).
     */
    public function users(): HasMany
    {
        return $this->hasMany(SiteUser::class);
    }

    /**
     * Returns all the site users (alias for backward compatibility).
     */
    public function site_users(): HasMany
    {
        return $this->hasMany(SiteUser::class);
    }

    /**
     * Retrieves the client for this site.
     */
    public function client(): BelongsTo
    {
        return $this->belongsTo(Client::class);
    }

    /**
     * Get the address as a one line string.
     */
    public function getInlineAddressAttribute(): string
    {
        $address = [];
        if ($this->address) {
            $address[] = $this->address;
        }
        if ($this->address2) {
            $address[] = $this->address2;
        }
        if ($this->postcode) {
            $address[] = $this->postcode;
        }

        return implode(', ', $address);
    }
}
